Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css selector span:first-child issue

I have a div. Inside that div I have a table with 2 rows. In the second row I have a single td. In that td I have first a img, than a span, than another span, and finally another img. I would like to select through css the first span and give it a red color. Here below my code. Unfortunately it is not working. Hope someone can help. Thank you in advance for your replies. Cheers. Marc. By the way, the html structure might look stupid. I agree. I just simplified to ease the lecture. So no need to comment the code.

http://cssdesk.com/sVKXg

<div id="myDiv">
  <table cellspacing="0">

        <tr>
            <td>
                <span>Some text</span>
            </td>
        </tr>

        <tr>
            <td>
                <img src="img/quotes-open.png" alt="" />
                <span>span1</span>
                <span>span2</span>
                <img src="img/quotes-close.png" alt="" />
            </td>
        </tr>

  </table>   
</div>
#myDiv tr:nth-child(2) span:first-child{
  color:red;}
like image 200
Marc Avatar asked Feb 12 '12 19:02

Marc


People also ask

How do you target first span 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.

How do I not select the first child in CSS?

This selector is used to select every element which is not the first-child of its parent element. It is represented as an argument in the form of :not(first-child) element.

How do I select the first child of a parent CSS?

The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

How do I target immediate child CSS?

The CSS child combinator ( > ) can be used to select all elements that are the immediate children of a specified element. A combinator combines and explains the relationship between two or more selectors.


2 Answers

The :first-child selector only selects that element if it is the first child of the parent. The first child in this case is an image, not a span. You are looking for the :first-of-type selector.

#myDiv tr:nth-child(2) span:first-of-type { color:red }
like image 163
animuson Avatar answered Nov 16 '22 14:11

animuson


The CSS should be:

#myDiv tr:nth-child(2) span:nth-child(2){
  color:red;
}
like image 30
terenaa Avatar answered Nov 16 '22 13:11

terenaa