css - HTML 中的 hr 中断 div :first-child?

标签 css html css-selectors

我有一个 div,里面有 2 个 float 的 div。在它们上方有一个 HR 标签。我正在尝试使用第一个 child ,因为 div 之间应该有一个间距。但是,这不起作用。该规则永远不会被应用。好像 hr 标签打破了它;如果我删除 hr 它工作正常。另外 :last-child 工作正常,有或没有 hr。有人对此有一些合乎逻辑的解释吗?

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style>
        #outer {
            width: 600px;
        }
        #outer > div {
            float: left;
            width: 290px;
            background-color: #1982c8;
        }
        #outer > div:first-child {
            margin-right: 20px;
        }
    </style>
</head>
<body>
    <div id="outer">
        <hr>
        <div>
            First inner.
        </div>
        <div>
            Second inner.
        </div>
    </div>
</body>
</html>

Fiddle

最佳答案

<hr>元素确实是其父元素的第一个子元素,而不是 <div> .因此,:first-child在这里不合适。

如果您的目标浏览器支持 CSS3,您可以使用 :first-of-type相反:

#outer > div:first-of-type {
    margin-right: 20px;
}

关于css - HTML 中的 hr 中断 div :first-child?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21193125/

相关文章:

css - 如果标签元素中有 anchor 元素,则复选框不起作用

html - IE 中具有最大宽度的 SVG 问题

html - 使用 docraptor : How to flow header logo on every page and fix footer on the first page of pdf. 打印 css 媒体

javascript - 存储 JSON : Javascript vs HTML

javascript - 跨度与位置 :absolute won't create scrollbar on container overflow

python - 如何根据其在 Selenium 中以什么结尾的元素来查找元素?

html - 仅当特定 "li"的子项时,CSS 才会影响 "ul"

php - 自定义字体不起作用。Codeigniter

html - 如何创建像 Apple 一样的导航栏?

javascript - 我可以在 Protractor 中使用 '>' css 选择器吗?