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.
Related
I have the following code in my wordpress theme:
<div class="BlaBla">
<ul>
<li>
<a>stuff to select</a>
</li>
<li>
<a>stuff to NOT select</a>
</li>
<\ul>
</div>
i have to select the first "a" tag to remove it (add the rule display: none) but i can't put id's or classes
i tried the following rules but select every things
div[class="BlaBla"] ul li a:first-child{
display:none;
}
div[class="BlaBla"] ul li:first-child{
display:none;
}
div[class="BlaBla"] > ul > li:first-child{
display:none;
}
you need to target the link inside the first list-item so the selector is
.BlaBla li:first-child a {
display: none;
}
About your attempted solutions:
the first one will target all the first links inside all the list-items
the second one will target the first list-item (and not the link inside the first list-item)
the last one uses the child combinator and, in your example, works exactly as in the second attempt.
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?)
I'm trying to apply a style to ONLY the very first child of an element. I'm using :first-child in the css to achieve this, but it looks like it's not the behaviour I'm looking for.
Take the following markup as example:
<ul class="myUl">
<ul class="mySubUl">
<li>foo0</li>
<li>foo1</li>
</ul>
<ul class="mySubUl">
<li>foo2</li>
<li>foo3</li>
</ul>
</ul>
======================
<ul class="myUl">
<li>foo0</li>
<li>foo1</li>
<ul class="mySubUl">
<li>foo2</li>
<li>foo3</li>
</ul>
</ul>
and this simple CSS:
ul.myUl li:first-child {
color: red;
}
Live fiddle: http://jsfiddle.net/bsSDh/1/
This applies not only to the first child of the ul.myUl elements, but also to the first child of ul.subUl. I would have expected this behaviour if the CSS selector was ul li:first-child (which works), but since I'm adding a class to the selector I'm expecting to apply that style only to the very first child.
Why does the style applies not only to the first child? Am I missing something about first-child specs or using the selector in the wrong way?
I think you need an additional child selector element like this:
ul.myUl > li:first-child {
color: red;
}
Example Fiddle
Your selector selects any <li> below ul.myUL, that is a first child. As this references only to the immediate parent and not any other ancestor, all those other <li> match as well.
EDIT
After your comment, I assume, that you will need a somehow complexer selector like this:
ul.myUl > li:first-child,
ul.myUl > ul:first-child > li:first-child {
color: red;
}
The selector
ul.myUl li:first-child
selects any li:first-child below any ul.myUl (i.e. first child of any parent inside the ul). Instead you might want to select a direct child via
ul.myUl > li:first-child
or even
ul.myUl > ul.subUl:first-child > li:first-child
Pay attention to the differences between the Child Combinator (a direct child) and the Descendant Combinator (any element contained).
You should use this two selectors to achieve the result wanted on the two known cases:
ul.myUl > ul:first-child > li:first-child {
color: red;
}
ul.myUl > li:first-child {
color: red;
}
Running Demo
I've been reading about this everywhere, and from what I've read to select a list without selecting the nested list. I need to have this
.myclass > ul > li
//or even just
ul > li
I've been trying to get it to work unsuccessfully. The selector is selecting everything, including the nested list. What am I missing?
Please see the code on JS Bin:
http://jsbin.com/asipap/4/edit
some CSS styles are inherited from parent elements unless another style explicitly overrides it, you've set the color for all the list items, but haven't overridden it for any other matched selector. Simply adding li { color: black } should solve the issue.
You need to select the ul that are inside an ul?
.cats, .cats ul{list-style-type:none;}
.test li ul > li{color:red;} /* li ul: an ul inside a li */
This select all nested list, maybe you want to use > to limit the deep.
See it here http://jsbin.com/asipap/16/
This li element matches the .test > ul > li selector. Therefore, all text inside that li will be red, including the ul inside the li.
<li>Test 1.1
<ul>
<li>nested</li>
</ul>
</li>
<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;};