当前位置:首页 > CSS

css怎样制作表格

2026-03-12 08:53:49CSS

CSS 制作表格的方法

使用 CSS 制作表格通常有两种方式:一种是直接使用 HTML 的 <table> 标签结合 CSS 样式,另一种是使用 <div> 或其他元素通过 CSS 的 display: table 系列属性模拟表格布局。

使用 HTML <table> 标签

HTML 原生表格标签结合 CSS 可以快速实现表格布局,适合展示结构化数据。

<table class="custom-table">
  <thead>
    <tr>
      <th>姓名</th>
      <th>年龄</th>
      <th>职业</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>张三</td>
      <td>25</td>
      <td>设计师</td>
    </tr>
    <tr>
      <td>李四</td>
      <td>30</td>
      <td>工程师</td>
    </tr>
  </tbody>
</table>
.custom-table {
  width: 100%;
  border-collapse: collapse; /* 合并边框 */
  margin: 20px 0;
  font-family: Arial, sans-serif;
}

.custom-table th, .custom-table td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}

.custom-table th {
  background-color: #f2f2f2;
  font-weight: bold;
}

.custom-table tr:nth-child(even) {
  background-color: #f9f9f9;
}

.custom-table tr:hover {
  background-color: #f1f1f1;
}

使用 CSS display: table 模拟表格

通过 CSS 的表格布局属性,可以用非 <table> 元素实现表格效果,适合需要更灵活的场景。

<div class="css-table">
  <div class="css-thead">
    <div class="css-tr">
      <div class="css-th">姓名</div>
      <div class="css-th">年龄</div>
      <div class="css-th">职业</div>
    </div>
  </div>
  <div class="css-tbody">
    <div class="css-tr">
      <div class="css-td">张三</div>
      <div class="css-td">25</div>
      <div class="css-td">设计师</div>
    </div>
    <div class="css-tr">
      <div class="css-td">李四</div>
      <div class="css-td">30</div>
      <div class="css-td">工程师</div>
    </div>
  </div>
</div>
.css-table {
  display: table;
  width: 100%;
  border-collapse: collapse;
  margin: 20px 0;
}

.css-thead {
  display: table-header-group;
}

.css-tbody {
  display: table-row-group;
}

.css-tr {
  display: table-row;
}

.css-th, .css-td {
  display: table-cell;
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}

.css-th {
  background-color: #f2f2f2;
  font-weight: bold;
}

.css-tr:nth-child(even) {
  background-color: #f9f9f9;
}

.css-tr:hover {
  background-color: #f1f1f1;
}

响应式表格设计

对于移动端或小屏幕设备,表格可能需要横向滚动或调整布局。

css怎样制作表格

.table-responsive {
  overflow-x: auto;
  max-width: 100%;
}

@media (max-width: 600px) {
  .custom-table, .css-table {
    font-size: 14px;
  }
}

高级样式技巧

  • 斑马纹效果:使用 nth-child(even/odd) 为交替行设置不同背景色。
  • 悬停效果:通过 :hover 伪类增强交互体验。
  • 固定表头:结合 position: sticky 实现滚动时表头固定。
.custom-table thead th {
  position: sticky;
  top: 0;
  z-index: 10;
}

标签: 表格css
分享给朋友:

相关文章

css制作时间轴

css制作时间轴

使用 Flexbox 布局制作时间轴 通过 Flexbox 可以快速实现水平或垂直时间轴。以下是一个垂直时间轴的示例代码: .timeline { display: flex; flex-d…

css制作goole

css制作goole

使用CSS制作Google标志 Google标志由多彩的字母和简洁的字体组成,可以通过CSS的字体属性、颜色渐变和布局来实现。 字体选择 Google标志使用自定义字体"Product Sans",…

css制作靶子

css制作靶子

CSS 制作靶子 使用 CSS 可以轻松创建一个靶子效果,主要通过嵌套的圆形元素和背景色实现。以下是实现方法: HTML 结构 <div class="target"> <d…

css菜单制作

css菜单制作

CSS 菜单制作方法 水平导航菜单 使用 display: inline-block 或 flexbox 可以轻松创建水平导航菜单。以下是一个基本示例: <nav> <ul c…

css 导航栏制作

css 导航栏制作

CSS 导航栏制作方法 水平导航栏 使用 display: inline-block 或 flexbox 创建水平导航栏。设置背景色、内边距和悬停效果增强交互性。 .navbar { back…

css 制作表单

css 制作表单

使用CSS制作表单的基本方法 创建表单时,HTML负责结构,CSS负责样式。表单元素包括输入框、按钮、下拉菜单等,通过CSS可以统一风格并提升用户体验。 HTML结构示例 <form cla…