【问题标题】:CSS and DIV with dynamic height具有动态高度的 CSS 和 DIV
【发布时间】:2012-12-13 18:34:42
【问题描述】:

我有一个这样的网页结构:

<div class="total">
    <div class="menu">
    </div>
    <div class="content">
    </div>
</div>

所以“menu” div 包含我的左侧菜单,“content” div 包含一些动态文本。实际上,我所做的结构都以正确的方式定位在我的“总”div 中。我实际上已经像这样在我的 CSS 上编辑了我的“总”div:

.total{
position:relative;
top:50px;
margin: 0 auto;
background-color:#eeeeee;
height:auto;
border:2px solid #000;
border-color:rgb(82,115,154);
}

问题是我无法获得我真正想要的东西:边框都在顶部(就像一个水平行)并且我的具有不同背景颜色的 div 没有出现。

如何使“总”div 的高度动态化?

编辑:链接到jsFiddle

【问题讨论】:

  • 他的意思是发布一个在JSfiddle上不起作用的示例代码,以便可以观察和测试以找到问题。 jsfiddle.net
  • 像这样:jsfiddle.net/vf98v(这次是为你做的,所以你下次知道)
  • 问题肯定出在 div.menu 或 div.content 的 CSS/HTML 上,能不能看一下 CSS?

标签: css html dynamic height


【解决方案1】:

.total 元素已完全折叠,因为它的所有子元素都是浮动的。您需要做的就是添加一个 clearfix。

http://jsfiddle.net/CJZCt/3/

.total{
position:relative;
top:50px;
margin: 0 auto;
background-color:#eeeeee;
height:auto;
border:2px solid #000;
border-color:rgb(82,115,154);
    overflow: hidden;
}

其他清除浮动的方法可以在这里找到:https://stackoverflow.com/a/1633170/1652962

【讨论】:

    【解决方案2】:

    您可以使用 clearfix 解决此问题:http://nicolasgallagher.com/micro-clearfix-hack/

    将此添加到您的 css,并将 cf 类添加到您的 div.content

    /**
     * For modern browsers
     * 1. The space content is one way to avoid an Opera bug when the
     *    contenteditable attribute is included anywhere else in the document.
     *    Otherwise it causes space to appear at the top and bottom of elements
     *    that are clearfixed.
     * 2. The use of `table` rather than `block` is only necessary if using
     *    `:before` to contain the top-margins of child elements.
     */
    .cf:before,
    .cf:after {
        content: " "; /* 1 */
        display: table; /* 2 */
    }
    
    .cf:after {
        clear: both;
    }
    
    /**
     * For IE 6/7 only
     * Include this rule to trigger hasLayout and contain floats.
     */
    .cf {
        *zoom: 1;
    }
    

    测试:http://jsfiddle.net/CJZCt/4/

    【讨论】:

      猜你喜欢
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多