当前位置:首页 > 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;
  }
}

斑马条纹效果

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

网页表格制作css代码

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;
}

紧凑型表格

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

网页表格制作css代码

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;
}

固定表头表格

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

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;
}

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

相关文章

vue实现子表格

vue实现子表格

实现子表格的基本思路 在Vue中实现子表格通常需要结合组件化思想和动态渲染技术。常见的场景是在主表格的行内嵌套子表格,展开主表行时显示对应的子表数据。 使用el-table实现子表格(Element…

使用vue实现表格

使用vue实现表格

使用 Vue 实现表格 Vue.js 提供了灵活的方式来实现表格功能,可以通过组合 v-for 指令和动态数据绑定快速构建表格。以下是几种常见的实现方法: 基础表格实现 通过 v-for 遍历数组数…

js实现代码雨

js实现代码雨

实现代码雨效果 使用HTML5 Canvas和JavaScript可以轻松实现经典的代码雨效果。以下是完整的实现代码和说明: HTML结构 <!DOCTYPE html> <htm…

js实现轮播代码

js实现轮播代码

基础轮播实现 使用HTML、CSS和JavaScript创建一个简单的轮播效果。HTML部分定义轮播容器和图片元素。 <div class="carousel"> <div c…

vue表格实现导出

vue表格实现导出

使用 vue-json-excel 插件 安装插件: npm install vue-json-excel --save 在组件中引入并注册: import JsonExcel from 'vue…

vue实现表格树

vue实现表格树

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