作为学习CSS的一部分(&实际应用它 - 通过创建简单主题),今天我想知道在CSS中清除浮动的some proper ways。Twitter的问题Bootstrap clear float CSS - 我做错了什么?
我想看到的Twitter是怎么做的,所以我下载引导,通过bootstrap.css文件去了,找到了我一直在寻找(我发现了两个代码块):
.clearfix {
*zoom: 1;
}
.clearfix:before, .clearfix:after {
display: table;
content: "";
}
.clearfix:after {
clear: both;
}
&
.container {
margin-left: auto;
margin-right: auto;
*zoom: 1;
}
.container:before, .container:after {
display: table;
content: "";
}
.container:after {
clear: both;
}
我立即尝试过了,和我的代码特定部分看起来像这样:
<p class="sample-preview">
<span class="sample-preview">PREVIEW</span>
<em>This is italicized aka emphasized</em>, and so is <em>this</em>.<br />
<strong>This is bold aka strong emphasis</strong>, and so is <strong>this</strong>.<br />
Use <strong><em>italics and bold together</em></strong> if you <strong><em>have to</em></strong>.
</p>
+
p.sample-preview {
border: 1px solid #FFCCC9;
background: #FFEBE9;
outline: 2px solid #FFEBE9;
padding: 10px;
}
span.sample-preview {
display: inline-block;
float: right;
margin:0;
font-weight: bold;
font-size: 12px;
background: #FFCCC9;
padding: 2px 5px;
}
.sample-preview {
margin-left: auto;
margin-right: auto;
*zoom: 1;
}
.sample-preview:before, .sample-preview:after {
display: table;
content: "";
}
.sample-preview:after {
clear: both;
}
虽然我不能完全肯定,我觉得这个代码是造成我想它在页面上一个奇怪的错误。我为什么这么认为?当我使用Firebug从代码中删除display: table;
时,一切似乎都很好。
你可以看看页面here,错误是 - 第一个粉红色框比内容高。我究竟做错了什么?
您应该使用无论是浮动还是显示:表格。选择一个和它一起使用,都会造成问题。, – Kyle 2012-03-19 12:26:15
@KyleSevenoaks我不明白。所以,在我的情况下是'display:table;'不必要?如果是这样,为什(你可以请尝试更清楚一点吗?) – 2012-03-19 12:28:21
嗯,这取决于你想要做什么。显示:表;有它的地位,如浮动。你想实现什么? :) – Kyle 2012-03-19 12:33:14