Is it possible to give a particular styling to a child element only if say odd number of child elements are present? Let me elaborate.
I have a
<ul>
tag which gets dynamically populated with data as
<li>
child nodes. I want to apply a styling to the last element, say if only 3 child nodes are present or there are odd numbers. My present requirement is only for three child nodes. I know it is possible to do it easily with JavaScript, but I need a pure CSS solution.
You can combine :nth-last-child() and :nth-child()
li:nth-last-child(1):nth-child(odd) {
background-color: red;
}
li:nth-last-child(1):nth-child(odd) {
background-color: red;
}
<ul>
<li>x</li>
<li>x</li>
<li>x</li>
</ul>
vs
li:nth-last-child(1):nth-child(odd) {
background-color: red;
}
<ul>
<li>x</li>
<li>x</li>
<li>x</li>
<li>x</li>
</ul>
You can achieve this by combining the :last-child pseudo-class with the :nth-child selector.
To select the last element only when a specific number of elements are present, use that number for :nth-child:
*{color:#fff;font-family:sans-serif;font-size:14px;list-style:none;margin:0;padding:0;}
li{
background:#000;
height:20px;
line-height:20px;
margin:2px;
padding:0 2px;
}
li:last-child:nth-child(3){
background:#f00;
}
hr{margin:10px 2px;}
<ul><li></li><li></li><li>I'm red</li></ul>
<hr>
<ul><li></li><li></li><li></li><li>I'm not</li></ul>
To select the last element only when an odd number of elements are present, use odd for :nth-child:
*{color:#fff;font-family:sans-serif;font-size:14px;list-style:none;margin:0;padding:0;}
li{
background:#000;
height:20px;
line-height:20px;
margin:2px;
padding:0 2px;
}
li:last-child:nth-child(odd){
background:#f00;
}
hr{margin:10px 2px;}
<ul><li></li><li></li><li>I'm red</li></ul>
<hr>
<ul><li></li><li></li><li></li><li>I'm not</li></ul>
Related
<div id="quicklinks">
<div class="sidenav-header">
<h3>Quick Links</h3>
</div>
Link One
Link Two
</div>
In the above code I have two links. It seems to address the style of the second link in this code block I am having to target the 3rd element. My CSS is addressing the <a> tag though
#quicklinks {
height:120px;
}
#quicklinks a {
display:block;
color:#fff;
text-align:left;
background:#92d050;
margin:6px 12px;
padding:10px 12px;
text-decoration:none;
border-radius:3px;
font-weight:normal;
}
#quicklinks a:nth-child(3) {
background:#ff9900;
}
Why is my nth-child set to #3 to effect the 2nd a element?
nth-child(3) is selecting the third child element of any type, including your <div class="sidenav-header"> element. You should use a:nth-of-type(2) to select the 2nd child element of type a
Have simple document with two unordered lists. The two lists are separate from each other in that one is not nested inside of the other.
See working example here: JSfiddle
A class is being applied to the First List, but not the Second List. I'm finding that the class is being applied to all lists on the page, even when the other lists do not share the same class.
Markup:
<style>
#listContainer
{
margin-top:15px;
}
. .expList ul, li
{
list-style: none;
margin:0;
padding:0;
cursor: pointer;
}
.expList li {
line-height:140%;
text-indent:0px;
background-position: 1px 8px;
padding-left: 20px;
background-repeat: no-repeat;
}
.expList { clear:both;}
</style>
<p style='font-size:1.4em;'>First list</p>
<div id='listContainer'>
<ul class='expList'>
<li>A<ul><li>A1</li></ul></li>
<li>A</li>
</ul>
</div>
<hr>
<p style='font-size:1.4em;'>Second list. </p>
<ul>
<li><b>Cats</b>
<ul>
<li>Cheezburger</li>
<li>Ceiling</li>
<li>Grumpy</li>
</ul>
</li>
<li><b>Role Models</b>
<ul>
<li>Bad Luck Brian</li>
<li>Paranoid Parrot</li>
<li>Socially Awkward Penguin</li>
</ul>
</li>
</ul>
Why would applying a class to a completely separate UL affect a different, non-nested UL on the same page?
EDIT - see the accepted answer to this question for a very good explanation of this problem.
You're applying the style to all li elements:
.expList ul, li
...means "ul elements in element with class expList, and all li elements".
Since it is the ul that has class expList, I'm wondering if you actually want:
.expList li { ... }
Meaning all li elements in ul.expList. Guessing though, hard to say without more info.
I have a structure:
<div id="div">
<ul class="ul">
<li class="li_one">
</li>
<li class="li_two">
</li>
</ul>
</div>
I want to set background:red to the second li element (class "li_two") using pseudo-selectors and want to begin from the most outer div. I'm trying to this way:
#div > ul:nth-child(1) { background:red; } // works but wrong, sets background to ul
#div ul:last-child { background:red; } // doesn't set to any element
#div ul:first-child { background:red; } // again sets to ul but not to li
#div [class=li_two] { background:red; } // only this one works fine
Is it possible to set style to li_two from #div using :nth-child or :last-child or :first-child selectors? How to do it?
#div li:last-child
Your 2nd option was almost right :) I think you misunderstood what last-child does. xx:last-child It doesn't select the last child element of element xx; it selects every xx element that is the last child of it's parent.
Some reading.
I've created a JSFiddle for you to test it
:nth-child() and the other pseudo-classes should be applied to the child elements, not the parent. Apply those pseudo-classes to the lis:
#div ul li:last-child {
background: red;
}
I have the following css which needs to alter the last-child which does not have the class "myClass" but I can't seem to get it to work.
Any ideas where I'm going wrong?
ul li:not(.myClass):last-child a {
font-style:italic;
}
Example html as requested:
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
<li class="myClass">Extra</li>
</ul>
I want to apply the css to li three...
this can't be done with css only if you are capable to use jQuery you might find this solution helpful.
http://jsfiddle.net/6ku3Y/
$('ul li').not('.myClass').last().find('a').addClass('mystyle');
If you're sure the element you want to target is the last but one, you can use nth-last-child(2)
ul li:nth-last-child(2) a {
border-right:0px solid #000;
}
If you want to apply the css only to li three, Try :nth-child(3)
ul li:nth-child(3) a {
border-right:0
}
http://jsfiddle.net/hhncn/
This pseudo-class matches elements on the basis of their positions
within a parent element’s list of child elements.
– http://reference.sitepoint.com/css/pseudoclass-nthchild
Current CSS syntax is not capable of facilating an AND operator to perform this kind of style. What you need is the CSS LESS Framework.
This will allow you to do this:
ul > li:not(.myClass) {
li:last-child {
//style here
}
}
Try something like this:
ul li:not(.myClass):nth-last-of-type(2) a {
border-right:0px solid #000;
}
SEE DEMO
I would like to add a border radius to a list of items but I don't want each item to have the style applied to it. Right now my style has the odd list elements with one color and the even elements a darker color. When I apply the border-radius to the li it is visible for each row but I only want the first item and the last item to have this be applied to. How do I make this happen without making a special id or class for only those two list items?
Here is my HTML and CSS:
<section id="list">
<ul>
<li>Song 1</li>
<li>Song 2</li>
<li>Song 3</li>
</ul>
</section>
ul{
list-style:none;
padding-left:0px;
width:600px;
}
ul li:nth-child(odd){
background: rgba(12,147,0,0.4);
}
ul li:nth-child(even){
background: rgba(12,147,0,0.7);
}
li{
padding:15px;
border-radius: 20px;
}
use :first-child and :last-child
li:first-child, li:last-child{
padding:15px;
border-radius: 20px;
}