Target list items in primary ul but not nested ul - html

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>

Related

Make nested list element show on mouseover

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.

Selecting nested lists

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;
}

Add submenu to existing menu

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>

Apply CSS class to li not child li

I have a class specified for a list:
.nav ul li.current {
background: #fff;
}
The problem is, I only want to style the parent li element, not any children.
<div class="nav">
<ul>
<li>Something</li>
<li class="current">Something else</li> <!-- apply class here -->
<li>
<ul>
<li>Child something</li>
<li class="current">Child something else</li> <!-- ...but not here -->
</ul>
</li>
</ul>
</div>
The CSS pasted above is changing the background for both li elements with the class "current". How do I correct this?
.nav > ul > li.current { ... }
.nav > ul > li.current {
...
}

CSS Direct Decedent Li

In css how would I change on hover the color of test 1 but not color of list 1, 2,3?
<ul>
<li>
test 1
<ul>
<li> List 1</li>
<li> List 2</li>
<li> List 3</li>
</ul>
</li>
</ul>
One way is to specify the "default" color:
li:hover {
color:#f00;
}
li, li:hover li {
color:#000;
}​
http://jsfiddle.net/D8dwt/1/
Another (cheat?) is to use more markup to wrap the content you want styled on hover:
li:hover span {
color:#f00;
}​
<ul>
<li>
<span>test 1</span>
<ul>
<li> List 1</li>
<li> List 2</li>
<li> List 3</li>
</ul>
</li>
</ul>​
This is one way to go:
ul > li {
color: red;
}
ul > li:hover {
color: blue;
}
ul > li:hover > ul > li {
color: red;
}
Add test1 into a div element so that it is in a separate leaf.
css:
div:hover {
color: blue;
}
Although there may be a way to do this without modifiying the html..
Give it it's own class and define it in your CSS file.
<li class="yourclass">
Or put it in tags and define the link in your CSS
li.yourclass a:hover {
text-decoration: underline ;
}