<ul>
<li> <span> apply </span> </li>
<li> <span> apply </span> </li>
<li>
<ul>
<li> <span> ignore </span> </li>
<li> <span> ignore </span> </li>
</ul>
</li>
</ul>
How can I apply a CSS rule only to span elements from the first level, and make span elements from the nested list ignore the rule?
Can this be done without specifically resetting the properties on the 2nd level spans?
Tried ul > li span but it doesn't seem to work, I get the styles applied to 2nd level too
Put your list in a wrapping div with an ID, for example <div id="ul-wrapper">, and try:
#ul-wrapper > ul > li > span {
/* my specific CSS */
}
#container > ul > li span { /* - Your CSS Here - */ }
Need to specify a container, so that only the first level ul may be selected.
As long as the parent of the outer ul isn't another li, you can use that as the starting point for your selector (example assuming it's a div):
div > ul > li span {
/* Whatever */
}
ul > li > span {border:solid 1px red;}
li > ul > li > span {border:none 1px red;};
Related
In my css file I have
.myclass > ul > li > a {
display:inline;
list-style-type:none;
}
both this style properties display and list-style-type are not working within "a" tag. However they are working if placed within "li" which does not serve the purpose because I want links which are aligned horizontally. Please advise what can be done.
<div class="myclass">
<ul>
<li>About Us </li>
<li>About Us </li>
<li>About Us </li>
</ul>
</div>
It looks like you're targeting the wrong elements here - essentially <a> has a list-style-type of none and is inline already anyway, so you're not seeing anything happen. list-style-type is something that will affect the <li> rather than the <a>:
.myclass > ul > li {
display: inline;
list-style-type: none;
}
To help make this a little clearer, you could also try adding a property that would have a visible impact on your <a> elements, like color:
.myclass > ul > li > a {
display:inline;
list-style-type:none;
color: hotpink;
}
Then you'll see that your selector was working - it just wasn't having any impact.
So i have a page like this :
<body>
<ul>
<li><span class="first">First</span></li>
<li><span class="second">Second</span></li>
<li><span class="third">Third</span></li>
<li><span class="fourth">Fourth</span></li>
</ul>
</body>
I want to change the style of the "li" tags that are only in the first and the second span.
I tried this .first,.second li{margin-left:10px;}, but it didn't work.
The comma needs to separate COMPLETE element paths:
.first li, .second li
{margin-left:10px;}
I've wasted enough time explaining why I didn't mention your improper formatting that I might as well correct you on it at this point;
<body>
<ul>
<li class="first">First</li>
<li class="second">Second</li>
<li class="third">Third</li>
<li class="fourth">Fourth</li>
</ul>
</body>
CSS:
li.first, li.second {
margin-left:10px;
}
If we're really going to travel down this rabbit hole and teach code, might as well mention this can be done entirely without classes:
ul li:nth-child(1), ul li:nth-child(2)
{
margin-left:10px;
}
The li has to follow each parent element, you can't group the parents together and then expect the child to apply to each of them. What you were asking for is for the style to apply to all li that are children of elements with class=second, and to all elements with class=first, not to all li that are children of elements with class=first
.first li, .second li { margin-left:10px }
https://jsfiddle.net/udnjqqkh/
.first li {margin-left:10px;}
.second li {margin-left:10px;}
How can I style only the top level li's from this example list?
<ul class='upper'>
<li class="first">dog</li>
<li>cat</li>
<li>bird</li>
<li>mouse</li>
<li>
<ul class="lower">
<li>chow</li>
<li>nibz</li>
<li>seed</li>
<li>cheese</li>
</ul>
</li>
ul.upper > li {
color:red;
}
This styles all li's which I understand because the recursive UL is inside a first level list item. Is there a simple way to style only the top level li's though? Maybe using ":not" in some way?
Edit: I realize you can overwrite the style below it using color:initial or by adding another color(and other ways) but I was wondering if there was a way to ONLY select the top level li's nicely so another style isn't needed.
So, your li are inheriting color from their ancestors, so you need to add color:initial, or color:black to override that
ul.upper > li {
color: red;
}
li {
color: initial;
}
<ul class='upper'>
<li class="first">dog</li>
<li>cat</li>
<li>bird</li>
<li>mouse</li>
<li>
<ul class="lower">
<li>chow</li>
<li>nibz</li>
<li>seed</li>
<li>cheese</li>
</ul>
</li>
You want the child combinator, ">"
.upper > li
You can define the deeply nested UL's list-items like this:
ul > li {
color:red;
}
ul ul > li {
color: #000;
}
So this can work throughout your page to identify any top-level list-items versus second-level list-items, regardless of class name. "ul ul" in CSS means "ul that is inside another ul"
Working example: https://jsfiddle.net/2Lyvp2bm/2
(I'm new, how do I add a code snippet to my answer?)
Please consider the following HTML :
<div class="mytest">
<ul>
<li>Parent 1</li>
<li>Parent 2
<ul>
<li>Child 1</li>
<li>Child 2</li>
<li>Child 3</li>
</ul>
</li>
<li>Parent 3</li>
</ul>
</div>
I want to put a border around the "parent" li only, and I was convinced this CSS would do the trick :
.mytest > ul li {border : 1px solid #000;}
I read this selctor as : "select li which are children of a ul which is a direct children of a element with class mytest". But if I use that, the "child" li's also get the border. cfr this fiddle. Can someone explain why please ? I see no reason and I'm probably missing something important here cause I'm sure I already used this selector sucessfully for the same purpose before...
You must add direct child selector between <ul> and <li> like so:
.mytest > ul > li {border : 1px solid #000;}
With your code, you targeted all <li> in <ul> elements directly child of .mytest
Try
.mytest > ul > li {border : 1px solid #000;}
Try to add > before li like this Demo
.mytest > ul > li {border : 1px solid #000;}
Then only it will be considered as direct children of ul and the style will be applied only for that li. Hope this helps!
Your selector, as it currently stands, selects the lists that are immediate children of your parent div and all descendant items of that list.
To have it select only those list items that are direct descendants if the list, you must add another child selector, like so:
.mytest>ul>li
Direct child selector selects the immediate child that follows the parent so your code would go like this -
.mytest is the parent and its direct child is ul and ul direct children are li
.mytext > ul > li {border : 1px solid #000;}
is the correct format, also if you want to add only to the first direct child you could do like
.mytest > ul > li:first-child {border : 1px solid #000;}
I'm trying to adress the descendant/child of the first child in a list. For example:
<ul>
<li>
<a>This "a" of the first list item should have a black background</a>
</li>
<li>
<a></a>
</li>
<li>
<a></a>
</li>
</ul>
Using first child like "ul li:first-child" will only affect the li but not the a descendant. I'm trying to format the child(s) of the first child.
Perhaps even "ul + li a" or something, but apparently that isn't woking either.
ul li:first-child a {
background-color:black;
}
http://jsfiddle.net/UbuAx/
I believe you want:
ul li:first-child > * {
This will match all direct children of the first <li>.
If they're always links, you could subsitute the * for a.
All anchors under the first list item:
ul li:first-child a { … }
Or for any descendants:
ul li:first-child * { … }
Or for only children (not grand children, etc. descendants):
ul li:first-child > * { … }
You specifically said:
/* a list's */
ul
/* first child's */
> li:first-child
/* descendant or child */
*
or
ul > li:first-child *
Strangely, none of the other answers give you exactly what you requested, though I agree about the * global selector being non-optimal. If you can specify, it might be nice.