html - 如何使用内部绝对div的所有宽度

标签 html css fixed-header-tables

在此fiddle ,可以看到 header 已经固定了。 我的问题是我无法获得绝对的 <div>里面<th> <th> 的宽度为 100%

设置width: 100%<div>让它使用所有可用的宽度

设置position: relative<th><div>在某个地方我看不到它(看起来它在 <th> 下,甚至使用 z-index 也不会改变任何东西。

注意:没有 JS,只有 HTML/CSS

html, body{
  margin:0;
  padding:0;
  height:100%;
}

section {
  position: relative;
  border: 1px solid #000;
  padding-top: 37px;
  background: #500;
}

.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>
          <th>
            Table attribute name
            <div>Table attribute name</div>
          </th>
          <th>
            Value
            <div>Value</div>
          </th>
           <th>Test<div>
           Test
           </div></th>
          <th>
            Description
            <div>Description</div>
          </th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>align</td>
          <td>left, center, right</td>
          <td></td>
          <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
        </tr>
        ...
      </tbody>
    </table>
  </div>
</section>

我已经尝试过 this但这里的问题是冒号宽度都是相同的,有时会因为某些内容而导致对齐中断

最佳答案

您也不需要添加 divposition:absolute 以及颜色 transparent。您可以使用 thead th 样式上的 position: Sticky 使标题保持全宽。了解更多关于 position-sticky .

请参阅下面的片段:

html, body{
  margin:0;
  padding:0;
  height:100%;
}
section {
  position: relative;
  border: 1px solid #000;
  padding-top: 37px;
  background: #500;
}
.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>
          <th>
            Table attribute name
            <div>Table attribute name</div>
          </th>
          <th>
            Value
            <div>Value</div>
          </th>
           <th>Test<div>
           Test
           </div></th>
          <th>
            Description
            <div>Description</div>
          </th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>align</td>
          <td>left, center, right</td>
          <td></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>

你可以测试一下here还有..

关于html - 如何使用内部绝对div的所有宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57255469/

相关文章:

html - Bootstrap - 下拉菜单和移动菜单不起作用

css - Twitter Bootstrap 内联表单间距

html - 在 CSS 中自动缩放字体大小

jquery - 带有固定横幅 div 的固定表头

php - 什么是多部分二进制输入?

css - 使一个 div 可见,另一个不可见

css - 如何固定 html 表中的标题,其中包含带有行跨度和 coll-span 的多行标题

jquery - 固定标题表 - 调整表大小

php - 导航未在我的视频集顶部显示为背景

javascript - 如何在jsp文件中使用javascript添加请求参数