浮动元素后忽略元素边距

发布于 2024-08-04 17:48:51 字数 623 浏览 4 评论 0原文

我有一个带有顶部和底部边距的“已清除”


标记。但是,在两个浮动元素作为列之后,上边距将被忽略,并且该行位于文本的正下方。

CSS:

hr {
    width: 100%;
    height: 1px;
    border: 1px;
    background-color: #d3d7cf;
    color: #d3d7cf;
    clear: both;
    margin: 16px auto;
}
.column {
    text-align: center;
    float: left;
    margin-right: 16px;
    width: 200px
}

HTML:

<hr/>
<div class="column">asd<br />fgh</div>
<div class="column">asd</div>
<hr/>

我发现我可以使用 overflow:hidden 将列包装在 div 中,但是有没有一种解决方案可以让我不需要向 HTML 添加额外的标记?

I have a "cleared" <hr> tag with margins top and bottom. However, after two floated elements as columns, the top margin is ignored and the line sits right below the text.

CSS:

hr {
    width: 100%;
    height: 1px;
    border: 1px;
    background-color: #d3d7cf;
    color: #d3d7cf;
    clear: both;
    margin: 16px auto;
}
.column {
    text-align: center;
    float: left;
    margin-right: 16px;
    width: 200px
}

HTML:

<hr/>
<div class="column">asd<br />fgh</div>
<div class="column">asd</div>
<hr/>

I found out that I can wrap the columns in a div with overflow: hidden, but is there a solution where I don't need to add extra markup to the HTML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不在是我 2024-08-11 17:48:51

将 margin-bottom: 16px 添加到“column”类中。

或者向列添加填充(如果您在列类上使用边框,则显然不是)。

Add margin-bottom: 16px to the "column" class.

Or add padding to the columns (obviously not if you're using borders on the column class).

倾城月光淡如水﹏ 2024-08-11 17:48:51

float:left 添加到 HR 样式。

现在,这应该使 HR 以与浮动 DIV 相同的流程播放,并应用您在 CSS 第一个版本中缺少的上边距。

Add float:left to the HR style.

This should now make the HR play in the same flow as the floated DIVs and apply the top margin that you were missing in the first version of your CSS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文