Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying CSS styling to only the first <li> in the root of a nested list

Tags:

html

css

i want to apply the CSS only first li but :first-child apply to all first child of every ul

here is my CODE

#menu-navigation li:first-child{
   color:red;
}​

When applied to this HTML:

<ul class="nav" id="menu-navigation">
    <li>Home</li>
    <li>About Us
        <ul>
            <li>Our Team</li>
        </ul>
    </li>
</ul>​

...both "Home" and "Our Team" turn red.

like image 205
Jassi Oberoi Avatar asked May 04 '12 15:05

Jassi Oberoi


People also ask

How do you style the first element in CSS?

The :first-of-type selector in CSS allows you to target the first occurence of an element within its container. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.

Which CSS pseudo class applies to the first element of a specified type?

The :first-of-type CSS pseudo-class represents the first element of its type among a group of sibling elements.

How do I select the first paragraph in CSS?

CSS ::first-line Selector The ::first-line selector is used to add a style to the first line of the specified selector. Note: The following properties can be used with ::first-line: font properties. color properties.

How do I select all except first in CSS?

The trick is very easy, in CSS we have the sibling selector ("+"), if i will make selector that choose "li + li" it will select all list-items except the first . That's all!


1 Answers

use the child selector:

#menu-navigation>li:first-child{
   color:red;
}​

For example: http://jsfiddle.net/w47LD/3/

like image 131
BiAiB Avatar answered Nov 13 '22 05:11

BiAiB