float浮動以及案例演示

浮動元素會影響後邊的元素,但不會影響前邊的元素css

清除浮動:html

方法一:在浮動元素後面添加一個空元素css3

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box{
            width:100px;
            height:100px;
            background:#abcdef;
            float:left;
            border:1px solid;
        }
        .clear{
            clear:both;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="box"></div>
        <div class="box"></div>
    </div>
    <div class="clear"></div>
    <div class="last">
        last~
    </div>
</body>
</html>

 

 

 

方法二:spa

給浮動元素的父元素添加overflow:hidden;code

再添加zoom:1; 兼容IEhtm

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box{
            width:100px;
            height:100px;
            background:#abcdef;
            float:left;
            border:1px solid;
        }
        .wrap{
            overflow: hidden;
            zoom:1;    
        }
        .clear{
            clear:both;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="box"></div>
        <div class="box"></div>
    </div>
    <div class="last">
        last~
    </div>
</body>
</html>

方法三:blog

使用css3的:after僞元素it

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .clearfix{
            zoom:1;/*兼容IE*/
        }
        .clearfix:after{
            content:'';
            display: block;
            height:0;
            visibility: hidden;
            clear:both;
        }
        .box{
            width:100px;
            height:100px;
            background:#abcdef;
            float:left;
            border:1px solid;
        }
    </style>
</head>
<body>
    <div class="wrap clearfix">
        <div class="box"></div>
        <div class="box"></div>
    </div>
    <div class="last">
        last~
    </div>
</body>
</html>

float完成導航案例演示io

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        /*重置樣式*/
        *{margin:0;padding:0;}
        a{text-decoration: none;}
        ul{list-style:none;}
        /*基本樣式*/
        .header{
            width:1120px;
            background:#ccc;
            margin:0 auto;
            overflow: hidden;
            zoom:1;
            padding:0 40px;
        }
        .logo{
            float:left;
            width:100px;
            height:68px;
        }
        .nav{
            float:right;
            overflow: hidden;
            zoom:1;
        }
        .nav li{
            float: left;
            margin-right:20px;
            
        }
        .nav li a{
            color:#333;
            display: block;
            height:68px;
            line-height:68px;
        }
        .nav li a:hover{
            color:#fff;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="logo">
            <a href="#"><img src="cat-little.jpg" alt="logo"></a>
        </div>
        <ul class="nav">
            <li><a href="#">導航1</a></li>
            <li><a href="#">導航2</a></li>
            <li><a href="#">導航3</a></li>
            <li><a href="#">導航4</a></li>
            <li><a href="#">導航5</a></li>
        </ul>

    </div>
</body>
</html>

相關文章
相關標籤/搜索