Help - Search - Members - Calendar
Full Version: css dropdownmenu
HTMLHelp Forums > Web Authoring > Cascading Style Sheets
MindyT
Hi, I'm trying to do a dropdown menu with CSS. The dropdown menu appears whether the mouse is over the menu or not. if someone could help me figure out what 's wrong, I would really appreciate it.

CODE

<php
require_once('functions.php');
?>  
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Untitled Document</title>
<meta name="keywords" content="">
<meta name="description" content="">
<link rel = "stylesheet" href = "Bixler.css">
<style type="text/css">
    nav {
        padding-top:2%;
        width:30%;
        height:27px;
        margin-left:auto;
        margin-right:auto;
    }
    nav ul {
        list-style-type:none;
    }
    nav > ul li a {
        padding:50px;  
    }
    
    nav li {
        float:left;
    }
    nav > ul > li > ul {
        display:none;
        position:absolute;
    }
    nav > ul > li:hover > ul {
        display:block;
    }
    #logo {
        width:304px;
        margin-left:auto;
        margin-right:auto;
    }
    h3 {
        color:red;
        text-align:center;
    }
    #contactInfo {
        float:left;
    }
    #servicesList, #eocialMedia {
        float:right;
        width: 33%;
    }
    article {
        float:left;
        width:35%;
        height:45%;
        margin-left:10%;
        background-color:#FFFFFF;
    }
    aside {  
        float:right;    
        width:30%;
        height:35%;
        margin-right:10%;     
        background-color:#FFFFFF;    
    }
    footer {
        background-color:#000000;
        overflow:auto;
        color:#C4C4C4;
          clear:both;
    }
</style>
</head>
<body>
<header>
<nav>
    <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">About Us</a></li>
            <ul>
                   <li><a href="#">Agents</a></li>
                <li><a href="#">Locations</a></li>                
            </ul>
        <li><a href="#">Services</a></li>              
        <li><a href="services.php" tabindex="3" accesskey="S">Services</a></li>
        <li><a href="quote.php" tabindex="4" accesskey="Q">Get A Quote</a></li>
        <li><a href="contact.php" tabindex="5" accesskey="C">Contact</a></li>
   </ul>
</nav>  
    <figure id="logo">
           <img src="assets/bixlerlogo.png" width="304" height="88" alt=""/>
    </figure>
    <h3>Service Beyond The Contract </h3>
</header>
<article>

</article>
<aside>

</aside>
<footer>
    <div id="contactInfo">
        Bixler Insurance <br/>
        1043 South 13th St. <br />
        Decatur, IN 46733<br />
        Phone: (260)-724-3438
    </div>
    <div id="eocialMedia">
        social media icons
    </div>    
    <div id="servicesList">
        list of services
    </div>
</footer>
<script src="../js/jquery-1.11.3.min.js"></script>
<script src="../js/bootstrap.js"></script>
</body>
</html>
pandy
You use a child selector but the last UL isn't a child of LI because you have nested the lists wrong.

QUOTE
CODE

      <li><a href="#">About Us</a></li>
            <ul>
                   <li><a href="#">Agents</a></li>
                <li><a href="#">Locations</a></li>                
            </ul>
        <li><a href="#">Services</a></li>


Your nested UL is directly inside the outer UL. The selector doesn't match and it's bad HTML. In a list, the only thing that can be directly contained in UL is LI. Så you must put your nested UL inside a LI, probably the last one.
Frederiek
Just have a look at the HTML code used at, the following links to see how to code a nested list.

http://css.maxdesign.com.au/listamatic2/vertical02.htm
http://meyerweb.com/eric/css/edge/menus/demo.html (inspect the sub menus in the top left menu)
MindyT
ah I see the problem now! Thanks so much for the help!
MindyT
I am so sorry, but one more question. The dropdown menu is kind of working, but submenu is going horizontally not vertically. I thought if I put

CODE

    nav ul ul li {
        display:block;
        width:60px;
    }
that was what was needed, but evidently not. Could I get some help please?
Christian J
QUOTE(MindyT @ Jun 3 2016, 04:14 AM) *

submenu is going horizontally not vertically. I thought if I put

CODE

    nav ul ul li {
        display:block;
        width:60px;
    }
that was what was needed, but evidently not.

Maybe it's because the LIs are already floated:

CODE
    nav li {
        float:left;
    }

Try set a width on the inner UL instead (and perhaps remove the LI float):

CODE
nav ul ul {
        display:block;
        width:60px;
    }

(Though I don't know exactly what your current HTML structure looks like.)


MindyT
I already have the width set and that didn't help. Removing float didn't help either.

here is the css code
CODE

* {
    margin:0;
    padding:0;
}
body {
    Height:100%;
    width:100%;
    background-color:#cccb99;
}

    nav {
        background-color:#000000;
        padding-top:2%;
        width:34%;
        height:27px;
        margin-top:2%;
        margin-left:auto;
        margin-right:auto;
        padding-left:3%;
    }
    nav a {
        color:red;
    }
        
    nav ul {
        list-style-type:none;
    }
    nav > ul > li {

    }
    nav > ul li a {
        background-color:#000000;
    }
    
    nav li {
        float:left;
        margin-right:3%;
    }
    nav > ul > li > ul {
        display:none;
        position:absolute;
    }
    nav > ul > li:hover > ul {
        display:block;
    }
    nav ul ul li {
        display:block;
        width:60px;
    }
    #logo {
        width:304px;
        margin-left:auto;
        margin-right:auto;
        margin-top:2%;
        margin-bottom:2%
    }
    h3 {
        color:#763c3c;
        text-align:center;
        margin-bottom:2%;
    }
    #contactInfo {
        float:left;
    }
    #servicesList, #eocialMedia {
        float:right;
        width: 33%;
    }
    article {
        float:left;
        width:35%;
        height:45%;
        margin-left:10%;
        margin-bottom:6%;
        background-color:#FFFFFF;
    }
    aside {  
        float:right;    
        width:30%;
        height:35%;
        margin-right:10%;     
        background-color:#FFFFFF;    
    }
    footer {
        background-color:#000000;
        overflow:auto;
        Width:100%;
        padding-left:10%;
        padding-top:2%;
        padding-bottom:2%;
        color:#C4C4C4;
          clear:both;
    }


html code
[code]
<php
require_once('functions.php');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Untitled Document</title>
<meta name="keywords" content="">
<meta name="description" content="">
<link rel = "stylesheet" href = "Bixler.css">
<style type="text/css">

</style>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About Us</a>
<ul>
<li><a href="#">Agents</a></li>
<li><a href="#">Locations</a></li>
</ul>
<li><!--end of about us menu list -->
<li><a href="#">Services</a></li>
<li><a href="services.php" tabindex="3" accesskey="S">Services</a></li>
<li><a href="quote.php" tabindex="4" accesskey="Q">Get A Quote</a></li>
<li><a href="contact.php" tabindex="5" accesskey="C">Contact</a></li>
</ul>
</nav>
<figure id="logo">
<img src="assets/bixlerlogo.png" width="304" height="88" alt=""/>
</figure>
<h3>Service Beyond The Contract </h3>
</header>
<article>

</article>
<aside>

</aside>
<footer>
<div id="contactInfo">
Bixler Insurance <br/>
1043 South 13th St. <br />
Decatur, IN 46733<br />
Phone: (260)-724-3438
</div>
<div id="eocialMedia">
social media icons
</div>
<div id="servicesList">
list of services
</div>
</footer>
</body>
</html>
Frederiek
I tested your last HTML and CSS code and the dropdown menu items are going vertically, despite the float they inherit from the outer LIs.

Though I'd put the background-color to the inner LI and add some padding to add some space.
Christian J
QUOTE(MindyT @ Jun 3 2016, 07:37 PM) *

I already have the width set

Not on the UL element.

QUOTE
and that didn't help. Removing float didn't help either.

I get a vertical submenu even without those changes, just like Frederiek.

QUOTE
<li><!--end of about us menu list -->

That should be an </li> end tag.

Also, using a percentage width for the NAV menu may cause problems in different window sizes.

BTW, hover menus work very poorly in touch screen browsers. This is because tapping a link on a touch screen fires both a hover and a click (try it so see what I mean).
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2019 Invision Power Services, Inc.