用于普通数据单元格。
二、CSS美化表格
2.1 简单样式调整
基础的样式调整可以立即提升表格的视觉效果:
table {
border-collapse: collapse;
width: 100%;
margin: auto;
}
th, td {
padding: 8px;
text-align: left;
border: 1px solid #ccc;
}
th {
background-color: #f2f2f2;
font-weight: bold;
}
border-collapse: collapse;
用于合并表格边框。
width: 100%;
设置表格宽度为100%。
margin: auto;
使表格居中显示。
padding
设置单元格内边距。
text-align: left;
设置文本对齐方式为左对齐。
border
设置边框样式。
background-color
设置背景颜色。
font-weight: bold;
设置字体加粗。
2.2 高级样式调整
通过CSS3的高级特性,可以进一步美化表格:
th {
background-color: #4CAF50;
color: white;
font-size: 18px;
font-family: Arial, sans-serif;
}
th:hover {
background-color: #45a049;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
tr:hover {
background-color: #ddd;
}
background-color
设置背景颜色。
color
设置文字颜色。
font-size
设置字体大小。
font-family
设置字体类型。
:hover
伪类用于鼠标悬停时的样式变化。
nth-child(even)
伪类选择器用于选择偶数行。
nth-child(odd)
伪类选择器用于选择奇数行。
三、响应式设计
为了适应不同屏幕尺寸,可以使用CSS3的媒体查询来实现响应式设计:
@media screen and (max-width: 600px) {
table {
display: block;
overflow-x: auto;
}
th, td {
display: block;
width: 100%;
}
th {
background-color: #4CAF50;
color: white;
font-size: 18px;
font-family: Arial, sans-serif;
}
th:hover {
background-color: #45a049;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
tr:hover {
background-color: #ddd;
}
}
@media
媒体查询用于设置特定屏幕尺寸下的样式。
display: block;
设置元素为块级元素。
overflow-x: auto;
设置水平滚动条。
width: 100%;
设置元素宽度为100%。
四、总结
通过以上CSS3表头设计技巧,您可以轻松打造出时尚美观的网页标题。在实际应用中,可以根据需求灵活运用这些技巧,为您的网页增添更多亮点。
|