Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make fixed header table with table and cell width in percents in CSS? [duplicate]

Tags:

html

css

I have found the following solution for fixed header table:

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>

The problem with this solution is that it sets fixed width both for table and cells in pixels. However, I need <table style="width:100%"> and <td style="width:X%">. How to make fixed header table with table and cell width in percents in CSS using only one html table and without JS?

like image 385
Pavel_K Avatar asked Apr 08 '18 07:04

Pavel_K


3 Answers

Check this solution posted on a different question. Fixed Header solution.

Or directly try out this Fiddle

The header content is placed inside a "div" in the "th". This div has position absolute and other styling to show the header as fixed.

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>
like image 117
Tom Avatar answered Oct 16 '22 11:10

Tom


Well, here is my solution.

For HTML you just need to wrap in two div wrappers and in the thead th you need to have some code duplication - I will shortly explain why.

You need the .table-outer wrapper in order to create the necessary padding-top and position:relative that will accommodate the fixed table header.

The .table-wrap wrapper is used to scroll the table contents. One wrapper alone could not be used to absolutely position the header because it would scroll with the parent.

Now I have duplicated and wrapped the th contents inside a p and a span. The span element is used to style and position the header properly. The p might seem unnecessary at first, but here is what it does - it ensures that the th expands properly in cases where th content has content of larger width than the largest td content.

HTML

<div class="table-outer">
  <div class="table-wrap">
    <table>
      <thead>
        <tr>
          <th>
            <p>Header 1</p><span>Header 1</span></th>
          <th>
            <p>Header 1</p><span>Header 2</span></th>
        </tr>
      </thead>
      <tbody>
       ...
      </tbody>
    </table>
  </div>
</div>

CSS

.table-outer {
  padding-top: 20px;
  position: relative;
  height: 100px;
  overflow: hidden;
}

.table-wrap {
  overflow-x: auto;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
thead th p {
  overflow: hidden;
  height: 0;
  margin: 0;
  padding: 0;
}

thead th span {
  position: absolute;
  top: 0;
  display: block;
  background: #000;
  color: #fff;
  width: 100%;
}

Here is a working fiddle, along with a small update that shows different content within the table: https://jsfiddle.net/8fa1w922/ https://jsfiddle.net/8fa1w922/2/

like image 3
scooterlord Avatar answered Oct 16 '22 11:10

scooterlord


Elements with some kind of display:table can't have scrollbars, but you could create another tabel inside a <div> which could do the overflow part.

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

#scroll {
  overflow-y: auto;
  height: 200px;
  width: fit-content;
}

thead {
  color: #fff;
  background: #000;
}

tbody {
  background: pink;
}

th,
td {
  padding: 0;
  text-align: left;
  vertical-align: top;
  border-left: 1px solid #fff;
}
<table>
  <thead>
    <tr>
      <th style="width:40%">Header 1</th>
      <th style="width:60%">Header 2</th>
    </tr>
  </thead>
</table>
<div id="scroll">
  <table>
    <tbody>
      <tr>
        <td style="width:40%">Cell 1, Row 1</td>
        <td style="width:60%">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>
      <tr>
        <td>Cell 1, Row 12</td>
        <td>Cell 2, Row 12</td>
      </tr>
      <tr>
        <td>Cell 1, Row 12</td>
        <td>Cell 2, Row 12</td>
      </tr>
      <tr>
        <td>Cell 1, Row 12</td>
        <td>Cell 2, Row 12</td>
      </tr>
    </tbody>
  </table>
like image 2
Sebastian Speitel Avatar answered Oct 16 '22 09:10

Sebastian Speitel