views:

733

answers:

6
+1  Q: 

css pseudo class

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title>How to Write a Good Report</title>
<style type="text/css">
div.weather strong:first-child {color:red;}
</style>
</head>

<body>

<div class="weather">
It's <strong>very</strong> hot and <strong>incredibly</strong> humid.
</div>

</body> </html>

Why only "very" is in color red while "incredibly" not since both of them are first child of the element specified by "div.weather strong"?

A: 

div.weather strong:first-child says to select the first strong element that is a child of weather, not all of the child elements of weather. It's only going to match a single element ever.

div.weather strong matches all of the strong elements that are descendants of weather. div.weather > strong matches all of the strong elements that are direct children of weather. div.weather * strong matches all of the strong elements that are grandchildren or later of weahter.

geofflane
+3  A: 

Because the psuedo-selector doesn't do what you think it does.

It selects, in your stated example, the first child of the element .weather that is a <strong> element.

So only the first instance matches. I can't back this up with reference to the spec, since I'm lazy right now (busy day...), but I have this sort of vision in my head:

<html>

<div class="weather">

<p><strong>Lorem</strong> ipsum <strong>sit</strong> <em>amet</em> <span><a...>dolor</a><em>yadda yadda yadda</em></span> <a>something</a>, I'm not good at coming up with random text. Sorry...</p>

</html>

                                    Weather
                                       |
+----------------+---------------------+---------------+-------------------+.....
|                |                     |               |                   |
(first-child)  (second-child)      (third-child)      (fourth-child)   (fifth-child)
strong         strong                em                span                a
                                                        |
                                                +-------+--------+
                                                |                |
                                            first-child     second-child
                                                a                em

It's not the prettiest ascii example, I know, but it's how I think of it.

David Thomas
Whether your answer is right or not (too lazy to read it...), the fact that you used an ascii art chart to describe your answer... you totally get a +1. Haha.
KyleFarris
@Kyle, thanks, man...I've never had rep from, essentially, a `tl;dr` comment before =)
David Thomas
A: 

first-child means "an element that is the first child", not "the first child of this element."

So your example means "the strong that is the first child of div.weather".

If you want to make, say, the first word in all the <strong>s within the div come out red, you'll need to add some markup and do something like this:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title>How to Write a Good Report</title>
<style type="text/css">
div.weather strong span:first-child {color:red;}
</style>
</head>

<body>

<div class="weather">
It's <strong><span>very</span> <span>very</span></strong> hot
and <strong><span>really</span> <span>incredibly</span></strong> humid.
</div>

</body> </html>
RichieHindle
what if I want to select "the first child of this element."?
Shore
Is it selectable without add extra tags like span?
Shore
@Shore, do you mean 'is it possible to select all `strong` elements that are children of `.weather`?' If so, yes: `.weather strong` should select all `<strong>` elements contained within `.weather`. If you want to select only those that are children of only `.weather`, (not in any nested spans or whatever) then `.weather > strong` should work.
David Thomas
@Shore: It depends what it is you want to select. You can't select the first word of the <strong> without wrapping it up in an HTML element like <span>.
RichieHindle
A: 

I think your understand of "first-child" is wrong. The first child is the the first element only. See the example at W3Schools.

Ben S
A: 

Both strong elements are a child of the div but only the first one is the first strong child. So you just misunderstood the selector.

Gumbo
+3  A: 

I think you've meant

div.weather > strong {color:red;}

That will select children only (first level of nesting) rather than all descendants.

porneL