Skip to content Skip to sidebar Skip to footer

Creating A Dropdown List Item Menu With Css Only

In a section of website I'm working on I have a NAV element that contains three sections: About, Portfolio, Contact. I'm trying to make it so that when you hover over the Portfolio

Solution 1:

This is the basics of it:

nav {
  position: absolute;
  padding: 10px0;
}
navul {
  list-style: none;
  ;
  padding: 0;
}
nav > ul > li {
  display: inline-block;
  position: relative;
  border: 1px solid lightgreen;
  /* for demo */
}
nava {
  font-weight: 800;
  padding: 5px10px;
  display: block;
}
nav > ul > li.subNavul {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  white-space: nowrap;
  background: pink;
}
navulli.subNav:hoverul {
  display: block;
}
<nav><ul><li><ahref="index.html">About</a></li><liclass="subNav"><aclass="selected">Portfolio</a><ul><li><ahref="writing_samples.html">Writing Samples</a></li><li><ahref="photoshop.html">Photoshop</a></li></ul></li><li><ahref="contact.html">Contact</a></li></ul></nav>

The parent li is given position:relative to provide positioning context.

The submenu is positioned absolutely, at the bottom of the parent li and aligned left.

Note that I have used the direct child selector> to target only the elements I want to.

Then, since the submenu is too wide to be contained within the parent's width, I added white-space:nowrap so that the text will flow as required.

Solution 2:

You have the right idea; the comment tags in the HTML below are used to remove space between the "li" elements.

Instead of using display:none, I use visibility: hidden for S.E.O purposes.

Even though you use position: absolute, you should also use z-index so that menu elements are able to be clicked if they are overlapping other content.

.mm,
.sm {
  list-style: none;
}
.mm {
  position: relative;
  margin: 0px;
  padding: 0px;
  background-color: #000;
  border-bottom: 4px solid red;
}
.sm {
  position: absolute;
  z-index: 1;
  visibility: hidden;
  background-color: #000;
  border-width: 0px4px4px4px;
  border-style: solid;
  border-color: red;
}
.mm > li {
  display: inline-block;
}
.mm > li > a {
  display: inline-block;
  padding: 8px;
}
.sma {
  display: block;
  padding: 8px;
}
.mm > li > a:hover + .sm,
.sm:hover {
  visibility: visible;
}
.mma {
  text-decoration: none;
  color: #FFF;
}
.mma:hover {
  text-decoration: underline;
  color: yellow;
}
<nav><ulclass="mm"><li><ahref="#"title="#">AAA</a></li><!--
        --><li><ahref="#"title="#">BBB</a><ulclass="sm"><li><ahref="#"title="#">SUB</a></li><!--
                --><li><ahref="#"title="#">SUB</a></li><!--
                --><li><ahref="#"title="#">SUB</a></li></ul></li><!--
		--><li><ahref="#"title="#">CCC</a><ulclass="sm"><li><ahref="#"title="#">SUB</a></li><!--
                --><li><ahref="#"title="#">SUB</a></li><!--
                --><li><ahref="#"title="#">SUB</a></li></ul></li></ul></nav><h1>CSS NAVIGATION</h1>

Post a Comment for "Creating A Dropdown List Item Menu With Css Only"