【css】談談 css 的各種居中——讀編寫高質量代碼有感


css 的居中有水平居中和垂直居中,這兩種居中又分為行內元素居中和塊級元素居中,不同的居中用不同方法。

水平居中

1、行內元素水平居中(文本,圖片)

給父層設置 text-align:center; 可以實現行內元素水平居中。

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    .center{text-align:center;}
    </style>
</head>
<body>
    <div class="center">
        <a href="http://www.google.com.hk/">谷歌搜索</a><br/><br/>
        <img src="cat.jpg" width="248" height="162" alt=""/>
    </div>
</body>
</html>

2、確定寬度塊級元素水平居中

確定寬度的塊級元素水平居中,常用的有 margin:0 auto; 相信很多開發人員都用的是這個,不過本人認為還有更好的寫法:margin-left:auto;margin-right:auto; 因為 margin-top 和 margin-bottom 在重置 css 時就已經寫了為 0 了。

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    .center{width:100px;height:100px;margin-left:auto;margin-right:auto;background:green;}
    </style>
</head>
<body>
    <div class="center"></div>
</body>
</html>

3、不確定寬度的塊級元素水平居中

不確定寬度的塊級元素有三種方法實現。

方法一:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    *{margin:0;padding:0;}
    ul{list-style:none;}
    table{margin-left:auto;margin-right:auto;}
    .demo li{float:left;display:inline;margin-right:5px;}
    .demo a{float:left;width:20px;height:20px;text-align:center;line-height:20px;background:#316ac5;color:white;border:1px solid #316ac5;text-decoration:none;}
    .demo a:hover{background:white;color:#316ac5;}
    </style>
</head>
<body>
    <table>
        <tbody>
            <tr>
                <td>
                    <ul class="demo">
                        <li><a href="#">1</a></li>
                    </ul>
                </td>
            </tr>
        </tbody>
    </table>
    <table>
        <tbody>
            <tr>
                <td>
                    <ul class="demo">
                        <li><a href="#">1</a></li>
                        <li><a href="#">2</a></li>
                        <li><a href="#">3</a></li>
                    </ul>
                </td>
            </tr>
        </tbody>
    </table>
    <table>
        <tbody>
            <tr>
                <td>
                    <ul class="demo">
                        <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>
                </td>
            </tr>
        </tbody>
    </table>
</body>
</html>

這里用到了 table 標簽來實現不確定寬度的塊級元素水平居中。table 本身不是塊級元素,如果不給它設定寬度的話,會由內部元素的寬度“撐開”,但即使不設定它的寬度,僅設置 margin-left:auto 和 margin-right:auto 就可以實現水平居中。

這種方法很巧妙,但是增加了無語義標簽,加深了標簽的嵌套層數。

方法二:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    *{margin:0;padding:0;}
    ul{list-style:none;}
    .wrapper{width:500px;height:100px;background:black;}
    .demo{text-align:center;padding:5px;}
    .demo li{display:inline;}
    .demo a{padding:2px 6px;background:#316ac5;color:white;border:1px solid #316ac5;text-decoration:none;}
    .demo a:hover{background:white;color:#316ac5;}
    </style>
</head>
<body>
    <div class="wrapper">
        <ul class="demo">
            <li><a href="#">1</a></li>
        </ul>
        <ul class="demo">
            <li><a href="#">1</a></li>
            <li><a href="#">2</a></li>
            <li><a href="#">3</a></li>
        </ul>
        <ul class="demo">
            <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>

方法二是改變元素的 display 值,使塊級元素變成行內元素,然后使用 text-align:center 使其居中。相對於方法一,它不用增加無語義標簽,簡化了標簽的嵌套深度,但它也存在一定的問題:它將塊級元素變成了行內元素,這樣就失去了一些塊級元素的功能,比如設置寬度,高度。

方法三:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    *{margin:0;padding:0;}
    ul{list-style:none;}
    .wrapper{width:500px;height:100px;background:black;}
    .demo{clear:both;padding-top:5px;float:left;position:relative;left:50%;}
    .demo li{display:inline;float:left;margin-right:5px;position:relative;left:-50%;}
    .demo a{float:left;width:20px;height:20px;text-align:center;line-height:20px;background:#316ac5;color:white;border:1px solid #316ac5;text-decoration:none;}
    .demo a:hover{background:white;color:#316ac5;}
    </style>
</head>
<body>
    <div class="wrapper">
        <ul class="demo">
            <li><a href="#">1</a></li>
        </ul>
        <ul class="demo">
            <li><a href="#">1</a></li>
            <li><a href="#">2</a></li>
            <li><a href="#">3</a></li>
        </ul>
        <ul class="demo">
            <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>

方法三通過給父層設置浮動和相對定位以及 left:50%,子元素設置相對定位和 left:-50% 來實現水平居中。它可以保留塊級元素的功能,而且不會添加無語義標簽,不增加嵌套深度,但是設置了相對定位,會帶來一定的副作用。

這三種方法各有優缺點,具體使用哪種方法可以視具體情況而定。

垂直居中

1、父層高度不確定的垂直居中

通過給父層設置相同的上下內邊距實現。

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    *{margin:0;padding:0;}
    .demo{width:500px;color:white;margin-bottom:10px;padding-top:20px;padding-bottom:20px;background:black;}
    .content{width:200px;height:50px;background:red;}
    </style>
</head>
<body>
    <div class="demo">hello world</div>
    <div class="demo"><img src="cat.jpg" width="248" height="162" alt=""/></div>
    <div class="demo"><div class="content"></div></div>
</body>
</html>

2、父層高度確定的單行文本垂直居中

通過給父層設置行高來實現,行高和父層高度相同。

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    *{margin:0;padding:0;}
    .demo{width:500px;color:white;background:black;height:100px;line-height:100px;}
    </style>
</head>
<body>
    <div class="demo">hello world</div>
</body>
</html>

3、父層高度確定的多行文本、圖片、塊級元素垂直居中

方法一:

說到垂直居中,css 中有個用於垂直居中的屬性 vertical-align,但只有在父層為 td 或者 th 時,這個屬性才會生效,對於其他塊級元素,例如 div、p 等,默認情況是不支持的。在 firefox 和 ie8 下,可以設置塊級元素的 display 值為 table-cell,來激活 vertical-align 屬性,但 ie6,7 並不支持,所以這種方法沒有辦法跨瀏覽器兼容。但是我們可以使用 table。

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    *{margin:0;padding:0;}
    .wrapper{background:black;width:500px;color:white;height:100px;}
    .demo{width:200px;background:red;height:50px;}
    </style>
</head>
<body>
    <table>
        <tr>
            <td class="wrapper">
                hellow world<br/>
                hellow world<br/>
                hellow world<br/>
            </td>
        </tr>
    </table>
    <table>
        <tr>
            <td class="wrapper">
                <img src="cat.jpg" alt=""/>
            </td>
        </tr>
    </table>
    <table>
        <tr>
            <td class="wrapper">
                <div class="demo"></div>
            </td>
        </tr>
    </table>
</body>
</html>

table 可以很好的實現垂直居中效果,但是它添加了無語義標簽,增加了嵌套深度。

方法二:

對支持 display:table-cell 的 ie8 和 firefox 用 display:table-cell 和 vertical-align:middle 來實現居中,對不支持 display:table-cell 的 ie6 和 ie7 使用 hack 寫法。

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    *{margin:0;padding:0;}
    .mb{margin-bottom:10px;}
    .wrapper{background:black;width:500px;color:white;height:100px;margin-bottom:10px;display:table-cell;vertical-align:middle;*position:relative;}
    .demo{width:200px;background:red;height:50px;}
    .vam{*position:absolute;*top:50%;}
    .va{*position:relative;*top:-50%;}
    </style>
</head>
<body>
    <div class="mb10">
        <div class="wrapper">
            <div class="vam">
                <div class="va">
                    hellow world<br/>
                    hellow world<br/>
                    hellow world
                </div>
            </div>
        </div>
    </div>
    <div class="mb10">
        <div class="wrapper">
            <div class="vam">
                <img src="cat.jpg" alt=""/>
            </div>
        </div>
    </div>
    <div class="mb10">
        <div class="wrapper">
            <div class="vam">
                <div class="va demo"></div>
            </div>
        </div>
    </div>
</body>
</html>

利用 hack 技術區別對待 firefox、ie8 和 ie6、ie7,在不支持 display:table-cell 的 ie6 和 ie7 下,通過給父子兩層元素分別設置 top:50% 和 top:-50% 來實現居中。這種方法的好處是沒有增加額外的標簽,但缺點也很明顯,一方面它使用了 hack,不利於維護,另一方面,它設置了 position:relative 和 position:absolute,帶來了副作用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM