Skip to content Skip to sidebar Skip to footer

How To Display Submenu On Dropdown Using Positioning And CSS?

I have this simple navigation menu for my design. But the problem is I can't display the sub menu when hovered. Is there any way to solve this? Or does my CSS has some missing styl

Solution 1:

There are quite a few errors in your code. The most critical one is the top: 100% on the submenu container, which moves it out of the visible area.

Apart from that it's important to define the submenu header li as position: relative and the submenu ul itself by default display: none and on hover display: block. And put the submenu container into the li that serves as its header. See my code below:

nav ul {
  list-style-type: none;
  background: #000;
}

nav li {
  display: inline-block;
  position: relative;
}

nav li>a {
  padding: 15px 15px;
  display: inline-block;
  text-decoration: none;
  color: white;
  text-align: center;
}

nav li>a:hover {
  background: violet;
}
nav ul li ul {
  display: none;
  position: absolute;
  top: 45px;
  left: 0px;
  width: 100px;
  overflow: visible;
  padding: 0;
}
nav ul li:hover ul {
  display: block;
}
nav ul ul li {
  background: green;
  display: block;
}
nav ul ul li>a:hover {
  color: red;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>sample UL</title>
  <style media="screen">

  </style>
</head>

<body>
  <nav>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Menu 1</a>
        <ul>
          <li><a href="#">submenu 1</a></li>
          <li><a href="#">submenu 2</a></li>
        </ul>
      </li>
    </ul>
  </nav>
</body>

</html>

Solution 2:

nav ul {
            list-style-type: none;
            overflow: hidden;
            background: #000;
            position: relative;
        }

        nav li {
            float: left;
        }

        nav li > a {
            padding: 15px 15px;
            display: inline-block;
            text-decoration: none;
            color: white;
            text-align: center;
        }

        nav li > a:hover {
            background: violet;
        }

        nav ul ul {
            background: green;
            top: 100%;
        }

        nav ul li a + ul {
            display: none;
        }

        nav ul li a:hover + ul {
            display: block;
        }
        
        .sub-menu:hover {
            display: block;
        }
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>sample UL</title>
</head>
<body>
<nav>
    <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Menu 1</a>
            <ul class="sub-menu">
                <li><a href="#">submenu 1</a></li>
                <li><a href="#">submenu 2</a></li>
            </ul>
        </li>
    </ul>

</nav>
</body>
</html>

Solution 3:

Just try this one. You could learn from it.

<!DOCTYPE html>
<html>
<head>
<style>
ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
}

li {
    float: left;
}

li a, .dropbtn {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
    background-color: red;
}

li.dropdown {
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.dropdown-content a:hover {background-color: #f1f1f1}

.dropdown:hover .dropdown-content {
    display: block;
}
</style>
</head>
<body>

<ul>
  <li><a href="#home">Home</a></li>
  <li><a href="#news">News</a></li>
  <li class="dropdown">
    <a href="javascript:void(0)" class="dropbtn">Dropdown</a>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </li>
</ul>

<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>

</body>
</html>

Solution 4:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>sample UL</title>
    <style media="screen">
    </style>
  </head>
  <body>
    <nav class="cf">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Menu 1</a>
          <ul class="submenu">
            <li><a href="#">submenu 1</a></li>
            <li><a href="#">submenu 2</a></li>
          </ul></li>
      </ul>
    </nav>
  </body>
</html>

Please refer Clearfix

Also given class name to nav as cf and inner ul as submenu

.cf:before, .cf:after {
    content:"";
    display:table;
}

.cf:after {
    clear:both;
}

.cf {
    zoom:1;
}

nav {
    background: #333A31;
    height: 2.3em;
}
ul, li {
    margin: 0;
    padding: 0;
    list-style: none;
    float: left;
}
ul {
    background: #D5333C;
    height: 2em;
    width: 100%;
}
li {
    position: relative;
}
li a {
    display: block;
    line-height: 2em;
    padding: 0 1em;
    color: white;
    text-decoration: none;
}
li a:hover {
    background: #9155311;
    height: 2em;
    padding-top: .3em;
    position: relative;
    top: -.3em;
    border-radius: .3em .3em 0 0;
}
.current, a:hover.current {
    background: #AD9B7F;
    color: #eee;
    padding-top: .3em;
    border-radius: .3em .3em 0 0;
    position: relative;
    top: -.3em;
    border-bottom: .3em solid #917F63;
    cursor: default;
}
/*dropdown menu styles*/
ul.submenu {
    float: none;
    background: #916A31;
    width: auto;
    height: auto;
    position: absolute;
    top: 2em;
    left: -9000em;
}
ul.submenu li {
    float: none;
}
nav li:hover ul {
    left: 0;
}
ul.submenu li a {
    border-bottom: 1px solid white;
    padding: .2em 1em;
    white-space: nowrap;
}
ul.submenu li:last-child a {
    border-bottom: none;
}
ul.submenu li a:hover {
    background: #D5973C;
    height: 2em;
    padding-top: .2em;
    top: 0;
    border-radius: 0;
}

Hope this helps.


Post a Comment for "How To Display Submenu On Dropdown Using Positioning And CSS?"