Html CSS:如何将边距应用于带有浮动的容器?
这是我的老问题,我从来没有找到一个真正的解决办法,所以我最终只是寻求帮助 HTMLHtml CSS:如何将边距应用于带有浮动的容器?,html,css,css-float,Html,Css,Css Float,这是我的老问题,我从来没有找到一个真正的解决办法,所以我最终只是寻求帮助 HTML <section class="cols-2"> <div class="wrapper"> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquya
<section class="cols-2">
<div class="wrapper">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos
</div>
<div class="wrapper">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos
</div>
</section>
<section …
因此,我在部分中有两列浮动。cols-2
。在这种情况下,部分没有自动高度
e、 g.如果我想在这一节之后紧接着再加一节
,我不能在这两节之间加一个边距
我不想应用一个高度
值,我只需要两个部分
以特定的间距彼此分开
参见我的示例:
谢谢你的提示和技巧,我很感激。你需要清除浮动。这不仅是你的老问题,也是每个网站建设者的问题。但幸运的是,由来已久的问题有着由来已久的解决方案 其中最常见的是在容器上使用
溢出:hidden
。其中一个问题是清除浮动
.cols-2 { overflow: hidden; }
当然,您可以简单地使用,容器之间会有空间,但最后一个容器的高度仍然不会为非零
.cols-2 { clear: left; }
一些清除技术通过插入另一个应用了
clear:left
的元素(如空div或伪元素)来解决此问题。.cols-2{overflow:auto;}
将解决由浮动元素引起的问题。请检查一下,这可能会对您有所帮助
.cols-2 {
margin:20px 0;
overflow:auto;
}
最好的解决方案是对父容器使用“clearfix”
.clearfix:before, .clearfix:after { content: ""; display: table; }
.clearfix:after { clear: both; }
.clearfix { zoom: 1; }
<section class="cols-2 clearfix">
<div class="wrapper">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos
</div>
<div class="wrapper">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos
</div>
</section>
.clearfix:before,.clearfix:before{content:;display:table;}
.clearfix:在{clear:两者;}之后
.clearfix{zoom:1;}
他说:“我的工作是在工作中完成的,我的工作是在工作中完成的,我的工作是在工作中完成的。”。在vero eos
他说:“我的工作是在工作中完成的,我的工作是在工作中完成的,我的工作是在工作中完成的。”。在vero eos
您可以对任何包含浮动元素的容器使用clearfix。您需要将clearfix css放在css文件的最末尾
只需添加一个类,您的任务就完成了 您是否想过使用:
-webkit-column-count: 2;
-webkit-column-rule: 1px solid black;
-webkit-column-gap: 20px;
当然,-webkit-
前缀必须替换为其他前缀,以便更大的兼容性
如果您期望的行为允许此
对于edding另一个元素,它是mostlikley with.clear{clear:tware;}@DennisHunink是的,“好”的旧时光。这种技术也有上千种组合,我认为这是“最佳解决方案”。而且,这在IE7和更低版本中不起作用。它适用于所有浏览器。这里看一看-你的代码可能没有工作,因为HTML有'section'标记,这是旧版本的IEM不支持的。我的错误是,我没有发现
zoom:1代码>部分(你应该在找到这段代码的地方贴上链接,以示表扬)。这是Paul Irish提供的帮助,附带他的HTML5样板文件——如果有的话。为什么您决定只显示-webkit-
版本?Opera和IE10支持无前缀版本,还有-moz-
。请设法把它修好。
-webkit-column-count: 2;
-webkit-column-rule: 1px solid black;
-webkit-column-gap: 20px;