zoukankan      html  css  js  c++  java
  • CSS 外层box自动计算高度的问题

    外层box自动计算高度的问题
    根据W3C定义,没有float属性的外层box不会自动计算高度,要计算高度,必须在内层最后一个box加入clear:both。
    Opera、netscape、mozilla等不会计算外层box高度,但是微软ie6会自动计算外层高度。比如:

    div css xhtml xml Example Source Code Example Source Code [www.52css.com]
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>www.52css.com</title>
    <style>
    .outer {
    600px;
    background:#000;
    }
    .inner1 {
    float:left;
    200px;
    height:100px;
    margin:5px;
    background:red;
    }
    .inner2 {
    float:left;
    200px;
    height:100px;
    margin:5px;
    background:yellow;
    }
    </style>
    </head>
    <body>
    <div class="outer">
    <div class="inner1"></div>
    <div class="inner2"></div>
    </div>
    </body>
    </html>

    上面的代码在ie中有黑色的背景,但是没有正确的计算上下的margin,在inner2下面加上一个包含clear:both属性的div后,可以正确计算margin。但是firefox中仍然没有黑色背景,通常的解决办法是定义一下clear:both这个div的高度,或者插入全角空格,这样就必须增加额外的高度。网上一种比较好的解决办法是在外层div中加入overflow属性,同时使用clear:both,这样就不会增加额外的高度了。如下:

    div css xhtml xml Example Source Code Example Source Code [www.52css.com]
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>www.52css.com</title>
    <style>
    .outer {
    600px;
    background:#000;
    overflow:auto;
    }
    .inner1 {
    display:inline;
    float:left;
    200px;
    height:100px;
    margin:5px;
    background:red;
    }
    .inner2 {
    display:inline;
    float:left;
    200px;
    height:100px;
    margin:5px;
    background:yellow;
    }
    .clear {
    clear:both;
    }
    </style>
    </head> 
    <body>
    <div class="outer">
    <div class="inner1"></div>
    <div class="inner2"></div>
    <div class="clear"></div>
    </div>
    </body>
    </html>

    因此,外层css要定义overflow属性,内层最后要加上clear属性。
  • 相关阅读:
    WCF系列(七) WCF安全系列(二) netTCPBinding绑定之Transport安全模式
    WCF系列(六) WCF安全系列(一) basicHttpBinding
    Convert .Net Program To Mono
    Adware:Win32/FastSaveApp 清除
    Python Http Get Post请求
    Python正则表达式应用示例
    Basic4android 使用Basic开发Android应用
    Decode Android AndroidManifest.xml file via C#
    Python工作记录
    趣文:程序员/开发人员的真实生活
  • 原文地址:https://www.cnblogs.com/feinian/p/1455882.html
Copyright © 2011-2022 走看看