当前位置:首页 > CSS

网页表格制作css代码

2026-02-26 22:15:04CSS

基础表格样式

使用CSS为HTML表格添加基础样式,包括边框、间距和背景色。以下代码适用于大多数简单表格场景:

<style>
  table {
    width: 100%;
    border-collapse: collapse;
    margin: 20px 0;
    font-family: Arial, sans-serif;
  }

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

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

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

  tr:hover {
    background-color: #f1f1f1;
  }
</style>

响应式表格设计

对于移动设备友好的响应式表格,当屏幕尺寸较小时转为垂直布局:

@media screen and (max-width: 600px) {
  table {
    border: 0;
  }

  table thead {
    display: none;
  }

  table tr {
    border-bottom: 2px solid #ddd;
    display: block;
    margin-bottom: 15px;
  }

  table td {
    border-bottom: 1px dotted #ccc;
    display: block;
    text-align: right;
    padding-left: 50%;
    position: relative;
  }

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

斑马条纹效果

创建交替行颜色的表格,增强可读性:

table.striped {
  border: none;
}

table.striped tr:nth-child(odd) {
  background-color: #f8f8f8;
}

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

table.striped th {
  background-color: #4CAF50;
  color: white;
}

table.striped td {
  border-bottom: 1px solid #ddd;
}

悬停高亮效果

为表格行添加悬停交互效果,提升用户体验:

table.hover-effect tr {
  transition: background-color 0.3s ease;
}

table.hover-effect tr:hover {
  background-color: #e3f2fd;
  cursor: pointer;
}

table.hover-effect th {
  background-color: #2196F3;
  color: white;
}

table.hover-effect td {
  border-bottom: 1px solid #ddd;
  padding: 12px 15px;
}

紧凑型表格

适合需要显示大量数据的紧凑布局:

table.condensed {
  font-size: 0.9em;
}

table.condensed th, 
table.condensed td {
  padding: 6px 8px;
}

table.condensed th {
  background-color: #555;
  color: white;
}

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

圆角边框表格

为表格添加现代风格的圆角设计:

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

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

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

table.rounded tr:last-child td:first-child {
  border-bottom-left-radius: 8px;
}

table.rounded tr:last-child td:last-child {
  border-bottom-right-radius: 8px;
}

固定表头表格

长表格滚动时保持表头固定:

网页表格制作css代码

table.fixed-header {
  position: relative;
}

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

table.fixed-header tbody {
  display: block;
  overflow-y: auto;
  max-height: 400px;
}

table.fixed-header th, 
table.fixed-header td {
  width: auto;
}

标签: 表格代码
分享给朋友:

相关文章

css好看的表格制作

css好看的表格制作

CSS表格美化方法 边框与间距优化 使用border-collapse合并边框,避免双线边框。设置padding增加单元格内边距,提升可读性。 table { border-collapse:…

vue扫码登录代码实现

vue扫码登录代码实现

Vue 扫码登录实现方案 准备工作 确保项目已安装 Vue 2/3 及相关依赖,需要后端提供生成二维码和验证扫码状态的接口。 生成二维码 使用 qrcode 库生成登录二维码,需调用后端接口获取唯一…

怎么制作css表格

怎么制作css表格

制作CSS表格的方法 使用HTML表格标签结合CSS样式 通过HTML的<table>、<tr>、<td>等标签创建表格结构,再使用CSS美化样式。例如: <…

vue实现的表格

vue实现的表格

Vue 实现表格的方法 使用原生 HTML 表格 在 Vue 模板中可以直接使用 HTML 的 <table> 标签创建表格。数据可以通过 v-for 指令动态渲染。 <templ…

vue实现网站代码

vue实现网站代码

Vue 实现网站代码的基本步骤 安装 Vue.js 通过 npm 或 CDN 引入 Vue.js。使用 npm 安装:npm install vue。或者直接在 HTML 文件中引入 Vue CDN:…

vue实现表格序号

vue实现表格序号

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