css设置表格样式

admin 29 0

在CSS中,你可以使用各种属性来设置表格的样式,以下是一些常见的CSS属性和它们的用法,这些属性可以帮助你自定义表格的外观:

1. **设置表格宽度和高度**:

table {
  width: 100%;
  height: 300px;
}

2. **设置表格边框**:

table, th, td {
  border: 1px solid black;
}

3. **设置表格边框间距**:

table {
  border-collapse: separate;
  border-spacing: 10px;
}

4. **设置表头样式**:

th {
  background-color: #4CAF50;
  color: white;
  text-align: left;
  padding: 10px;
}

5. **设置单元格样式**:

td {
  padding: 10px;
  text-align: center;
}

6. **设置表格背景色**:

table {
  background-color: #f2f2f2;
}

7. **设置表格字体**:

table {
  font-family: Arial, sans-serif;
  font-size: 14px;
}

8. **设置表格鼠标悬停效果**:

table:hover {
  background-color: #ddd;
}

9. **设置表格行交替颜色**:

tr:nth-child(even) {
  background-color: #f2f2f2;
}

10. **设置表格响应式布局**:

@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 #ccc;
    position: relative;
    padding-left: 50%;
  }
  td:before {
    position: absolute;
    top: 6px;
    left: 6px;
    width: 45%;
    padding-right: 10px;
    white-space: nowrap;
  }
  td:nth-of-type(1):before { content: "姓名"; }
  td:nth-of-type(2):before { content: "邮箱"; }
  td:nth-of-type(3):before { content: "电话"; }
}

你可以将这些CSS样式添加到你的样式表中,或者直接在HTML文件的``标签内定义,记得将CSS选择器与你的HTML元素匹配,以便正确应用样式。