Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: first-child selector including text nodes

is there any way to select a CSS-element that is the first child of its parent, counting text nodes? I want to remove the top margin of a heading if it is at the top of its parent, but if I use:

#content h1 {
    margin-top: 1em;
}
#content h1:first-child {
    margin-top: 0;
}

and have some HTML like

<div id="content">
    This is some text.
    <h1>A heading</h1>
    Some more text.
</div>

the margin is still removed. Is there any way in CSS to prevent this?

like image 466
BlackWolf Avatar asked Mar 04 '14 18:03

BlackWolf


1 Answers

Remove the margin, not just the margin-top, h1 element is pushing the next element down

#content h1 {
    margin-top: 1em;
}
#content h1:first-child {
    margin: 0px;
}

Demo Fiddle

If you want to remove all except first

#content h1:not(:first-child) {
    margin: 0px;
}

Updated Fiddle

like image 144
Vinay Pratap Singh Bhadauria Avatar answered Sep 28 '22 09:09

Vinay Pratap Singh Bhadauria