I have a bunch of elements with class names red
, but I can't seem to select the first element with class="red"
using the following CSS rule:
.home .red:first-child { border: 1px solid red; }
<div class="home"> <span>blah</span> <p class="red">first</p> <p class="red">second</p> <p class="red">third</p> <p class="red">fourth</p> </div>
What's wrong with this selector and how can I correct it to target the first child with class red
?
:first-child
The purpose of the selector is, as the name suggests, to select the first child tag of the parent tag. So this example works (just tried it on here):However, this method does not work if you nest the tags under different parent tags, or your
red
class tag is not the first tag under the parent tag.Also note that this not only applies to the first such tag in the entire document, but also every time there is a new parent tag surrounding it, for example:
first
andthird
will be red.For your case you can use
:nth-of-type
selector:Thanks to Martyn, who deleted the answer containing this method.
For more information about
:nth-child()
and:nth-of-type()
, please visit http://www.quirksmode.org /css/nthchild.html.Please note that this is a CSS3 selector, so some now-outdated browser versions may not function as expected (e.g. IE8 or earlier). Visit https://caniuse.com/?search=nth-of-type for more details.
This is one of the most famous examples of authors misunderstanding how
:first-child
works. Introduced in CSS2, :first-childpseudo class represents
the first child of its parent. That's it. There is a very common misconception that it selects the first child element that matches the criteria specified by the rest of the compound selector. Due to the way selectors work (see here for an explanation), this is simply not true.Selector level 3 introduces the :first-of- type
Unfortunately, there is no similarpseudo-class
, representing the first element among its siblings of element type. This answer explains the difference between :first-childand
:first-of-typewith pictures and text. However, like
:first-child, it does not look at any other conditions or properties. In HTML, element types are represented by tag names. In the question, the type is
p.
:first-of-class
While we wait forpseudo-class to match the first child element of a given class. When this answer was first posted,
the newly released FPWD selector level 4 introduced the :nth-match()pseudo-class
, which was designed around the existing selector mechanism, as As I mentioned in the first paragraph, by adding the selector list parameter you can provide the rest of the selector compound selector to get the desired filtering behavior. In recent years, this functionality has been incorporated into :nth-child( )itself
, with the selector list appearing as an optional second argument, to simplify things and avoid :nth- match()False impression of matching across the entire document (see final comment below) .
cross-browser support (seriously, it's been almost 10 years, and there's only been one implementation in the past 5 years), here's a workaround Lea Verou and I developed independently (she did it first!) by first applying the style you want to all elements of that class:
...then useto override the generic sibling combinator in the rule ~
Now only the first element with:
class="red"
will have a border.
Here are instructions on how to apply the rules: