html - 如何在 CSS 中制作带有表格和单元格宽度的固定标题表格?

标签 html css

<分区>

我找到了固定头表的以下解决方案:

table {
  width: 450px;
  border-collapse: collapse;
}

thead {
  display: block;
  width: 450px;
  overflow: auto;
  color: #fff;
  background: #000;
}

tbody {
  display: block;
  width: 450px;
  height: 200px;
  background: pink;
  overflow: auto;
}

th,
td {
  padding: 0;
  text-align: left;
  vertical-align: top;
  border-left: 1px solid #fff;
}
<table>
  <thead>
    <tr>
      <th style="width:200px">Header 1</th>
      <th style="width:200px">Header 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="width:200px">Cell 1, Row 1</td>
      <td style="width:200px">Cell 2, Row 1</td>
    </tr>
    <tr>
      <td>Cell 1, Row 2</td>
      <td>Cell 2, Row 2</td>
    </tr>
    <tr>
      <td>Cell 1, Row 3</td>
      <td>Cell 2, Row 3</td>
    </tr>
    <tr>
      <td>Cell 1, Row 4</td>
      <td>Cell 2, Row 4</td>
    </tr>
    <tr>
      <td>Cell 1, Row 5</td>
      <td>Cell 2, Row 5</td>
    </tr>
    <tr>
      <td>Cell 1, Row 6</td>
      <td>Cell 2, Row 6</td>
    </tr>
    <tr>
      <td>Cell 1, Row 7</td>
      <td>Cell 2, Row 7</td>
    </tr>
    <tr>
      <td>Cell 1, Row 8</td>
      <td>Cell 2, Row 8</td>
    </tr>
    <tr>
      <td>Cell 1, Row 9</td>
      <td>Cell 2, Row 9</td>
    </tr>
    <tr>
      <td>Cell 1, Row 10</td>
      <td>Cell 2, Row 10</td>
    </tr>
    <tr>
      <td>Cell 1, Row 11</td>
      <td>Cell 2, Row 11</td>
    </tr>
    <tr>
      <td>Cell 1, Row 12</td>
      <td>Cell 2, Row 12</td>
    </tr>
  </tbody>
</table>

此解决方案的问题在于它以像素为单位为表格和单元格设置了固定宽度。但是,我需要 <table style="width:100%"><td style="width:X%"> .如何在 CSS 中仅使用一个 html 表且不使用 JS 来制作固定的表头表和单元格宽度百分比?

最佳答案

检查在不同问题上发布的此解决方案。 Fixed Header solution .

或者直接试试这个 Fiddle

标题内容放在“th”中的“div”内。此 div 具有绝对位置和其他样式以将标题显示为固定。

html, body{
  margin:0;
  padding:0;
  height:100%;
}
section {
  position: relative;
  border: 1px solid #000;
  padding-top: 37px;
  background: #500;
}
section.positioned {
  position: absolute;
  top:100px;
  left:100px;
  width:800px;
  box-shadow: 0 0 15px #333;
}
.container {
  overflow-y: auto;
  height: 200px;
}
table {
  border-spacing: 0;
  width:100%;
}
td + td {
  border-left:1px solid #eee;
}
td, th {
  border-bottom:1px solid #eee;
  background: #ddd;
  color: #000;
  padding: 10px 25px;
}
th {
  height: 0;
  line-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  color: transparent;
  border: none;
  white-space: nowrap;
}
th div{
  position: absolute;
  background: transparent;
  color: #fff;
  padding: 9px 25px;
  top: 0;
  margin-left: -25px;
  line-height: normal;
  border-left: 1px solid #800;
}
th:first-child div{
  border: none;
}
<section class="">
  <div class="container">
    <table>
      <thead>
        <tr class="header">
          <th>
            Table attribute name
            <div>Table attribute name</div>
          </th>
          <th>
            Value
            <div>Value</div>
          </th>
          <th>
            Description
            <div>Description</div>
          </th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>align</td>
          <td>left, center, right</td>
          <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
        </tr>
        <tr>
          <td>bgcolor</td>
          <td>rgb(x,x,x), #xxxxxx, colorname</td>
          <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td>
        </tr>
        <tr>
          <td>border</td>
          <td>1,""</td>
          <td>Specifies whether the table cells should have borders or not</td>
        </tr>
        <tr>
          <td>cellpadding</td>
          <td>pixels</td>
          <td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td>
        </tr>
        <tr>
          <td>cellspacing</td>
          <td>pixels</td>
          <td>Not supported in HTML5. Specifies the space between cells</td>
        </tr>
        <tr>
          <td>frame</td>
          <td>void, above, below, hsides, lhs, rhs, vsides, box, border</td>
          <td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td>
        </tr>
        <tr>
          <td>rules</td>
          <td>none, groups, rows, cols, all</td>
          <td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td>
        </tr>
        <tr>
          <td>summary</td>
          <td>text</td>
          <td>Not supported in HTML5. Specifies a summary of the content of a table</td>
        </tr>
        <tr>
          <td>width</td>
          <td>pixels, %</td>
          <td>Not supported in HTML5. Specifies the width of a table</td>
        </tr>
      </tbody>
    </table>
  </div>
</section>

关于html - 如何在 CSS 中制作带有表格和单元格宽度的固定标题表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49715524/

相关文章:

javascript - 在 Html 应用程序中,使用 javascript,如何获取图像在屏幕上的位置并将其放入文本文档中?

html - 使 css 网格与微软浏览器兼容

css - Internet Explorer 8 不应用内联显示并正确阻止

css - 悬停填充按钮背景从下到上,文本颜色从下到上

javascript - 单击菜单 <li> 并生成其他子菜单

html - 嵌套的 li 样式不适用于 IE。

html - 使用 Twitter Bootstrap 连续 4 列

javascript - Knockoutjs单选按钮无法选择默认选中

html - 如何防止叠加的 div 在某些分辨率下产生间隙? (谷歌时代精神)

php - 如果 div id "arrow"存在,使其表行具有类 "odd0"