【问题标题】:Why does the top of a float div not align with the parent div?为什么浮动 div 的顶部与父 div 不对齐?
【发布时间】:2013-06-19 02:46:49
【问题描述】:

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

拨弄http://jsfiddle.net/7MpYj/

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

我玩过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 魔法可以拉起浮动 div 中的文本?

【问题讨论】:

  • 我不太明白你的意思——你能更具体地说明你希望它是什么样子吗?
  • 我想对齐“声明”和“宪法”这两个词,最好是在包含彩色框的顶部。是不是更清楚了?
  • 知道了。我刚刚发布了一个答案

标签: css html css-float


【解决方案1】:

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

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

jsFiddle example

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

【讨论】:

  • 谢谢。这行得通,但它是通过按下左侧(非浮动)文本而不是提升右侧(浮动)文本来实现的。但我要去阅读有关块格式化上下文的信息。
  • @ShouldKnowThisAlready 扩展了您的 jsfiddle 的视图。它已经按照您的意愿工作了。再试一次。
  • 您看到的是 h3 元素的正常边距。如果你通过 CSS 删除它,你可以得到我认为你想要的:jsfiddle.net/j08691/7MpYj/4
  • 好的,谢谢。您的解释加上上面的@Vijay 一起解释了这一点。我不明白主列中的边距正在消失。
【解决方案2】:

将此添加到您的 CSS:

div.sidebar h3 {
    margin-top:0;
}

这是一个小提琴:http://jsfiddle.net/7MpYj/3/

【讨论】:

  • 噢!谢谢——我没有想到问题是h3,而不是浮动。现在我理解了@j08691 的答案,其中块格式化上下文意味着主列和浮动列都显示了 h3 的边距。
【解决方案3】:

尝试调整垂直对齐以将文本拉到顶部:

div.sidebar {
    vertical-align: top;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 2022-12-17
    • 2011-10-04
    • 1970-01-01
    • 2015-02-12
    • 2015-08-25
    • 2013-04-04
    相关资源
    最近更新 更多