:first-child not working as expected
Solution 1:
The h1:first-child
selector means
Select the first child of its parent
if and only if it's anh1
element.
The :first-child
of the container here is the ul
, and as such cannot satisfy h1:first-child
.
There is CSS3's :first-of-type
for your case:
.detail_container h1:first-of-type
{
color: blue;
}
But with browser compatibility woes and whatnot, you're better off giving the first h1
a class, then targeting that class:
.detail_container h1.first
{
color: blue;
}
Solution 2:
:first-child
selects the first h1
if and only if it is the first child of its parent element. In your example, the ul
is the first child of the div
.
The name of the pseudo-class is somewhat misleading, but it's explained pretty clearly here in the spec.
jQuery's :first
selector gives you what you're looking for. You can do this:
$('.detail_container h1:first').css("color", "blue");
Solution 3:
For that particular case you can use:
.detail_container > ul + h1{
color: blue;
}
But if you need that same selector on many cases, you should have a class for those, like BoltClock said.
Solution 4:
you can also use
.detail_container h1:nth-of-type(1)
By changing the number 1 by any other number you can select any other h1 item.
Solution 5:
You could wrap your h1
tags in another div
and then the first one would be the first-child
. That div
doesn't even need styles. It's just a way to segregate those children.
<div class="h1-holder">
<h1>Title 1</h1>
<h1>Title 2</h1>
</div>