display——table-cell屬性


display的table和table-cell一般情況下用的不多,所以很少有人去關注它,但他們兩個聯手起來會給你驚喜!

當兩個或者兩個以上標簽一起使用顯示在同一行時,以前常用的是float、position進行布局,在高版本的瀏覽器可以使用flex、grid進行布局。無意中發現使用display:table-cell也是一個很好用的自適應布局,本文就display:table-cell做學習總結。


display:table-cell指讓標簽元素以表格單元格的形式呈現,使元素類似於td標簽。IE8+及現代版本的瀏覽器都支持此屬性,IE6/7不支持(可用其他方法實現類似效果)。同樣,display:table-cell屬性也會被float,position:absolute等屬性破壞效果,應避免同時使用。

 
 
 
 
 
描述
none 此元素不會被顯示。
block 此元素將顯示為塊級元素,此元素前后會帶有換行符。
inline 默認。此元素會被顯示為內聯元素,元素前后沒有換行符。
inline-block 行內塊元素。(CSS2.1 新增的值)
list-item 此元素會作為列表顯示。
run-in 此元素會根據上下文作為塊級元素或內聯元素顯示。
compact CSS 中有值 compact,不過由於缺乏廣泛支持,已經從 CSS2.1 中刪除。
marker CSS 中有值 marker,不過由於缺乏廣泛支持,已經從 CSS2.1 中刪除。
table 此元素會作為塊級表格來顯示(類似 <table>),表格前后帶有換行符。
inline-table 此元素會作為內聯表格來顯示(類似 <table>),表格前后沒有換行符。
table-row-group 此元素會作為一個或多個行的分組來顯示(類似 <tbody>)。
table-header-group 此元素會作為一個或多個行的分組來顯示(類似 <thead>)。
table-footer-group 此元素會作為一個或多個行的分組來顯示(類似 <tfoot>)。
table-row 此元素會作為一個表格行顯示(類似 <tr>)。
table-column-group 此元素會作為一個或多個列的分組來顯示(類似 <colgroup>)。
table-column 此元素會作為一個單元格列顯示(類似 <col>)
table-cell 此元素會作為一個表格單元格顯示(類似 <td> 和 <th>)
table-caption 此元素會作為一個表格標題顯示(類似 <caption>)
inherit 規定應該從父元素繼承 display 屬性的值。

 

display:table-cell可以代替浮動布局,但是其不是最好的方法。其他方法有待進一步學習!


這里拋出這樣一個問題,如下,讓塊里的多行文字垂直居中?一說到垂直居中就會想到,單行文字垂直居中line-height等於height;塊級元素垂直居中,position定位或者flex布局。但這里我介紹display:table和table-cell是如何讓多行文字垂直居中的。雖然感覺用的不多,但是在某些時候還是挺管用的,如下:

1.多行文字居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>table</title>
    <style>
        .parent{
            display: table;
            width: 400px;
            height: 400px;
            text-align: center;
            border:1px solid red;
            margin:0 auto;
            background: blue;   /*背景顏色無效*/
        }
        .child{
            display: table-cell;    /*子元素成為表格單元格(類似 <td> 和 <th>)*/
            height: 200px;
            background: yellow;
            vertical-align: middle; /*表格容器可以設置垂直對齊屬性*/
            white-space: pre;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child">
display: table-row-group;
display: table-header-group;
display: table-footer-group;
display: table-row;
display: table-cell;
display: table-column-group;
display: table-column;
display: table-caption;
display: ruby-base;
display: ruby-text;
display: ruby-base-container;
display: ruby-text-container;
        </div>
    </div>
</body>
</html>

效果如下:

    

 

設置了display:table-cell的元素:

  • 對寬度高度敏感
  • 對margin值無反應
  • 響應padding屬性
  • 內容溢出時會自動撐開父元素

2.制作自適應搜索框

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>table</title>
    <style>
            .search-box{
                display: table;
                width:100%;
            }
            .search-content{
                width: 30%;
                display: table-cell;
                border: 1px solid #ccc;
                padding: 8px 0px;
            }
            .search-btn{
                display: table-cell;
                width: 5%;
                white-space: nowrap;
                padding: 5px 12px;
                background-color: #ccc;
                border: 1px solid #ccc;
                border-radius: 4px;
                border-bottom-right-radius: 0;
                border-top-right-radius: 0;
                font-size: 14px;
                color: #555;
                border-right: 0;
            }
    </style>
</head>
<body>
        <div class="search-box">
            <span class="search-btn">搜索</span>
            <input type="text" class="search-content"/>
        </div>
</body>
</html>

效果如下:
    

3.大小不固定的垂直居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>table</title>
    <style>
        .content {
            display: table-cell;
            padding: 10px;
            border: 2px solid #999;
        }

        .content div {
            display: inline-block;
            vertical-align: middle;
        }
    </style>
</head>
<body>
    <div class="content">
        <div style="padding: 50px 40px;background: #cccccc;color: #fff;"></div>
        <div style="padding: 60px 40px;background: #639146;color: #fff;"></div>
        <div style="padding: 70px 40px;background: #2B82EE;color: #fff;"></div>
        <div style="padding: 80px 40px;background: #F57900;color: #fff;"></div>
        <div style="padding: 90px 40px;background: #BC1D49;color: #fff;"></div>
    </div>
</body>
</html>

效果如下:
    

4.倆列自適應布局(寬度自動調節)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>table</title>
    <style>
        .content {
            display: table;
            padding: 10px;
            border: 2px solid #999;
            width:20%;
        }
        .left-box {
            float: left;
            margin-right: 10px;
        }
        .right-box {
            display: table-cell;
            padding: 10px;
            width: 3000px;  /*右側自適應*/
            vertical-align: top;
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
    <div class="content">
            <div class="left-box">
                <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563504355842&di=38efab5b4e8d2d2546238af82ce055d9&imgtype=0&src=http%3A%2F%2Fimg.9ku.com%2Fgeshoutuji%2Fsingertuji%2F1%2F15169%2F15169_1.jpg" width="70">
            </div>
            <div class="right-box">...</div>
    </div>
</body>
</html>

 

效果如下:
    

左邊頭像部分使用了float左浮動屬性,右側使用 display: table-cell則實現了兩列自適應布局。

注:我們為一個元素設置了display:table-cell屬性,而不將其父元素設置為display:table-row屬性,瀏覽器會默認創建一個表格行。

5.列表布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>table</title>
    <style>
    .content {
        padding: 10px;
        margin: 10px auto;
        display: table;
        width: 20%;
        border: 2px solid #999;
    }

    .content ul {
      display: table-row;
    }

    .content ul li {
      display: table-cell;
      height: 100px;
      line-height: 100px;
      text-align: center;
      border: 1px solid #ccc;
    }
    </style>
</head>
<body>
    <div class="content">
        <ul>
          <li>1</li>
          <li>2</li>
          <li>3</li>
          <li>4</li>
          <li>5</li>
        </ul>
    </div>
</body>
</html>

效果如下:

      

這類布局常用浮動布局(給每個li加上float:left屬性)實現,但這樣做有明顯不足:

  • 需要清除浮動
  • 不支持不定高列表的浮動

 


免責聲明!

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



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