Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Selectors: Possible to select ONLY the second element?

Is this even possible?

<div class="column">
    <div> 
        <div> 
        </div>    
        <div> <!-- Definitely not this DIV! -->
        </div>    
    </div>
    <div>     <!-- THIS DIV ONLY! -->
    </div>    
</div>

The following CSS selector gets the one I've marked "Definitely not this DIV!".

.column div:last-child {
    background-color:red;
}

The following CSS selector gets the one I want AND the one I've marked "Definitely not this DIV!".

.column div + div {
    background-color:red;
}

Same goes for this one: It gets the one I want AND the one I've marked "Definitely not this DIV!".

.column div:first-child + div {
    background-color:red;
}

Question: Is this even possible to do with CSS that will be recognized by IE7? (I assume my solution will work with FF, Safari, Chrome, and X:nth-child(n) only works with IE9+) Thanks.

like image 363
Chuck Le Butt Avatar asked Jun 05 '11 17:06

Chuck Le Butt


People also ask

How do I select a second sibling in CSS?

Selecting Only the Next Sibling with Adjacent Sibling Combinator ( + ) The + character used in CSS is a combinator — it combines 2 CSS selectors. It selects the second element, only if it is just after the first element, and both elements are children of the same parent.

How do you select an element for a second child in CSS?

You have a list of elements inside a parent element and you want to select the second element only, for some reason, using only CSS. How will do that? Its simple. You can use the CSS :nth-child selector to select a particular element or the nth element, inside its parent element.

How do I select all except first child CSS?

By using the :not(:first-child) selector, you remove that problem. You can use this selector on every HTML element. Another common use case is if you use an unordered list <ul> for your menu.

How do I select a specific element in CSS?

The CSS id Selector The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.


1 Answers

This should do it.

#column > div:first-child + div

You were using a class selector to select an element with an ID.

You may want to look through the CSS Selectors, as it's the direct descendant & sibling selectors that gives you the finesse to choose a particular DOM element.

like image 147
zzzzBov Avatar answered Sep 21 '22 09:09

zzzzBov