Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"?

like image 991
omg Avatar asked Nov 22 '25 21:11

omg


2 Answers

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.

like image 152
David Thomas Avatar answered Nov 25 '25 10:11

David Thomas


I think you've meant

div.weather > strong {color:red;}

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

like image 34
Kornel Avatar answered Nov 25 '25 10:11

Kornel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!