滚动时,边框将消失.我希望边框显示出来.我怎么能这样做呢?或者,我如何才能仅从滚动部分开始滚动?

enter image description here

Html-表格 struct

  <div class="wrapper">
    <table>
      <thead>
        <th class='fix'>Fixed</th>
        <th>Col 1</th>
        <th>Col 2</th>
        <th>Col 3</th>
        <th>Col 4</th>
        <th>Col 5</th>
        <th class='fix'>Fixed</th>
      </thead>
      <tbody>
        <tr>
          <td class='fix'>First Content</td>
          <td>A1</td>
          <td>A2 (with longer content)</td>
          <td>A3</td>
          <td>A4</td>
          <td>A5</td>
          <td class='fix'>Last Content</td>
        </tr>
      </tbody>
    </table>
  </div>

Css-表格css

.wrapper {
  overflow-x:scroll;
  width:100%; 
}
table {
  table-layout: fixed; 
  width: 100%;
  border-collapse: collapse;
  background: white;
}
thead {
  font-family: arial
}
tr {
  border-bottom: 1px solid #ccc;
}
td, th {
  vertical-align: top;
  text-align: left;
  width:100px;
  padding: 5px;
  border: 1px solid red;
}
.fix {
  position:sticky;
  background: white;
}
.fix:first-child {
  left:0;
  width:120px;
}
.fix:last-child {
  right:0;
  width:120px;
}

玩转代码:https://jsbin.com/marezen/1/edit?html,css,output

推荐答案

为了拥有固定的表格边框,您需要:

从CSS中移除边框-折叠

table {
  table-layout: fixed; 
  width: 100%;
  /*border-collapse: collapse;*/
  background: white;
}

还要添加单元格间距="0"以删除单元格间距

<table cellspacing="0">

与我的解决方案相结合,拥有与您的示例相同的边框:

Css-

.wrapper {
  overflow-x:scroll;
  width:100%; 
}

table {
  table-layout: fixed; 
  width: 100%;
  /* border-collapse: collapse; */
  background: white;
  border-top:1px solid red;
  /* border-left:1px solid red; */
}

thead {
  font-family: arial
}

tr {
  /* border-bottom: 1px solid #ccc; */
}

td, th {
  vertical-align: top;
  text-align: left;
  width:100px;
  padding: 5px;
  /* border: 1px solid red; */
  border-right:1px solid red;
  border-bottom:1px solid red;
}
.fix {
  position:sticky;
  background: white;
  border-left:1px solid red;
}
.fix:first-child {
  left:0;
  width:120px;
}
.fix:last-child {
  right:0;
  width:120px;
}

td:nth-child(6),
th:nth-child(6){
  border-right:none;
}

超文本标记语言

<div class="wrapper">
    <table cellspacing="0">
      <thead>
        <th class='fix'>Fixed</th>
        <th>Col 1</th>
        <th>Col 2</th>
        <th>Col 3</th>
        <th>Col 4</th>
        <th>Col 5</th>
        <th class='fix'>Fixed</th>
      </thead>
      <tbody>
        <tr>
          <td class='fix'>First Content</td>
          <td>A1</td>
          <td>A2 (with longer content)</td>
          <td>A3</td>
          <td>A4</td>
          <td>A5</td>
          <td class='fix'>Last Content</td>
        </tr>
        <tr>
          <td class='fix'>First Content (with longer content)</td>
          <td>B1</td>
          <td>B2</td>
          <td>B3</td>
          <td>B4</td>
          <td>B5</td>
          <td class='fix'>Last Content</td>
        </tr>
        <tr>
          <td class='fix'>First Content</td>
          <td>C1</td>
          <td>C2</td>
          <td>C3</td>
          <td>C4</td>
          <td>C5</td>
          <td class='fix'>Last Content (with longer content)</td>
        </tr>
      </tbody>
    </table>
  </div>

Html相关问答推荐

如何正确使用counter()、counter—increment()和counter—reset()嵌套标题?

你能让Cypress测试为p—fileUpload做文件上传吗?

Tumblr博客:如何修复提交页面和询问页面框列计数问题?

在NzMessageService中传递动态TemplateRef- Angular 15

有没有一种方法可以动态地从网格或Flexbox中取出HTML元素?

使用响应迅速的网格系统,将两列保持在同一行,同时允许更多列用于更大的屏幕

如何将功能附加到嵌入式药剂中的按钮?

高度:适合-内容不拉伸以适合内部长度

Django HTML标记-Merge for Loop with Conditional语句

悬停表格单元格文本时,使元素的字体大小更大,同时保持表格单元格的高度不变

为什么我的 html 对话框在 React 中没有渲染在我的内容之上?

如何从 div 内的图像中删除填充而不从其他所有内容中删除填充?

使用修饰键微调 HTML 输入范围中的值

在DIV上应用梯度模糊未如预期工作

SVG 调整大小而不是溢出

如何使用 CSS Flex 设计一个导航栏,其中一组项目左对齐而另一组项目右对齐?

在中心而不是边界处填充 svg 的背景

使用 tailwind css 将 HTML/CSS/JS 站点转换为 React App

使元素扩展宽度减go margin-right

我的卡片内容无法在 bootstrap 5 中正确居中