【问题标题】:Parent div's height setting interferes with child div's float父 div 的高度设置会干扰子 div 的浮动
【发布时间】:2011-10-27 13:54:48
【问题描述】:

我正在制作的网页有些问题...

我有一个 div 包含页面上的所有元素(或者更确切地说是页面页眉和页脚之间的所有元素,但这无关紧要)。 body div 的高度会随着它所持有的内容而变化。 包装器包含一个 id="content" 的 div,它应该包含我想在页面上显示的任何内容,一个 div 我用作按钮 (id="collapse") 和一个 div (id="calendar")创建一个包含即将发生的事件列表的字段。

CSS:

#body {
    height:500px;
    min-height:250px;
    width:100%;
    background:#fc0;
    padding-bottom:100%;
    margin-bottom:-100%;
}
#calendar {
    float:right;
    height:100%;
    width:20%;
    border-left:10px solid #678DC0;
    background:#b0c4de;
    padding-bottom:100%;
    margin-bottom:-100%;
}
#collapse {
    float:right;
    margin-top:10px;
    text-align:left;
    padding-left:10px;
    padding-top:20px;
    padding-bottom:20px;
    background:#678DC0;
    border-top-left-radius:25px;
    -moz-border-top-left-radius:25px; /* Firefox 3.6 and earlier */
    border-bottom-left-radius:25px;
    -moz-border-bottom-left-radius:25px;
}
#content {
    height:auto;
    width:100%;
    background:#b0c4de;
    text-align:center;
    padding:50px;
}

HTML:

<div id="body">
<div id="calendar"></div>
<div id="collapse">>></div>
<div id="content"></div>
</div>

现在这是我的问题:如果我设置 #body height:auto; #collapse div 的浮点数和 #content div 的宽度表现得很奇怪。 #collapse div 一直浮动到#body div 的边缘,而不是在它浮动到#calendar div 时停止。 #content div 也是如此。它一直延伸到#body 边缘,而不是#calendar。

希望我给了你一个正确的表述。对我的要求有任何疑问吗?

有什么想法吗?

【问题讨论】:

  • 当你使用float时,它会浮动下一个兄弟元素,除非你有一个Clear:both|left|right;作为 CSS 样式。因为你的“body” div 有 height:auto;,它会根据子 div 的大小而增长/缩小。

标签: css html height css-float


【解决方案1】:

正如 Ryan 所说,您可以使用 clear 样式正确定位浮动元素。我在这里设置了一个测试页面:http://jsbin.com/apinuj/2/edit#html,live,演示了如何在您的 html 上使用 clear fix 以便在设置为 height: auto. 时正确对齐 body div

【讨论】:

  • 非常感谢您!典型的好答案! :) 但是我对一件事感到困惑...那行说 article, aside, figure, footer, header, hgroup, menu, nav, section { display: block; } 我知道 display:block 是做什么的,但是你把它分配给了什么?
  • 哦,别担心。那是jsbin创建的代码。基本上它使 html5 标签(如导航、部分等)在 IE 中工作。以下是一些相关信息:http://net.tutsplus.com/tutorials/html-css-techniques/how-to-make-all-browsers-render-html5-mark-up-correctly-even-ie6/
  • 嗯,这有点奇怪...我没有让它在我的电脑上工作,所以我开始寻找我编码的内容和你给我的内容之间的差异。我什么也没找到。比我将文件中的所有内容复制粘贴到那个实时编辑器。这样可行。但是,这是奇怪的部分,如果我按“渲染”它不会按我想要的方式排列。如果我按“代码”,它会。哇。帮助! jsbin.com/apinuj/4
  • 嗯,不确定。有时间我会看的。
  • 似乎 html 存在日历 div 为空的问题。我向它添加了一个空格,它现在可以正确浮动:http://jsbin.com/apinuj/6/。至于页面高度不一样,可以通过改变容器的最小高度来修改。
猜你喜欢
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 2019-07-08
  • 2013-07-18
  • 2013-02-02
  • 1970-01-01
  • 2012-03-16
相关资源
最近更新 更多