一、簡介
在網頁設計過程中,表格往往是一個重要的布局工具,但是,使用原生的HTML表格標籤往往會破壞網頁的語義結構,影響SEO。而使用CSS表格樣式則可以在不破壞語義的前提下實現優美的布局效果。
二、使用方法
1. 使用標籤代替表格標籤
<div class="table"> <div class="row"> <div class="cell">單元格1</div> <div class="cell">單元格2</div> </div> <div class="row"> <div class="cell">單元格3</div> <div class="cell">單元格4</div> </div> </div>
2. 使用CSS樣式設置表格樣式
.table{ display: table; width: 100%; border-collapse: collapse; } .row{ display: table-row; } .cell{ display: table-cell; border: 1px solid #ddd; padding: 10px; }
三、樣式優化
1. 表頭加粗
.table thead{ font-weight: bold; }
2. 隔行變色
.table tbody tr:nth-child(even){ background-color: #f9f9f9; }
3. 懸浮效果
.cell:hover{ background-color: #f5f5f5; }
四、響應式布局
使用CSS媒體查詢可以實現響應式布局,讓表格在不同的屏幕尺寸下呈現不同的樣式。
@media screen and (max-width: 767px){ .table{ display: block; } .row{ display: block; margin-bottom: 10px; } .cell{ display: block; border: none; position: relative; padding-left: 50%; height: 30px; line-height: 30px; } .cell:before{ content: attr(data-title); position: absolute; left: 0; top: 0; width: 50%; padding-left: 10px; font-weight: bold; } }
五、總結
使用CSS表格樣式優化網頁布局可以讓網頁布局更加美觀、語義化、響應式。通過樣式優化可以讓表格更加易於閱讀和使用。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/238842.html