For example, if I want the first <div> inside a <p> element, which selector would I use?
To get all <div>s in a <p> element, I would do:
p > div
in CSS.
How could I accomplish this, only getting the first <div>?
For example, if I want the first <div> inside a <p> element, which selector would I use?
To get all <div>s in a <p> element, I would do:
p > div
in CSS.
How could I accomplish this, only getting the first <div>?
The :first-child selector allows you to target the first element immediately inside another element.
<p>
<div>First child...</div>
<div>Second...</div>
<div>Third...</div>
<div>Fourth...</div>
</p>
In CSS:
p > div:first-child {
font-size: 1.5em;
}
Edit: Important note is that you should never use div inside p. I wrote this code to show you how to select first child in your case, but it's really so bad to use div inside p. Whenever you use <div> between <p> and </p> like (for example) this:
<p> <div></div> </p>
Browser will attempt to correct (to be valid code) it to this:
<p></p> <div></div> <p></p>
Good luck!
If you are using p > div as the tags then the selector would be p > div:first-child
It finds p tag then the very first div.