当前位置:首页 > CSS

css样式制作表格

2026-04-01 22:20:39CSS

CSS 制作表格的基本方法

使用 tabletrtd 等 HTML 标签结合 CSS 样式可以创建美观的表格。以下是一个基础示例:

<table class="styled-table">
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
  </tr>
  <tr>
    <td>Data 1</td>
    <td>Data 2</td>
  </tr>
</table>
.styled-table {
  width: 100%;
  border-collapse: collapse;
  margin: 20px 0;
}

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

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

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

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

响应式表格设计

对于移动设备,可以通过媒体查询调整表格布局:

css样式制作表格

@media screen and (max-width: 600px) {
  .styled-table {
    display: block;
  }

  .styled-table thead, .styled-table tbody, .styled-table th, 
  .styled-table td, .styled-table tr {
    display: block;
  }

  .styled-table thead tr {
    position: absolute;
    top: -9999px;
    left: -9999px;
  }

  .styled-table tr {
    margin-bottom: 15px;
  }

  .styled-table td {
    border: none;
    position: relative;
    padding-left: 50%;
  }

  .styled-table td:before {
    position: absolute;
    left: 6px;
    content: attr(data-label);
    font-weight: bold;
  }
}

表格样式进阶技巧

添加圆角边框和阴影效果:

css样式制作表格

.styled-table {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}

.styled-table th:first-child {
  border-top-left-radius: 8px;
}

.styled-table th:last-child {
  border-top-right-radius: 8px;
}

使用 CSS 变量实现主题切换:

:root {
  --table-header-bg: #4CAF50;
  --table-header-color: white;
  --table-row-hover: #e8f5e9;
}

.styled-table th {
  background-color: var(--table-header-bg);
  color: var(--table-header-color);
}

.styled-table tr:hover {
  background-color: var(--table-row-hover);
}

固定表头和列的实现

对于大型表格,可以固定表头和首列:

.table-container {
  height: 400px;
  overflow: auto;
}

.sticky-header thead th {
  position: sticky;
  top: 0;
  z-index: 10;
}

.sticky-first-col td:first-child, 
.sticky-first-col th:first-child {
  position: sticky;
  left: 0;
  z-index: 5;
  background: white;
  box-shadow: 2px 0 5px rgba(0,0,0,0.1);
}

这些方法可以根据实际需求组合使用,创建出功能完善且美观的表格布局。

标签: 样式表格
分享给朋友:

相关文章

h5实现表格样式

h5实现表格样式

实现H5表格样式的方法 使用HTML和CSS创建基础表格 在HTML中,表格通过<table>标签定义,配合<tr>(行)、<td>(单元格)和<th>…

vue表格实现单选

vue表格实现单选

实现 Vue 表格单选功能 使用 v-model 绑定选中状态 在表格的每一行添加单选按钮或点击行触发选中,通过 v-model 绑定一个变量存储当前选中行的唯一标识(如 id)。 示例代码: &l…

网页表格制作css代码

网页表格制作css代码

基础表格样式 使用CSS为HTML表格添加基础样式,包括边框、间距和背景色: table { width: 100%; border-collapse: collapse; margin…

vue实现表格树

vue实现表格树

Vue 实现表格树的方法 使用递归组件实现树形表格 在 Vue 中可以通过递归组件实现树形表格结构。定义一个组件,该组件可以调用自身来渲染子节点。 <template> &l…

vue实现sku表格

vue实现sku表格

Vue实现SKU表格的方法 SKU(Stock Keeping Unit)表格常用于电商平台展示商品的多属性组合,例如颜色、尺寸等。以下是基于Vue的实现方法: 数据结构设计 SKU的核心是属性组合…

vue实现表格序号

vue实现表格序号

实现表格序号的常见方法 在Vue中为表格添加序号列可以通过多种方式实现,以下是几种典型方案: 使用v-for索引 <template> <table> <…