*初级小白*~ 2022-11-14 17:58 采纳率: 97.9%
浏览 34
已结题

css表格如何修改表头边框

第一张图片没有向下滚动,表格表头边框是白色,第二种图片表格向下滚动,表格表头边框变成红色,向下滚动表格,如何让表格表头的边框始终保持白色

img

img

<template>
  <div class="container">
    <table>
      <thead>
        <tr>
          <th>序号</th>
          <th>标题1</th>
          <th>标题2</th>
          <th>标题3</th>
          <th v-for="i in 30">
            {{ i + i + i }}
          </th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in 100" :key="index">
          <td>1</td>
          <td>内容1</td>
          <td>内容2</td>
          <td>内容3</td>
          <td v-for="d in 30">
            {{ d + d + d }}
          </td>
        </tr>
      </tbody>
    </table>
  </div>

</template>
<script>
import { defineComponent, ref,reactive } from 'vue';

export default defineComponent({
  setup() {

    return {

    };
  },
});
</script>
<style scoped lang="scss">
.container {
  width: 600px;
  height: 400px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  overflow: scroll;
}

table {
  table-layout: fixed;
  border: none;
  border-collapse: collapse;
  background: white;
  text-align: center;

  thead{
   tr{
     color: white;
     height: 70px;
     th{
       background-color: #CCE8EB;
       min-width: 100px;
       position:-webkit-sticky; position:sticky;
       top:0;
       z-index: 3;
       border: 1px solid #fff;
     }
     th:nth-of-type(1) {
       background-color: #269191;
       left:0;
       z-index:4;
     }
     th:nth-of-type(2){
       background-color: #269191;
       left:100px;
       z-index:4;
     }
     th:nth-of-type(3){
       background-color: #269191;
       left:200px;
       z-index:4;
     }
     th:nth-of-type(4){
       background-color: #269191;
       left:300px;
       z-index:4;
     }
   }
 }
 tbody{
   tr{
     height: 50px;
     color: green;
     td{
       min-width: 100px;
       position:-webkit-sticky; position:sticky;
       border: 1px solid red;
     }
     td:nth-of-type(1){
       background-color: #CCE8EB;
       font-weight: bold;
       left: 0;
       z-index:3;
     }
     td:nth-of-type(2){
       background-color: #CCE8EB;
       left: 100px;
       z-index:3;
     }
     td:nth-of-type(3){
       background-color: #CCE8EB;
       left: 200px;
       z-index:3;
     }
     td:nth-of-type(4){
       background-color: #CCE8EB;
       box-shadow: 5px 5px 5px #0078A855;
       left: 300px;
       z-index:3;
     }
   }
 }

}
</style>


  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2022-11-14 18:13
    关注

    border-collapse: collapse;的问题,边框塌陷了。

    The problem occurs because of the use of border-collapse: collapse. When browsers collapse the borders, the top and bottom border on the <th> must be getting applied to surrounding elements—the top border to the <table> and the bottom border to the following <tr>.
    
    If you use border-collapse: separate and fashion your borders to sit on one side, the borders will truly attach to the <th>, stay fixed as expected, and appear collapsed.
    
    

    下面有2种解决办法

        table th {
            /* Apply both top and bottom borders to the <th> */
            border-top: 1px solid #f00;
            border-bottom: 1px solid #f00;
            border-right: 1px solid #f00;
        }
    
        table td {
            /* For cells, apply the border to one of each side only (right but not left, bottom but not top) */
            border-bottom: 1px solid;
            border-right: 1px solid;
        }
    
        table th:first-child,
        table td:first-child {
            /* Apply a left border on the first <td> or <th> in a row */
            border-left: 1px solid;
        }
    
    
    

    伪元素来设置边框

    th:after,
    th:before {
      content: '';
      position: absolute;
      left: 0;
      width: 100%;
    }
    
    th:before {
      top: -1px;
      border-top: 1px solid red;
    }
    
    th:after {
      bottom: -1px;
      border-bottom: 2px solid red;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月23日
  • 已采纳回答 11月15日
  • 创建了问题 11月14日

悬赏问题

  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆