Search code examples
htmlcssscrollscrollbar

Retain table layout while using block display for tbody


I have a table with a scrollable tbody. The problem is for the scroll to work I set the display for the the tbody to block, this in turn messes up the table layout

<div align="center">
        <table class="sampleTable">
          <thead>
            <tr>
                <th class="centerAlign" colspan="3">
                    <b>HEADER</b>
                </th>
            </tr>
            <tr>
                <th class="centerAlign">COLUMN 1</th>
                <th class="centerAlign">COLUMN 2</th>
                <th class="centerAlign">COLUMN 3</th>
            </tr>
           </thead>
            <tbody class="scroll">
                <tr>
                    <td class="centerAlign">
                        <a href="google.com">DATA</a>
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                </tr>
                <tr>
                    <td class="centerAlign">
                        <a href="google.com">DATA</a>
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                </tr>
                <tr>
                    <td class="centerAlign">
                        <a href="google.com">DATA</a>
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                </tr>
                <tr>
                    <td class="centerAlign">
                        <a href="google.com">DATA</a>
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                </tr>
                <tr>
                    <td class="centerAlign">
                        <a href="google.com">DATA</a>
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                </tr>
                <tr>
                    <td class="centerAlign">
                        <a href="google.com">DATA</a>
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                </tr>
                <tr>
                    <td class="centerAlign">
                        <a href="google.com">DATA</a>
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                </tr>
                <tr>
                    <td class="centerAlign">
                        <a href="google.com">DATA</a>
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                </tr>
                <tr>
                    <td class="centerAlign">
                        <a href="google.com">DATA</a>
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                </tr>
                <tr>
                    <td class="centerAlign">
                        <a href="google.com">DATA</a>
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                </tr>
                <tr>
                    <td class="centerAlign">
                        <a href="google.com">DATA</a>
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                </tr>
                <tr>
                    <td class="centerAlign">
                        <a href="google.com">DATA</a>
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                    <td class="centerAlign">
                        DATA
                    </td>
                </tr>
              </tbody>
          </table>
         </div>

And here's the css:

table {
  border-collapse: collapse;
  clear: both;
}

table.sampleTable {
  border: 0;
  width: 60%;
  height: 90%;
}

td {
  font-weight: normal;
  font-family: arial;
  font-size: 13px;
  color: #222222;
  line-height: 18px;
  border-top: 1px solid #ebebeb;
  border-right: 1px solid #ebebeb;
  border-bottom: 1px solid #ebebeb;
}

td.centerAlign {
  text-align: center;
}

th.centerAlign {
  text-align: center;
}

/* Scrollable tbody. */

tbody.scroll {
    overflow: scroll;
    height: 100px;
    width: 100%;
}

The table looks like this and does not scroll: Without block display

Now if I add 'display: block to the tbody.scroll class and to thead, it starts scrolling (as I want it to) but the layout gets messed up.

tbody.scroll {
        overflow: scroll;
        height: 100px;
        width: 100%;
        display: block;
    }

thead {
    display:block;
}

This is how it looks now.enter image description here

Is it possible to have the tbody scroll, without disturbing the look and feel of the table otherwise ?


Solution

  • I found the solution. Adding width in viewports fixed it.

    td.centerAlign {
      text-align: center;
      width: 100vw;
    }
    
    th.centerAlign {
      width: 100vw;
      text-align: center;
    }