css - 为什么 float div 的顶部与父 div 不对齐?

标签 css html css-float

我正在尝试创建页面的一部分,其中两个文本 div 彼此对齐,但一个 float 到另一个的右侧。无论我尝试什么, float 的 div 似乎都会在开始之前留下大约一行空白文本。

fiddle http://jsfiddle.net/7MpYj/

这是它在 Chrome(和 Safari)中的样子:

enter image description here

我玩过 margin、padding、vertical-align,似乎没有什么可以摆脱两者之间的空行。

CSS 是:

body {
  font-family: sans-serif;
  font-size: 13px;
}
div.sidebar {
  width: 300px;
  float: right;
  background: pink;
}
div.maincolumn {
  background: #aaffaa;
}
div.clear {
  clear: right;
}

HTML 是:

<div class="maincolumn">
  <div class="sidebar">
    <h3>Constitution</h3>
    We the People of the United States, in Order to form a more perfect
    Union, establish Justice, insure domestic Tranquility, provide for
    the common defence, promote the general Welfare, and secure the
    Blessings of Liberty to ourselves and our Posterity, do ordain and
    establish this Constitution for the United States of America.
  </div>
  <h3>Declaration</h3>
  When in the Course of human events, it becomes necessary for one
  people to dissolve the political bands which have connected them with
  another, and to assume among the powers of the earth, the separate and
  equal station to which the Laws of Nature and of Nature's God entitle
  them, a decent respect to the opinions of mankind requires that they
  should declare the causes which impel them to the separation.
  <p/>
  We hold these truths to be self-evident, that all men are created
  equal, that they are endowed by their Creator with certain unalienable
  Rights, that among these are Life, Liberty and the pursuit of
  Happiness.--That to secure these rights, Governments are instituted
  among Men, deriving their just powers from the consent of the
  governed, --That whenever any Form of Government becomes destructive
  of these ends, it is the Right of the People to alter or to abolish
  it, and to institute new Government, laying its foundation on such
  principles and organizing its powers in such form, as to them shall
  seem most likely to effect their Safety and Happiness.
</div>
<div class="clear">
  Edward by the grace of God King of England, lord of Ireland and
  duke of Aquitaine sends greetings to all to whom the present
  letters come. We have inspected the great charter of the lord
  Henry, late King of England, our father, concerning the liberties
  of England in these words:
</div>

是否有一些 CSS 魔术可以拉出 float div 中的文本?

最佳答案

是的。将 overflow:auto 添加到 div.maincolumn

div.maincolumn {
    background: #aaffaa;
    overflow:auto;
}

jsFiddle example

您的特定布局是 block formatting context 的示例以及 overflow:auto 如何恢复您寻求的行为。

关于css - 为什么 float div 的顶部与父 div 不对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17182110/

相关文章:

HTML 文件和 CSS 文件不会在本地主机中链接

jquery - 添加类 "selected"

html - float 属性在我的 CSS 中不起作用

javascript - 对多个图像使用模糊图像加载

css - 使用多列设置 select 和 options 元素的样式

html - 响应式文本

javascript - 砌体画廊未正确堆叠

ios - &lt;input type ="number"/> 在 iOS 上不显示数字键盘

html - 需要div排列如截图所示

jquery - 我通过 jquery 更改某些 css 样式后出现 iOS css 错误