1

谁能帮我指出哪些样式是构建这个固定标题表的关键?这是我发现有用的唯一示例,但我不知道哪些是最重要的样式使这种情况发生。

我也很好奇为什么需要一个 DIV 和填充来实现这一点?

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


<style>
    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;
    }
</style>
4

1 回答 1

1

你的问题有点含糊。您发布的代码没有固定的标头,因为它们position: fixed在元素上使用。该代码基本上是一个常规的旧表,其中包含数据。“棘手”的部分是用户创建了一个位于现有标题部分的新标题部分,thead这就是为什么您会看到这样的代码

<th>
     Table attribute name
     <div>Table attribute name</div>
</th>

表属性名称”文本被隐藏,其中th {color: transparent;}的文本div显示在其位置。

至于那是div class="container"使用以下方法创建可滚动容器:

.container {
    overflow-y: auto;
    height: 200px;
}

我相信用户正在使用tableanddiv组合,这样他们就可以在下面有一个带有可滚动数据的标题,并且它们都会正确对齐。

如果您想要此代码的真正简化版本而不使用表格,您可以使用:

HTML

<div class="wrap">
    <div class="top">Title here</div>
    <div class="scroll">
        <div class="text">All your base are belong to us</div>
    </div>
</div>

CSS

 html, body{
      margin:0;
      padding:0;
      height:100%;
}
.wrap {
    margin: 0 auto;
    width: 600px;
}
.top {
    border: 1px solid red;
    background: lightgray;
    line-height: 50px;
    text-align: center;
}
.scroll {
    background: pink;
    height: 200px;
    overflow-y: auto;
}
.text {
    height: 1000px;
}

这里是 JS.Fiddle如果你想玩它。希望有帮助。

于 2015-09-03T00:57:12.300 回答