Css Select First Child If It's A Certain Tag
I need to select a particular element only if it occurs as the first child of a div. Is there a CSS selector that'll handle that case? For example, I want to select this figure: &l
Solution 1:
You could use CSS :first-child
selector with descendant selector like this:
JSFiddle - DEMO
divfigure:first-child {
color:red;
}
OR: with CSS >
child selector (as suggested by @Alohci)
div > figure:first-child {
color:red;
}
Solution 2:
I don't see any issue with figure:first-child
selector. It would select the <figure>
element only if it is the first child of its parent.
While :first-child
represents any element which is the first child in the children tree of the parent, the figure
part would limit the selector to match an element only if it is a <figure>
.
Solution 3:
have you tried the following?
divfigure {
color: green;
}
divfigure:first-child {
color: blue;
}
Solution 4:
figure:first-child
will select all the figures that are first child of a parent.
Check this example at W3C.
Solution 5:
Use div figure:first-child
selector.
Here is example
<div>
<figure>test</figure>
<p>div1 pgraph1</p>
<p>div1 pgraph1</p>
</div>
<div>
<p>div2 pgraph1</p>
<figure>test 2</figure>
<p>div2 pgraph1</p>
</div>
CSS:
divfigure:first-child{
border:1px solid red;
}
It will apply red border only to first child.
Please refer to fiddle for demo
Post a Comment for "Css Select First Child If It's A Certain Tag"