How to select nth child with css? [duplicate]


Tags: html,css,css-selectors

Problem :

This question already has an answer here:

Given the following HTML and CSS I want to highlight the second element in the list using a nth-child pseudo class. How would I change the below to accomplish this?

Fiddle: http://jsfiddle.net/jeljeljel/DHKj5/

HTML

<div id="topdiv">
    <div class="middlediv">
        <div>A</div>
        <div>B</div>
        <div>C</div>
    </div>
</div>

CSS

.topdiv > div:nth-child(n+2) {
    font-weight:bold;
    font-color:red;
}


Solution :

Demo

#topdiv .middlediv > div:nth-child(2) {
    font-weight:bold;
    color:red;
}

You need to use # for id and . for class

Just to explain n+2 will select all the siblings following the 2nd element. Also > will select only the immediate children of the matching selector.


    CSS Howto..

    angular ngShow with animate.css

    How to display thick line under header of sub menu using css

    How to test css with media queries

    How do I inspect CSS pseudo classes with firebug?

    How to fill an HTML form with CSS?

    CSS - fluid design, how to correctly position right floats below the opposing element?

    How to style DOM element with CSS that was created with JavaScript function

    How to size a content div to cover the entire content area in jQuery Mobile

    HTML/CSS How to connect a list to h tag and word-spacing

    How to use nth-child() but not for child in CSS?

    Hide / show element if text is / isn't to long in jQuery

    How to get 3 div's side by side under an angle

    how to put links in one line [CSS]

    How to select the last element within an element regardless of type in CSS?

    How to properly use css will-change property?

    how to know about the font information in a psd file?

    twitter bootstrap: How to span links in the navbar?

    How to absolutely position element against page rather than floated parent?

    How to put a space between the hover effect in this situation css html php?

    How to design a ordered list inside a selection drop down menu

    how to animate width and height 100% using css3 animations?

    CSS: How to scale an image from the center instead of top-left

    Building a DOS-feel script. How to make the command line?

    How create an overlay button on different containing div

    How to style radio button or checkbox inside a bootstrap table?

    How to make the headline (H1 with lines on the sides) with CSS only

    How to change css file for this aim?

    How I can change the position of 3 div's to be side by side?

    How to add css class in aspx.cs page means code behind page

    Textarea doesn't show newline in IE