CSS:包含文本节点的第一子选择器 [英] CSS: first-child selector including text nodes

查看:408
本文介绍了CSS:包含文本节点的第一子选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法选择一个CSS元素,它是其父的第一个子元素,计数文本节点?我想删除一个标题的顶部边距,如果它在其父的顶部,但如果我使用:

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;
}

并且有一些HTML

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

余额仍被移除。

推荐答案

删除 margin ,而不只是 margin-top ,h1元素正在推动下一个元素

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;
}

这篇关于CSS:包含文本节点的第一子选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆