您好,登錄后才能下訂單哦!
如何搭建CSSdisplay屬性的table表格布局,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
項目改造中遇到DIV+CSS實現的table,新需求需要在表格使用單元格合并,網上調查返現CSS display:table實現的table表格,沒有單元格的屬性和樣式,經過一番思考,曲折現實了單元格的合并,即采用正行嵌套一個單獨的display:table的DIV,然后在嵌套的表格DIV內部通過控制行列數和行列的高度,實現單元格合并。個人建議全新實現使用<table> HTML標簽即可
一、CSS display屬性的表格布局相關屬性的解釋:
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>)
二、示例代碼
1、普通表格
XML/HTML Code復制內容到剪貼板
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>display普通表格</title> <style type="text/css"> .table, .table * {margin: 0 auto; padding: 0;font-size: 14px;font-family: Arial, 宋體, Helvetica, sans-serif;} .table {display: table; width: 80%; border-collapse: collapse;} .table-tr {display: table-row; height: 30px;} .table-th {display: table-cell;font-weight: bold;height: 100%;border: 1px solid gray;text-align: center;vertical-align: middle;background-color:#E5E5E5;} .table-td {display: table-cell; height: 100%;border: 1px solid gray; text-align: center;vertical-align: middle;} </style> </head> <body> <div class="table"> <div class="table-tr"> <div class="table-th">省份/直轄市</div> <div class="table-th">GDP(億元)</div> <div class="table-th">增長率</div> </div> <div class="table-tr"> <div class="table-td">廣東</div> <div class="table-td">72812</div> <div class="table-td">8.0%</div> </div> <div class="table-tr"> <div class="table-td">河南</div> <div class="table-td">37010</div> <div class="table-td">8.3%</div> </div> <div class="table-tr"> <div class="table-td">江蘇</div> <div class="table-td">70116</div> <div class="table-td">8.5%</div> </div> </div> </body> </html>
運行效果
2、列合并實現表格
實現思路:基于display:table的表格實現,沒有<table>的rowspan和colspan單元格合并的實現,所以曲折實現,將表格每行單獨嵌套一個獨立的表格,這樣在嵌套的獨立表格內部,單元格合并就能通過控制嵌套表格的行數和列數以及單元格的寬高來實現
XML/HTML Code復制內容到剪貼板
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>基于display列合并表格</title> <style type="text/css"> .table, .table * {margin: 0 auto; padding: 0;font-size: 14px;font-family: Arial, 宋體, Helvetica, sans-serif;} .table {display: table; width: 80%; border-collapse: collapse;} .table-tr {display: table-row; height: 30px;} .table-th {display: table-cell;font-weight: bold;height: 100%;border: 1px solid gray;text-align: center;vertical-align: middle;background-color:#E5E5E5;} .table-td {display: table-cell; height: 100%;} .sub-table {width: 100%;height: 100%;display: table;} .sub-table-tr {display: table-row; height: 100%;} .sub-table-td {display: table-cell; height: 100%;border: 1px solid gray; text-align: center;vertical-align: middle;} </style> </head> <body> <div class="table"> <div class="table-tr"> <div class="table-td"> <div class="sub-table"> <div class="sub-table-tr"> <div class="table-th" style="width: 40%;">省份/直轄市</div> <div class="table-th" style="width: 30%;">GDP(億元)</div> <div class="table-th" style="width: 30%;">增長率</div> </div> </div> </div> </div> <div class="table-tr"> <div class="table-td"> <div class="sub-table"> <div class="sub-table-tr"> <div class="sub-table-td" style="width: 40%;">廣東</div> <div class="sub-table-td" style="width: 30%;">72812</div> <div class="sub-table-td" style="width: 30%;">8.0%</div> </div> </div> </div> </div> <div class="table-tr"> <div class="table-td"> <div class="sub-table"> <div class="sub-table-tr"> <div class="sub-table-td" style="width: 40%;">河南</div> <div class="sub-table-td" style="width: 30%;">37010</div> <div class="sub-table-td" style="width: 30%;">8.3%</div> </div> </div> </div> </div> <div class="table-tr"> <div class="table-td"> <div class="sub-table"> <div class="sub-table-tr"> <div class="sub-table-td" style="width: 40%;">江蘇</div> <div class="sub-table-td" style="width: 30%;">70116</div> <div class="sub-table-td" style="width: 30%;">8.5%</div> </div> </div> </div> </div> <div class="table-tr"> <div class="table-td"> <div class="sub-table"> <div class="sub-table-tr"> <div class="sub-table-td" style="width: 70%;">各省/直轄市GDP平均增長率</div> <div class="sub-table-td" style="width: 30%;">8.26%</div> </div> </div> </div> </div> </div> </body> </html>
運行效果
3、行合并表格
行合并的實現思路:與列合并的實現思路類似,將有單元格合并的列單獨嵌套一個display為table的DIV,高度=單行高*單元格合并數目的倍數,同行的其他列同樣均單獨嵌套DIV,實例代碼如下
XML/HTML Code復制內容到剪貼板
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>基于display的行合并表格</title> <style type="text/css"> .table, .table * {margin: 0 auto; padding: 0;font-size: 14px;font-family: Arial, 宋體, Helvetica, sans-serif;} .table {display: table; width: 80%; border-collapse: collapse;} .table-tr {display: table-row; height: 30px;} .table-th {display: table-cell;font-weight: bold;height: 100%;border: 1px solid gray;text-align: center;vertical-align: middle;background-color:#E5E5E5;} .table-td {display: table-cell; height: 100%;} .sub-table {width: 100%;height: 100%;display: table;} .sub-table-tr {display: table-row; height: 100%;} .sub-table-td {display: table-cell; height: 100%;border: 1px solid gray; text-align: center;vertical-align: middle;} </style> </head> <body> <div class="table"> <div class="table-tr"> <div class="table-td"> <div class="sub-table"> <div class="sub-table-tr"> <div class="table-th" style="width: 40%;">省份/直轄市</div> <div class="table-th" style="width: 30%;">GDP(億元)</div> <div class="table-th" style="width: 30%;">增長率</div> </div> </div> </div> </div> <div class="table-tr"> <div class="table-td"> <div class="sub-table"> <div class="sub-table-tr"> <div class="sub-table-td" style="width: 40%;">廣東</div> <div class="sub-table-td" style="width: 30%;">72812</div> <div class="sub-table-td" style="width: 30%;">8.0%</div> </div> </div> </div> </div> <div class="table-tr" style="height:60px;"> <div class="table-td"> <div class="sub-table"> <div class="sub-table-tr"> <div class="sub-table-td" style="width: 40%; border: none;"> <div class="sub-table"> <div class="sub-table-tr" style="height:50%;"> <div class="sub-table-td" style="width: 100%; height:50%;"> 河南 </div> </div> <div class="sub-table-tr" style="height:50%;"> <div class="sub-table-td" style="width: 100%; height:50%;"> 江蘇 </div> </div> </div> </div> <div class="sub-table-td" style="width: 30%;border: none;"> <div class="sub-table"> <div class="sub-table-tr" style="height:50%;"> <div class="sub-table-td" style="width: 100%; height:50%;"> 37010 </div> </div> <div class="sub-table-tr" style="height:50%;"> <div class="sub-table-td" style="width: 100%; height:50%;"> 70116 </div> </div> </div> </div> <div class="sub-table-td" style="width: 30%;border: none;"> <div class="sub-table"> <div class="sub-table-tr"> <div class="sub-table-td" style="width: 100%;"> 8.4% </div> </div> </div> </div> </div> </div> </div> </div> <div class="table-tr"> <div class="table-td"> <div class="sub-table"> <div class="sub-table-tr"> <div class="sub-table-td" style="width: 70%;">各省/直轄市GDP平均增長率</div> <div class="sub-table-td" style="width: 30%;">8.26%</div> </div> </div> </div> </div> </div> </body> </html>
運行效果:
看完上述內容,你們掌握如何搭建CSSdisplay屬性的table表格布局的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。