html - Clearfix 将内容下推

标签 html css css-float clearfix

我有一个有序列表,li 项中的元素是 float 的,所以我将 clearfix 代码应用于 li,但由于某种原因 li 元素被下推。

JS Fiddle 在这里:http://jsfiddle.net/48psdoeu/2/

.true-false ol {} .true-false ol > li:before,
.true-false ol > li:after {
  content: " ";
  display: table;
}
.true-false ol > li:after {
  clear: both;
}
.true-false ol .question {
  width: 70%;
  padding-top: 0;
  float: left;
}
.true-false ol .answer {
  width: 10%;
  float: left;
}
p {
  padding: 0;
  margin: 0;
}
<div class="true-false">
  <ol type="i">
    <li>
      <div class="question">
        <p>Always add water to chemicals as fast as possible.</p>
      </div>
      <div class="answer">test</div>
    </li>
    <li>
      <div class="question">
        <p>Always keep chemicals in their original container.</p>
      </div>
      <div class="answer">test</div>
    </li>

  </ol>
</div>

最佳答案

display: table 声明混淆了每个 li 元素中文本的基线,导致文本被下推。

display: table 在 clearfix 中并不是真正需要的。事实上,它实际上根本没有帮助使 clearfix 工作。 It's there for a different reason.您可以将其替换为 display: block,它会起作用。

但是你真的根本不需要这里的 clearfix。您可以通过对每个(连续的)li 应用清除来正常清除 float 。我可以想象你唯一真正需要 clearfix 的地方是 .true-false.true-false ol,但这取决于你的布局。不过我可以肯定的是,您绝对不需要在 li 元素上使用它。

.true-false ol > li {
  clear: both;
}
.true-false ol .question {
  width: 70%;
  padding-top: 0;
  float: left;
}
.true-false ol .answer {
  width: 10%;
  float: left;
}
p {
  padding: 0;
  margin: 0;
}
<div class="true-false">
  <ol type="i">
    <li>
      <div class="question">
        <p>Always add water to chemicals as fast as possible.</p>
      </div>
      <div class="answer">test</div>
    </li>
    <li>
      <div class="question">
        <p>Always keep chemicals in their original container.</p>
      </div>
      <div class="answer">test</div>
    </li>

  </ol>
</div>

关于html - Clearfix 将内容下推,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28591047/

相关文章:

html - float :right in the correct order it appears in HTML code

javascript - 使用 Ajax 传递单击的提交按钮值

javascript - 数据未显示在创建的指令中

html - 让一封信延伸到页面下方?

html - 为什么 link 元素中的 media 属性对 print 值不起作用?

html - float 或 'display: inline' 带有 css 背景图像的输入标签

javascript - 在值上附加元素

CSS全宽减边距左div,20px同行右div

html - 如何使用 CSS 制作 float 顶部?

html - 选择选项未在 Internet Explorer 中显示