1

Could anyone help me to point out which styles is the key to build this fixed header table? This is the only example I find out useful, but I can not figure out which are the most important styles make this happen.

Also I am curious why there needs to be a DIV and padding for th td to make this happen?

<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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kuan
  • 11,149
  • 23
  • 93
  • 201
  • I think because it headlines, they can not be reduced. Thats why you table still with some value of `min-width` is equal to width of text plus your paddings/margins. – AleshaOleg Sep 02 '15 at 22:21
  • @AleshaOleg Thanks, could you help to point out which styles is crucial to build this layout? – Kuan Sep 02 '15 at 22:47

1 Answers1

1

Your question is a little vague. The code you posted doesn't have a fixed header in the since that they are using the position: fixed on an element. That code is basically a regular old table with data in it. The "tricky" part is the user creates a new header section that sits over the existing thead which is why you see the code like this

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

The "Table attibute name" text is being hidden with the th {color: transparent;} and the text in the div is showing in its place.

As for the div class="container"thats creating the scrollable container using:

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

I believe the user is using the table and div combo just so they could have a header with scrollable data underneath and it would all be aligned properly.

If you want a real simplified version of this code without using tables you could use this:

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

Here is the JS.Fiddle if you want to play around with it. Hope that helps.

crazymatt
  • 3,266
  • 1
  • 25
  • 41
  • Thanks, let me play with it a little bit first. – Kuan Sep 03 '15 at 15:17
  • Hi, Matt, I check the fiddle. Sadly my boss insists that I need to use instead
    even I already implement
    version like yours. So I wonder if you have a
    version fiddle example?
    – Kuan Sep 03 '15 at 15:29
  • To better help you I would need to know what you are trying to achieve. If you need to use tables with the static header that has multiple columns that match the table body... then I suggest using the code you posted. If you just need one header title and scrollable content underneath then there is an easy way to do this. – crazymatt Sep 03 '15 at 17:19