Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selector for first heading of a div

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).

like image 518
Jordan Avatar asked Jul 23 '13 05:07

Jordan


People also ask

How do you target the first element in CSS?

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.

How do I select the first child of a div in CSS?

The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

How do I select heading in CSS?

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.


Video Answer


2 Answers

You can try this

.content > img + h1{
 // css style
}

2nd option

.content > h1:first-child{
// here css
}

3rd

.content h1:first-child{
// here style
}
like image 129
Rohit Azad Malik Avatar answered Oct 21 '22 08:10

Rohit Azad Malik


.content > h1:first-of-type {margin-top:0;}

that should do the trick

like image 26
Jonathan Avatar answered Oct 21 '22 09:10

Jonathan