Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select first 2 <li> elements using css

Tags:

css

Hi i want to apply css for first 2 elements (one,two) that are adjacent to first <p> element.

<div class="one">
    <ul>
        <p>
            <li>One</li>
            <li>Two</li>
        <p>
        <li>Four</li>
        <li>Five</li>
    </ul>
</div>

Following getting applied to all 4 li elements

.one ul p:nth-child(odd) ~ li {
    background: lightsteelblue;
}
like image 618
rajesh Avatar asked Sep 07 '13 11:09

rajesh


People also ask

How do I select a particular Li in CSS?

To specify the style for a list, use the list-style property to specify the type of marker. The selector in your CSS rule can either select the list item elements li , or it can select the parent list element ul so that its list elements inherit the style.

How do I select first and second child in CSS?

The CSS child selector has two selectors separated by a > symbol. The first selector indicates the parent element. The second selector indicates the child element CSS will style.

How do you select multiple elements in CSS?

When you group CSS selectors, you apply the same styles to several different elements without repeating the styles in your stylesheet. Instead of having two, three, or more CSS rules that do the same thing (set the color of something to red, for example), you use a single CSS rule that accomplishes the same thing.

How do I select the first sibling in CSS?

It is possible to target first sibling with CSS, with some limitations. This works, but requires that you can explicitly set the styles on the siblings to override the setting for first child. But if your html is dynamic and sometimes you'll only have a single li. heading, it'll be hidden.


4 Answers

For first 2 li elements inside ul p try:

.one ul li:nth-child(-n+3){
    // your style
}

See on jsfiddle

And as other mates mentioned: you have invalid markup.

If you removed p element, try:

.one ul li:nth-child(-n+2){
    // your style
}

See on jsfiddle

Update: My suggestion is to use another ul instead of p: So you have valid markup and same result:

HTML

<div class="one">
    <ul>
        <li>0</li>
        <li>
            <ul>
                <li>One</li>
                <li>Two</li>
                <li>3</li>
            </ul>
        <li>
        <li>Four</li>
        <li>Five</li>
    </ul>
</div>

CSS:

.one ul {
    padding: 0;
    list-style-type: none;
}
.one ul ul li:nth-child(-n+2){
    // your style
}

Updated jsfiddle

Note: As your last comment, If you have only 2 special li element, Why not define a class name simply... <li class="bold"> Do it simple

ul li.bold {
    // your style
}
like image 191
Vahid Hallaji Avatar answered Oct 19 '22 00:10

Vahid Hallaji


This should be compatible with IE8 (uses CSS 2.1 selectors only):

li:first-child, li:first-child+li {
    color: blue;
}
like image 41
Nathan J.B. Avatar answered Oct 19 '22 00:10

Nathan J.B.


First three elements you can select with e.g.

ul li:nth-of-type(-n+3) {

}

But like others mentioned, your markup is invalid and you should definitely correct it.

like image 15
mkas Avatar answered Oct 19 '22 01:10

mkas


Like others already mentioned, the straightforward answer would be li:nth-child(-n+2) { ... }.

I don't know about you, but when I first learn this -n+2 part, my reaction was "what? is this a typo?". So, for those who like to know a little explanation rather than just copy-and-paste the counter-intuitive magic, here I include a link to a wonderful blog post by Sara Cope explaining the -n+2 part:

The syntax for selecting the first n number of elements is a bit counter-intuitive. You start with -n, plus the positive number of elements you want to select. For example, li:nth-child(-n+2) will select the first 2 li elements.

Such explanation is even missing in the w3cshool for nth-child.

like image 11
RayLuo Avatar answered Oct 19 '22 02:10

RayLuo