简体   繁体   English

CSS浮动和块元素

[英]CSS floats and block elements

I have an annoying CSS layout problem. 我有一个讨厌的CSS布局问题。 I'm trying to float images on a particular page: 我正在尝试在特定页面上浮动图像:

img {
  float: left;
}

I think make sure my headings don't start indented with: 我认为确保我的标题不会缩进:

h3 {
  clear: left;
}

It all works fine except for some of the images that have lists (or any block element) floating past them (or not as the case is). 除了一些列表(或任何块元素)漂浮在它们之外的图像(或者不是这样)之外,一切正常。 The reason for this is actually clear in the CSS spec: block elements don't flow. 其原因实际上在CSS规范中是明确的:块元素不会流动。 Line/inline elements do. 行/内联元素。

This is a real problem for lists however. 然而,这是列表的真正问题。 Is there any way around it in a fairly generic and compatible way? 有一种相当通用和兼容的方式吗?

Here's what I always do to make sure the float is always cleared: 这是我一直在做的事情,以确保浮动始终被清除:

  1. Add the following to the CSS: 将以下内容添加到CSS:

     .clearfix:after { content: "."; display: block; clear: both; visibility: hidden; line-height: 0; height: 0; } .clearfix { display: inline-block; } html[xmlns] .clearfix { display: block; } `*` html .clearfix { height: 1%; } 

    You can also find this code here . 您也可以在此处找到此代码。

  2. Mark every parent of the element that's floated with class clearfix . 标记使用类clearfix浮动的元素的每个父级。

It does sound like the problem is with clearing floats. 它确实听起来像清除花车的问题。 But as a comment said, a screenshot would be nice. 但正如评论所说,截图会很好。 I personally clear my floats by setting the overflow property: http://www.quirksmode.org/css/clearing.html 我个人通过设置溢出属性清除我的浮动: http//www.quirksmode.org/css/clearing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM