html - CSS:包含文本节点的第一个 child 选择器

标签 html css css-selectors

有什么方法可以选择作为其父元素的第一个子元素的 CSS 元素,计算文本节点?如果标题位于其父级的顶部,我想删除标题的上边距,但如果我使用:

#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>

边距仍然被移除。 CSS 中有什么方法可以防止这种情况发生吗?

最佳答案

移除margin,而不仅仅是margin-top,h1元素将下一个元素向下推

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

Demo Fiddle

如果你想删除除第一个以外的所有内容

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

Updated Fiddle

关于html - CSS:包含文本节点的第一个 child 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22179642/

相关文章:

css - WP二十十七: Hiding the main menu and show the mobile menu always

css - 数据抓取: How to select width from a style tag by using Scrapy CSS?

css - 为什么我似乎无法针对此 asp :button?

javascript - 无法在 d3 强制布局中的节点上显示文本

javascript - 如何使用 phonegap 和 android 混淆 javascript 和 html 文件

css - polymer 如何在滚动时更改工具栏文本大小

css - HTML 显示不正确? (laravel-dompdf 包)

css - 我可以将:nth-​​child()或:nth-​​of-type()与任意选择器结合使用吗?

PHP HTML 复选框只提交最后一个值 MySQL 问题

php - 文本溢出问题