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;}
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.
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.
The :first-child selector is used to select the specified selector, only if it is the first child of its parent.
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.
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 }
The CSS should be:
#myDiv tr:nth-child(2) span:nth-child(2){
color:red;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With