1. 程式人生 > 實用技巧 >HTML CSS+DIV 實現排版佈局

HTML CSS+DIV 實現排版佈局

單列布局案例:

程式碼如下:
<!doctype html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title>單列布局</title>
    </head>
    <style>
        body{
            margin:0;
        }
        .box{
            width:960px;
            margin:0 auto;
        }
        .box .header
{ height:120px; border:1px solid #f00; line-height:120px; } .box .main{ height:300px; border:1px solid #0f0; line-height:300px; } .box .footer{ height:100px; border:1px solid #00f; line-height
:100px; } div{ text-align:center; } </style> <body> <div class="box"> <div class="header">頭部</div> <div class="main">主題</div> <div class="footer">底部</div>
</div> </body> </html>

執行結果:

雙列布局案例:

程式碼如下:
<!doctype html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title>雙列布局</title>
    </head>
    <style>
        body{
        margin:0;
        }
        .box{
            width:80%;
            margin:0 auto;
            overflow:hidden;
        }
        .box .left{
            width:30%;
            height:400px;
            background-color:#0f0;
            float:left;
        }
        .box .right{
            width:70%;
            height:400px;
            background-color:#f00;
            float:left;
        }
    </style>
    <body>        
        <div class="box">
            <div class="left"></div>
            <div class="right"></div>
        </div>        
    </body>
</html>

執行結果:

三列布局案例:

程式碼如下:
<!doctype html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title>三列布局</title>
    </head>
    <style>
    body{
        margin:0;
    }
    .box{
        width:960px;
        margin:0 auto;
        position:relative;
    }
    .box .left{
        width:200px;
        height:400px;
        background-color:#0f0;
        position:absolute;
    }
    .box .center{
        height:400px;
        background-color:#00f;
        margin:0 300px 0 200px;
    }
    .box .right{
        width:300px;
        height:400px;
        background-color:#f00;
        position:absolute;
        right:0;
        top:0;
    }
    </style>
    <body>        
        <div class="box">
            <div class="left"></div>
            <div class="center"></div>
            <div class="right"></div>
        </div>            
    </body>
</html>

執行結果如下圖

混合佈局案例:

程式碼如下:
<!doctype html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title>混合佈局</title>
    </head>
    <style>
    body{
        margin:0;
    }
    .box{
        width:960px;
        margin:0 auto;
    }
    .header{
        height:120px;
        background-color:#ddd;    
    }
    .main{
        height:400px;
        background-color:#f00;
        position:relative;
    }

    .main .left{
        width:200px;
        height:400px;
        position:absolute;
        left:0;
        top:0;
        background-color:#0fccaa;
    }
    .main .center{
        height:400px;
        margin:0 305px 0 205px;
        background-color:#123456;
    }

    .main .right{
        width:300px;
        height:400px;
        position:absolute;
        right:0;
        top:0;
        background-color:#ff0;
    }
    .footer{
        height:80px;
        background-color:#ddd;
    }
    </style>
    <body>        
        <div class="box">
            <div class="header"></div>
            <div class="main">
                <div class="left"></div>
                <div class="center"></div>
                <div class="right"></div>        
            </div>
            <div class="footer"></div>
        </div>    
    </body>
</html>

執行結果如下圖

總結:

排版佈局需要掌握的知識

  • div 相關屬性
  • float 浮動屬性
  • position 定位屬性
  • clear 清除浮動的應用