I have the following HTML structure:
<div class="content">
<img src="#">
<!-- Text content here -->
</div>
This forms the basis for a content page of a website I've made which uses a CMS to manage the content, with the ability to select an image and then a WYSIWYG editor for the text content. An example of the page's HTML that would be output by the CMS is:
<div class="content">
<img src="#">
<h1>Lorem</h1>
<p>Lorem ipsum dolor</p>
</div>
Is there a CSS selector that will select the h1 in the example HTML above? I want to be able to remove the top margin of the heading if the text content section begins with a heading, but I can't do this with a class (since the text content is output by the CMS through a WYSIWYG editor).
The :first-child selector allows you to target the first element immediately inside another element. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.
The :first-child selector is used to select the specified selector, only if it is the first child of its parent.
To select all level 1 headings we would use the h1 selector. Element selectors will apply CSS to every instance of that element in your document.
You can try this
.content > img + h1{
// css style
}
2nd option
.content > h1:first-child{
// here css
}
3rd
.content h1:first-child{
// here style
}
.content > h1:first-of-type {margin-top:0;}
that should do the trick
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With