天天看點

浮動引發的Bug

浮動引發的Bug

浮動的方法

float:left;right;none

浮動的原理

改變元素的排布方式;浮動元素會脫離文檔流。

浮動影響

産生卡頓;浮動的子元素撐不開父元素高度。父元素不設定高度,子元素都浮動;

浮動解決方法

1.給父級也加浮動(這種情況當父級margin:0 auto;時不居中)

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>1.給父級也加浮動(不居中了)</title>
    <style>
        .parentElement{ width:300px;margin:0 auto;border:10px solid #000;float:left;}
        .subElement{ width:200px;height:200px;background:red;float:left;}
    </style>
</head>
<body>
<div class="parentElement">
        <div class="subElement"></div>
</div>
</body>
</html>
           

2.給父級加display:inline-block;(這種情況當父級margin:0 auto;時不居中。隻有IE6,7居中)

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>給父級加display:inline-block</title>
    <style>
        .parentElement{ width:300px;margin:0 auto;border:10px solid #000; display:inline-block;}
        .subElement{ width:200px;height:200px;background:red;float:left;}
    </style>
</head>
<body>
<div class="parentElement">
        <div class="subElement"></div>
</div>
</body>
</html>
           

3.在浮動元素下加

IE6下,塊元素有最小高度,即當height<19px時,預設為19px;

解決方法:font-size:0;或overflow:hidden;

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>在浮動元素下加<div class="clear"></div></title>
    <style>
        .parentElement{ width:300px;margin:0 auto;border:10px solid #000;}
        .subElement{ width:200px;height:200px;background:red;float:left;}
        .clear{ height:0px;font-size:0;clear:both;}
    </style>
</head>
<body>
    <div class="parentElement">
        <div class="subElement"></div>
        <div class="clear"></div>
    </div>
</body>
</html>
           

4.在浮動元素下加

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>在浮動元素下加<br clear="all"/></title>
    <style>
        .parentElement{ width:300px;margin:0 auto;border:10px solid #000;}
        .subElement{ width:200px;height:200px;background:red;float:left;}

    </style>
</head>
<body>
    <div class="parentElement">
        <div class="subElement"></div>
        <br clear="all"/>
    </div>
</body>
</html>
           

5.給浮動元素父級加{zoom:1;}

:after{content:""; display:block;clear:both;}

在IE6,7下浮動元素的父級有寬度就不用清浮動

haslayout 根據元素内容的大小 或者父級的父級的大小來重新的計算元素的寬高

display: inline-block

height: (任何值除了auto)

float: (left 或 right)

width: (任何值除了auto)

zoom: (除 normal 外任意值)

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>無标題文檔</title>
    <style>
        .parentElement{margin:0 auto;border:10px solid #000;}
        .subElement{ width:200px;height:200px;background:red;float:left;}
        .clear{zoom:1;}
        .clear:after{content:""; display:block;clear:both;}
    </style>
</head>
<body>
    <div class="parentElement clear">
            <div class="subElement"></div>
    </div>
</body>
</html>

           

6.給浮動元素父級加overflow:auto;或者overflow:hidden;

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>無标題文檔</title>
    <style>
        .parentElement{ width:300px;border:10px solid #000;overflow:hidden;}
        .subElement{ width:200px;height:200px;background:Red;float:left;}
    </style>
</head>
<body>
<div class="parentElement">
    <div class="subElement"></div>
</div>
</body>
</html>