Css實現左欄固定寬度,右欄自適應寬度執行個體

來源:互聯網
上載者:User

 代碼如下 複製代碼

    <!doctype html>
    <html lang="zh-CN">
    <head>
            <meta charset="UTF-8">
            <title>左欄固定寬度,右欄自適應之絕對位置法</title>
            <style type="text/css">
            body{
                    margin: 0;
            }
            #nav{
                    top: 0;
                    left: 0;
                    width: 230px;
                    height: 600px;
                    background: #ccc;
                    position: absolute;
            }
            #main{
                    height: 600px;
                    margin-left: 230px;
                    background: #0099ff;
            }
            </style>
    </head>
    <body>
            <div id="container">
                    <div id="nav">
                            左欄
                    </div>
                    <div id="main">
                            右欄
                    </div>
            </div>
    </body>
    </html>

看起來很美好,但是。。

由於左欄使用絕對位置,脫離了文檔流,因此有一個缺陷,即當左欄高度大於右欄時,無法將container撐開,這個缺陷單單只看兩欄布局並沒有太大影響,但如果兩欄布局下面有一個底欄,就會出現底欄與左欄重疊的情況:

 代碼如下 複製代碼

 

    <!doctype html>
    <html lang="zh-CN">
    <head>
            <meta charset="UTF-8">
            <title>左欄固定寬度,右欄自適應之絕對位置法</title>
            <style type="text/css">
            body{
                    margin: 0;
            }
            #nav{
                    top: 0;
                    left: 0;
                    width: 230px;
                    height: 600px;
                    background: #ccc;
                    position: absolute;
            }
            #main{
                    height: 400px;
                    margin-left: 230px;
                    background: #0099ff;
            }
            #footer{
                    text-align: center;
                    background: #009000;
            }
            </style>
    </head>
    <body>
            <div id="container">
                    <div id="nav">
                            左欄
                    </div>
                    <div id="main">
                            右欄
                    </div>
            </div>
            <div id="footer">
                    底欄
            </div>
    </body>
    </html>

我們再來看看第二種方法,左欄固定寬度,右欄自適應之負margin法:

 代碼如下 複製代碼

 

    <!doctype html>
    <html lang="zh-CN">
    <head>
            <meta charset="UTF-8">
            <title>左欄固定寬度,右欄自適應之負margin法</title>
            <style type="text/css">
            body{
                    margin: 0;
            }
            #container{
                    margin-left: 230px;
                    _zoom: 1;
                    /*相容IE6下左欄消失問題*/
            }
            #nav{
                    float: left;
                    width: 230px;
                    height: 600px;
                    background: #ccc;
                    margin-left: -230px;
                    position: relative;
                    /*相容IE6下左欄消失問題,IE6真不讓人省心啊>_<*/
            }
            #main{
                    height: 600px;
                    background: #0099ff;
            }
            </style>
    </head>
    <body>
            <div id="container">
                    <div id="nav">
                            左欄
                    </div>
                    <div id="main">
                            右欄
                    </div>
            </div>
    </body>
    </html>

這樣無論兩欄的高度如何變化都不會有問題了:

 代碼如下 複製代碼

 

    <!doctype html>
    <html lang="zh-CN">
    <head>
            <meta charset="UTF-8">
            <title>左欄固定寬度,右欄自適應之負margin法</title>
            <style type="text/css">
            body{
                    margin: 0;
            }
            #container{
                    margin-left: 230px;
                    _zoom: 1;
                    /*相容IE6下左欄消失問題*/
            }
            #nav{
                    float: left;
                    width: 230px;
                    height: 600px;
                    background: #ccc;
                    margin-left: -230px;
                    position: relative;
                    /*相容IE6下左欄消失問題,IE6真不讓人省心啊>_<*/
            }
            #main{
                    height: 400px;
                    background: #0099ff;
            }
            #footer{
                    clear: both;
                    text-align: center;
                    background: #009000;
            }
            </style>
    </head>
    <body>
            <div id="container">
                    <div id="nav">
                            左欄
                    </div>
                    <div id="main">
                            右欄
                    </div>
            </div>
            <div id="footer">
                    底欄
            </div>
    </body>
    </html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.