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 section, a drop down appears allowing you to choose between two other sections, “Writing Samples” and “Photoshop.” I am trying to accomplish this using only CSS.
This is my HTML section:
<nav>
<ul>
<li>
<a href="index.html" >About</a>
</li>
<li class="subNav">
<a class="selected" >Portfolio</a>
<ul>
<li><a href="writing_samples.html">Writing Samples</a></li>
<li><a href="photoshop.html">Photoshop</a></li>
</ul>
</li>
<li>
<a href="contact.html">Contact</a>
</li>
</ul>
</nav>
And CSS:
nav {
position: absolute;
bottom: 0;
right: 0;
padding: 10px 0;
}
nav ul {
list-style: none;
margin: 0 10px;
padding: 0;
}
nav li {
display: inline-block;
}
nav a {
font-weight: 800;
padding: 15px 10px;
}
nav ul li.subNav ul {
display: none;
}
nav ul li.subNav:hover ul {
display: block;
}
I have reached the point that when I hover over the Portfolio list item, you see the resulting list items “Writing Samples” and “Photoshop”, except that it displays these two items as a part of the original unordered list, and moves the “Portfolio” list item above the rest of the items. I would like “Writing Samples” and “Photoshop” to appear vertically under “Portfolio”, but I can’t quite figure this out with CSS.
2
Answers
This is the basics of it:
The parent
li
is givenposition: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.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.