等,用于构建基本的表格结构。
创建基本表格
| 标题1 |
标题2 |
标题3 |
| 内容1 |
内容2 |
内容3 |
| 内容4 |
内容5 |
内容6 |
表格属性
border
: 设置表格边框宽度。
cellspacing
: 设置单元格间距。
cellpadding
: 设置单元格内边距。
CSS3表格设计
CSS3为表格设计提供了丰富的样式,如边框、背景、颜色、阴影等。
边框样式
table {
border-collapse: collapse;
}
th, td {
border: 1px solid #000;
}
背景与颜色
th {
background-color: #f2f2f2;
color: #333;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
阴影与渐变
th {
text-shadow: 1px 1px 2px #ccc;
}
table {
background-image: linear-gradient(to right, #f2f2f2, #fff);
}
HTML5与CSS3表格高级技巧
响应式表格
@media screen and (max-width: 600px) {
table, thead, tbody, th, td, tr {
display: block;
}
thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
tr {
border: 1px solid #ccc;
}
td {
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
}
td:before {
position: absolute;
top: 6px;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
content: attr(data-title);
}
}
表格排序与筛选
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById(“myTable”);
switching = true;
dir = “asc”;
while (switching) {
switching = false;
rows = table.rows;
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
if (dir == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
} else if (dir == “desc”) {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
switchcount++;
} else {
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}
}
}
总结
通过HTML5与CSS3,我们可以打造出精美易用的表格,提升网页的视觉效果。掌握这些技巧,将有助于您在网页设计中更好地展示数据和信息。
|