We help IT Professionals succeed at work.

How to apply a css stylesheet to an asp menu

Dovberman
Dovberman asked
on
I used the following example in an attempt to apply a css style sheet to a menu.
http://jobinjohn.blogspot.com/2011/06/styling-aspnet-menu-control-with-css.html

This is my aspx code snippet:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 <asp:Menu ID="Menu1" CssClass="MYMenuClass"
        Orientation="Horizontal"
        MaximumDynamicDisplayLevels="3"
        runat="server">


        <Items>
            <asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item">
                <asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
                <asp:MenuItem NavigateUrl="#" Text="New Item New Item" Value="New Item"></asp:MenuItem>
                <asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
            </asp:MenuItem>

This is my css code snippet:

.MYMenuClass {
    color: Green;
    background-color: #00FFFF;
}

.MYMenuClass ul {
    background: #00FFFF;
}

.MYMenuClass ul li {
    background: #00FFFF none no-repeat;
    text-align: center; /* set width if needed.*/
    width: 200px;
}

.MYMenuClass ul li a
{
    color: black;
    padding: 4px 2px 4px;
    padding-left:8px !important;
    border:1px solid #648ABD;
    border-bottom: 0
   
}

.MYMenuClass ul li a:hover
{
    background-image: none;
}

.MYMenuClass ul li a:hover
{
    color: White;
}

Nothing in the css file is applied to the menu.

What am I missing?

Thanks,
Comment
Watch Question

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Look at the source in the browser and see if your classes are listed in the menu items.  Your menu is generated at the server but CSS is applied in the browser.  If the classes aren't listed in the menu items in the browser, they won't be applied.
Lokesh B RDeveloper
Top Expert 2015

Commented:
Hi,

Just Run the Page and you can see the CSS applied to your Menu items in browser.

Author

Commented:
This is my source:

<asp:Menu ID="Menu1" CssClass="MYMenuClass"
         Orientation="Horizontal"
         MaximumDynamicDisplayLevels="3"
         runat="server">

?? How do I list the class here?
         <Items>
             <asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item">
                 <asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
                 <asp:MenuItem NavigateUrl="#" Text="New Item New Item" Value="New Item">

</asp:MenuItem>
                 <asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
             </asp:MenuItem>

Author

Commented:
The stylesheet  Menu1.css is in the list.
Lokesh B RDeveloper
Top Expert 2015

Commented:
Hi,

Do you want to generate the menu dynamically?

Author

Commented:
I do not care.  I just want the style sheet properties to be applied.
Developer
Top Expert 2015
Commented:
Hi,

Check the following code.


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .MYMenuClass {
            color: Green;
            background-color: #00FFFF;
        }

            .MYMenuClass ul {
                background: #00FFFF;
            }

                .MYMenuClass ul li {
                    background: #00FFFF none no-repeat;
                    text-align: center; /* set width if needed.*/
                    width: 200px;
                }

                    .MYMenuClass ul li a {
                        color: black;
                        padding: 4px 2px 4px;
                        padding-left: 8px !important;
                        border: 1px solid #648ABD;
                        border-bottom: 0;
                    }

                        .MYMenuClass ul li a:hover {
                            background-image: none;
                        }

                        .MYMenuClass ul li a:hover {
                            color: White;
                        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:Menu ID="navMenu" runat="server" Orientation="Horizontal" CssClass="MYMenuClass">
            <Items>
                <asp:MenuItem Text="Home" NavigateUrl="home.aspx"></asp:MenuItem>
                <asp:MenuItem Text="Categories"></asp:MenuItem>
                <asp:MenuItem Text="Products"></asp:MenuItem>
                <asp:MenuItem Text="About Us" NavigateUrl="AboutUs.aspx"></asp:MenuItem>
                <asp:MenuItem Text="Contact Us"></asp:MenuItem>
            </Items>
        </asp:Menu>
    </div>
    </form>
</body>
</html>

output.PNG

Author

Commented:
I understand what you did.

The menu control is not used.  The css code is in the html file (myfile.aspx).

What I do  not understand is why the examples using a style sheet (css file) did not work for me.
Lokesh B RDeveloper
Top Expert 2015
Commented:
Hi,

Check whether your css file is referenced properly.

It will work with the css file as well. See the below sample code of ASPX.

CSS file is in my Content Folder.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="WebApplication1.file.WebForm4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   <link href="../Content/MyMenuCss.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:Menu ID="navMenu" runat="server" Orientation="Horizontal" CssClass="MYMenuClass">
            <Items>
                <asp:MenuItem Text="Home" NavigateUrl="home.aspx"></asp:MenuItem>
                <asp:MenuItem Text="Categories"></asp:MenuItem>
                <asp:MenuItem Text="Products"></asp:MenuItem>
                <asp:MenuItem Text="About Us" NavigateUrl="AboutUs.aspx"></asp:MenuItem>
                <asp:MenuItem Text="Contact Us"></asp:MenuItem>
            </Items>
        </asp:Menu>
    </div>
    </form>
</body>
</html>

Author

Commented:
That makes sense.  I was wondering how to reference the css style sheet file.

Author

Commented:
The dynamic form works.

The css style sheet reference does not work for  either of the references shown below.

<link href="C:\Users\David\Documents\Visual Studio 2012\Projects\StockPickerMax\StockPickerMax\Menu1.css" rel="stylesheet" />

<link href="~/Menu1.css" rel="stylesheet" />

I will use the dynamic method but prefer the style sheet method.

Is my css file correct ?


   .MYMenuClass {
    color:#008000;
    background-color: #00FF00;
}

             .MYMenuClass ul {
                 background: #00FFFF;
             }

                 .MYMenuClass ul li {
                     background: #00FFFF none no-repeat;
                     text-align: center; /* set width if needed.*/
                     width: 200px;
                 }

                     .MYMenuClass ul li a {
                         color: black;
                         padding: 4px 2px 4px;
                         padding-left: 8px !important;
                         border: 1px solid #648ABD;
                         border-bottom: 0;
                     }

                         .MYMenuClass ul li a:hover {
                             background-image: none;
                         }

                         .MYMenuClass ul li a:hover {
                             color: White;
                         }

Thanks for all your help.
Lokesh B RDeveloper
Top Expert 2015
Commented:
Capture.PNGHi,

Just Drag & Drop the CSS file from the Folder to ASPX page in Visual Studio.

As shown in the below image.

Author

Commented:
That worked:

<head id="Head1" runat="server">
     <title></title>
     <link href="Menu1.css" rel="stylesheet" />

 </head>

Thank you.

Author

Commented:
Excellent. Provided just what I needed in a timely fashion.