I'm trying to make a collapsible/expandable navigation bar menu. I have the right element targeted, but I can't get it to show the sub-menu on hover-over.
I'd like to keep the HTML as is, and not use any classes if possible, I'd like to learn the basics of doing this without classes, to attain a better understanding of what I'm doing, in manipulating HTML elements. The main point in doing this, is just to get comfortable with accessing elements.
ul {
list-style: none;
}
ul li a {
color: white;
display: none;
}
ul li:hover a {
display: block;
background-color: red;
}
ul ul li {
background-color: pink;
color: white;
}
ul ul li:hover ul a {
display: block;
background-color: purple;
}
<nav>
<ul>
<li>Music</li>
<ul>
<li>Songs</li>
<ul>
<li>Blue Slide Park</li>
<li>What's The Use</li>
<li>Hurt Feelings</li>
<li>Fight The Feeling</li>
</ul>
<li>Albums</li>
<ul>
<li>Blue Slide Park</li>
<li>WMWTSO</li>
<li>GO:OD AM</li>
<li>The Devine Feminine</li>
<li>Swimming</li>
</ul>
</ul>
<li>Videos</li>
<ul>
<li>Objects</li>
<li>Dang!</li>
<li>Weekend</li>
<li>Killin' Time</li>
<li>My Favorite Part</li>
<li>Best Day Ever</li>
</ul>
<li>About</li>
</ul>
</nav>
Your have a wrong structure of HTML markup.
Also, you should only handle the display of <ul> instead of <a> like this:
nav > ul ul {
display: none;
}
nav > ul > li:hover > ul,
nav > ul > li > ul > li:hover > ul {
display: block;
}
<nav>
<ul>
<li>Music
<ul>
<li>Songs
<ul>
<li>Blue Slide Park</li>
<li>What's The Use</li>
<li>Hurt Feelings</li>
<li>Fight The Feeling</li>
</ul>
</li>
<li>Albums
<ul>
<li>Blue Slide Park</li>
<li>WMWTSO</li>
<li>GO:OD AM</li>
<li>The Devine Feminine</li>
<li>Swimming</li>
</ul>
</li>
</ul>
</li>
<li>Videos
<ul>
<li>Objects</li>
<li>Dang!</li>
<li>Weekend</li>
<li>Killin' Time</li>
<li>My Favorite Part</li>
<li>Best Day Ever</li>
</ul>
</li>
<li>About</li>
</ul>
</nav>
Hope this helps you.
Related
<nav>
<ul>
<li>What is it?</li>
<li>
Inventory
<ul>
<li>
X-box 360
<ul>
<li>Building Blocks</li>
<li>Decorations</li>
<li>Redstone & Transportation</li>
<li>Materials</li>
<li>Food</li>
<li>Tools, Weapons & Armor</li>
<li>Brewing</li>
<li>Miscellaneous</li>
</ul>
</li>
<li>PC</li>
<ul>
<li>Building Blocks</li>
<li>Decoration Blocks</li>
<li>Redstone</li>
<li>Transportation</li>
<li>Miscellaneous</li>
<li>Foodstuff</li>
<li>Tools</li>
<li>Combat</li>
<li>Brewing</li>
<li>Materials</li>
</ul>
</li>
<li>Mobile</li>
<ul>
<li>Materials</li>
<li>Tools & Weapons</li>
<li>Decoration Blocks</li>
<li>Building Blocks</li>
</ul>
</li>
<li>PS4</li>
<ul>
<li>Building Blocks</li>
<li>Decorations</li>
<li>Redstone & Transportation</li>
<li>Materials</li>
<li>Food</li>
<li>Tools, Weapons & Armor</li>
<li>Brewing</li>
<li>Miscellaneous</li>
</ul>
</li>
</nav>
That's my code so far; it does a single level drop down but I tried to add a second level by doing the same thing I did to get the first drop down and it wont work. Its for a school project and I have researched how to add a second level but nothing I've tried has helped so far, so if you guys could give me any advice, or show me where I need to add thing to make it work I would love it. Thanks!
First you need fix your html code, there are several error in markup, can you check if this is the desired menu, notice in your code example that you are closing li element before open ul for each submenu, this is an error, ul need to be inside li and after ul you should close parent li.
a {
color: #FFF;
}
nav {
margin: 50px 0;
background-color: #E64A19;
}
nav ul {
padding: 0;
margin: 0;
list-style: none;
position: relative;
}
nav ul li {
display:inline-block;
background-color: #E64A19;
}
nav a {
display:block;
padding:0 10px;
color:#FFF;
font-size:20px;
line-height: 60px;
text-decoration:none;
}
nav a:hover {
background-color: #000000;
}
/* Hide Dropdowns by Default */
nav ul ul {
display: none;
position: absolute;
top: 60px; /* the height of the main nav */
}
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display:inherit;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
width:170px;
float:none;
display:list-item;
position: relative;
}
/* Second, Third and more Tiers */
nav ul ul ul li {
position: relative;
top:-60px;
left:170px;
}
/* Change this in order to change the Dropdown symbol */
li > a:after { content: ' +'; }
li > a:only-child:after { content: ''; }
<nav>
<ul>
<li>What is it?</li>
<li>
Inventory
<ul>
<li>
X-box 360
<ul>
<li>Building Blocks</li>
<li>Decorations</li>
<li>Redstone & Transportation</li>
<li>Materials</li>
<li>Food</li>
<li>Tools, Weapons & Armor</li>
<li>Brewing</li>
<li>Miscellaneous</li>
</ul>
</li>
<li>
PC
<ul>
<li>Building Blocks</li>
<li>Decoration Blocks</li>
<li>Redstone</li>
<li>Transportation</li>
<li>Miscellaneous</li>
<li>Foodstuff</li>
<li>Tools</li>
<li>Combat</li>
<li>Brewing</li>
<li>Materials</li>
</ul>
</li>
<li>
Mobile
<ul>
<li>Materials</li>
<li>Tools & Weapons</li>
<li>Decoration Blocks</li>
<li>Building Blocks</li>
</ul>
</li>
<li>
PS4
<ul>
<li>Building Blocks</li>
<li>Decorations</li>
<li>Redstone & Transportation</li>
<li>Materials</li>
<li>Food</li>
<li>Tools, Weapons & Armor</li>
<li>Brewing</li>
<li>Miscellaneous</li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
If you don't need two first items then you need extract from the first ul to individual ul.
You will need a portion of css code to format this menu.
can you view and example here https://codepen.io/andornagy/pen/xhiJH
I need to add css styles to parent list.
I have one parent ul and children. I want to apply color to fruits, vegetables and flowers but not Apple, Banana, Orange.
I want to do this using a CSS selector.
ul:root>li {
color: red;
}
<ul>
<li>Fruits
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
</li>
<li>Vegetables</li>
<li>Flowers</li>
</ul>
You could simply add a class to the parent ul and then use the direct descendant selector to target only those li items.
This is definitely going to change the colors for Apple or Orange but you can then reset the color on the sub ul items.
Here's your updated demo.
.parent-list > li {
color: red;
}
.parent-list > li ul {
color: initial;
}
<ul class="parent-list">
<li>Fruits
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
</li>
<li>Vegetables</li>
<li>Flowers</li>
</ul>
Use like this...
<ul>
<li>Fruits
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
</li>
<li>Vegetables</li>
<li>Flowers</li>
</ul>
<style>
ul li{
color: red;
}
ul li li{
color: black;
}
</style>
ul > li { /* select list items that are children of a ul */
color: red;
}
ul ul li { /* select list items that are descendants of a ul, itself... */
color: black; /* ...a descendant of another ul */
}
<ul>
<li>Fruits
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
</li>
<li>Vegetables</li>
<li>Flowers</li>
</ul>
<style>
ul li {
color: red;
}
ul ul li {
color: black;
}
</ul>
</style>
<ul>
<li>Fruits
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
</li>
<li>Vegetables</li>
<li>Flowers</li>
I have problem with the menu in my project in particular with the show him. I want when I hover the Tips for health, Recipes and inside of the last one the sub-menu of Salads, Fresh and Smoothies to show.
This is the HTML code:
`
<div id="nav">
<ul class="nav-list">
<li>Home</li>
<li>Tips for health</li>
<li>
<ul class="sub-nav-list">
<li>The healing properties</li>
<li>5 important spices that you should include in your diet</li>
<li>Bad habits and their removal</li>
<li>Water as a source of life</li>
</ul>
</li>
<li>Recipes</li>
<li>
<ul class="sub-nav-list">
<li><a href="#"Salads</a></li>
<li>Lettuce</li>
<li>French salad</li>
<li><a href="salata%20cherveno%20cveklo.html"Red Beet Salad</a></li>
</ul>
</li>
<li>
<ul class="sub-nav-list">
<li>Fresh</li>
<li>Sweet fresh</li>
<li>Fresh detox</li>
<li>Citrus fresh</li>
</ul>
</li>
<li>
<ul class="sub-nav-list">
<li>Smoothies</li>
<li>Смути шоколад</li>
<li>Chocolate smoothie</li>
<li>Smoothie with banana and pineapple</li>
</ul>
</li>
<li>Contacts</li>
</ul>
</div>
And this is the CSS code:
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #F6F2E8;
}
li a {
display: block;
color: #A6BB79;
padding: 8px 0 8px 16px;
text-decoration: none;
}
.sub-nav-list{
display: none;
}
ul.nav-list li > ul.sub-nav-list li a:hover {
color: red;
display: block;
}
Do you mean that when you hover a parent item '<li>Tips for health</li>' you want the sub navigation ul '<ul class="sub-nav-list"></ul>' to show?
If so you have a few things you need to change.
You need to rearrange your html so that the ul.sub-nav-list are within the parent li's
You need to target the ul.sub-nav-list rather than the li a's with the css to display them
For example;
<div id="nav">
<ul class="nav-list">
<li>Home</li>
<li>
Tips for health
<ul class="sub-nav-list">
<li>The healing properties</li>
<li>5 important spices that you should include in your diet</li>
<li>Bad habits and their removal</li>
<li>Water as a source of life</li>
</ul>
</li>
<li>
Recipes
<ul class="sub-nav-list">
<li><a href="#"Salads</a></li>
<li>Lettuce</li>
<li>French salad</li>
<li><a href="salata%20cherveno%20cveklo.html"Red Beet Salad</a></li>
</ul>
</li>
<li>
Fresh
<ul class="sub-nav-list">
<li>Sweet fresh</li>
<li>Fresh detox</li>
<li>Citrus fresh</li>
</ul>
</li>
<li>
<li>Smoothies<
<ul class="sub-nav-list">
<li>Смути шоколад</li>
<li>Chocolate smoothie</li>
<li>Smoothie with banana and pineapple</li>
</ul>
</li>
<li>Contacts</li>
</ul>
</div>
This now has the correct structure.
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #F6F2E8;
}
li a {
display: block;
color: #A6BB79;
padding: 8px 0 8px 16px;
text-decoration: none;
}
.sub-nav-list{
display: none;
}
ul.nav-list li:hover > ul.sub-nav-list {
color: red;
display: block;
}
This will display any ul that is a child of an li that is hovered
I have a navbar that contains a menu nested 3 times. So I have a div with id="navbar", which contains an ul, that has a li item with id="menu", that contains another ul, that contains more li items. Now I want to target every ul seperately, to add some JS code to it, make the first layer display in a row and so forth. So far I've only managed to target the first level, and the third one, but haven't been able to change properties of the second one. Somehow #navbar ul li goes to the first one, but #navbar ul li ul li targets the third. What am I doing wrong?
HTML:
<div id="navbar">
<ul>
<li>IJS</li>
<li class="ion-navicon-round" id="menu"></li>
<ul>
<li>knjižnica</li>
<li>zaloga
<ul>
<li>novi izvodi tiskanih revij</li>
<li>elektronske revije</li>
<li>katalog</li>
<li>baze podatkov</li>
</ul>
</li>
<li>storitve
<ul>
<li>medknjižnična izposoja</li>
<li>fotokopirnica</li>
</ul>
</li>
</ul>
<li>ENG</li>
</ul>
</div>
CSS:
#navbar {
background-color: #913D88;
color: #fff;
font-size: 1.5em;
}
#navbar ul li {
display: block;
margin: 0;
padding: 0;
list-style: none;
}
#navbar ul li a:link, #navbar ul li a:visited {
color: #fff;
text-decoration: none;
}
#navbar ul li ul li {
display: none;
}
Small mistake. Your second level ul are outside of li elements instead of inside.
The problem is there in the following code.
<li class="ion-navicon-round" id="menu"></li>
<ul>
<li>knjižnica</li>
<li>zaloga
<ul>
<li>novi izvodi tiskanih revij</li>
<li>elektronske revije</li>
<li>katalog</li>
<li>baze podatkov</li>
</ul>
</li>
<li>storitve
<ul>
<li>medknjižnična izposoja </li>
<li>fotokopirnica</li>
</ul>
</li>
</ul>
Should be like this.
<li class="ion-navicon-round" id="menu">
<ul>
<li>knjižnica</li>
<li>zaloga
<ul>
<li>novi izvodi tiskanih revij</li>
<li>elektronske revije</li>
<li>katalog</li>
<li>baze podatkov</li>
</ul>
</li>
<li>storitve
<ul>
<li>medknjižnična izposoja</li>
<li>fotokopirnica</li>
</ul>
</li>
</ul>
</li>
DEMO
you have an error in your syntax.
the second "ul" is not in an li, because you close the li before it.
so try
#navbar ul ul li {
background-color: #ffff00;
}
Hi I have a basic menu for which I would like to add a submenu, that appears only when a certain menu link is hovered. Everything I have tried does not hide the submenu when a link is not hovered. Here is my code:
CSS
.navmenu{
float:right;
font-size: 13px;
font-weight:400;
text-transform: uppercase;
}
.navmenu li{
position: relative;
display: inline-block;
float: left;
}
.navmenu li a{
text-decoration:none;
color:#eee;
padding:15px 37px 19px 37px;
}
.navmenu li a:hover{
background:#36332e;
}
.active a{
background:#36332e;
}
HTML
<ul class="navmenu">
<li class="active">Home</li>
<li>About Us
<ul>
<li>Sub Link 1</li>
<li>SubLink 2</li>
</ul>
</li>
<li>Testimonials</li>
<li>Services</li>
<li>Contact Us</li>
</ul>
You need to initially hide the menu:
.navmenu li ul { display: none; }
and then display it when you hover over the nav item:
.navmenu li:hover ul { display: none; }
You should also be careful about defining styles that target .navmenu li or .navmenu li a because those will also target your submenu. You should instead use child selectors, giving you more control over the non-submenu links, so your selectors will look like:
.navmenu > li
.navmenu > li > a
I've encorperated some of those changes into this JSFiddle to get you started:
http://jsfiddle.net/Wexcode/B5P26/
Edit:
This is actually going to lose it's hover state when you hover over the submenu links:
.navmenu > li > a:hover {
background:#36332e;
}
Instead, you should do this:
.navmenu ul { position: absolute; }
.navmenu > li:hover { background: #e6332e; }
.navmenu > li > a { display: block; }
Since the <ul> is nested inside the <li> element, you won't lose the hover state when you hover over the submenu links. I updated the fiddle to reflect these changes.
<ul class="navmenu">
<li class="active">Home</li>
<li>About Us
<ul>
<li>
Sub Link 1
<ul>
</li> <a href=# >hi hi hi</a>
<ul>
<li>hello hello hello</li>
<li>hello hello hello</li>
<li>hello hello hello</li>
</ul>
</li>
</li><a href=# >hi hi hi</a> </li>
</li> <a href=# >hi hi hi</a> </li>
</ul>
</li>
<li>SubLink 2</li>
</ul>
</li>
<li>Testimonials</li>
<li>Services</li>
<li>Contact Us</li>
</ul>