display的table和table-cell通常状况下用的很少,因此不多有人去关注它,但他们两个联手起来会给你惊喜!html
当两个或者两个以上标签一块儿使用显示在同一行时,之前经常使用的是float、position进行布局,在高版本的浏览器可使用flex、grid进行布局。无心中发现使用display:table-cell也是一个很好用的自适应布局,本文就display:table-cell作学习总结。浏览器
display:table-cell指让标签元素以表格单元格的形式呈现,使元素相似于td标签。IE8+及现代版本的浏览器都支持此属性,IE6/7不支持(可用其余方法实现相似效果)。一样,display:table-cell属性也会被float,position:absolute等属性破坏效果,应避免同时使用。ruby
值 | 描述 |
---|---|
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是如何让多行文字垂直居中的。虽然感受用的很少,可是在某些时候仍是挺管用的,以下:学习
<!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>
效果以下:flex
设置了display:table-cell的元素:spa
<!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>
效果以下:
code
<!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>
效果以下:
htm
<!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>
效果以下:
blog
左边头像部分使用了float左浮动属性,右侧使用 display: table-cell则实现了两列自适应布局。
注:咱们为一个元素设置了display:table-cell属性,而不将其父元素设置为display:table-row属性,浏览器会默认建立一个表格行。
<!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属性)实现,但这样作有明显不足: