HTML Table with Fixed Header and Scrollable Body


HTML Table with Fixed Header and Scrollable Body

Hi friends, In this tutorial, you will learn how to create an HTML table with fixed header and scrollable body using Datatables. The fixed header property helps us to view the header name at a fixed position against the table rows.

Actually, there are so many tutorials on this topic such as using bootstrap by sticky position or by CSS, etc. but the fact is that sometimes those tips and tricks might not work and sometimes it may work.

Also read, Google Maps Add Marker Using Maps Javascript API and HTML

Finally, I have got a solution for you that will work better. So, we can create an HTML table with a fixed header with the help of Datatables.

  • Datatables provides HTML tables with various features such as searching the data among table rows or showing the number of table rows per page so that the data can be loaded quickly.
  • It also provides the facility to sort the data in ascending or descending order just by clicking on the individual header fields of the HTML tables.
  • It also helps us to set the scrollbar to move the table data horizontally.
  • It also helps us to set the scrollbar to move the table data vertically.

Steps to create an HTML table with fixed header and scrollable body using Datatables

Step 1:- Create an HTML file in the root directory of your local server.
Step 2:- Put some table data so that we can check the scrolling along with the fixed header.
Step 3:- Copy the CSS link from the Datatables CDN and paste it inside the head section of the HTML file as shown below

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.23/fh-3.1.8/datatables.min.css"/>


Step 4:- Copy the Javascript links from the Datatables CDN and paste them just before the body tag ends as shown below

<script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.23/fh-3.1.8/datatables.min.js"></script>


Step 5:- Also copy the javascript code and paste it below the javascript links pasted before as shown below

<script>
        $('.cell-border').DataTable( {
            "paging": true,
            "fixedHeader": true,
            "bDestroy": true,
            "scrollY": '200px',
            "sScrollX": "100%",
            "scrollCollapse": true,
            "responsive": true
        });
</script>

Complete Code:-

<!DOCTYPE html>
<html>
  <head>
    <title>HTML table with Fixed Header</title>

    <link rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.23/fh-3.1.8/datatables.min.css"/>
  </head>
  <body>
  <h3>HTML table with fixed header and scrollable body</h3>
   <div style="width:80%;">
      <table id="" class="cell-border" style="width:100%;">
          <thead>
            <tr>
              <th scope="col">Sl</th>
              <th scope="col">Name</th>
              <th scope="col">Phone Number</th>
              <th scope="col">Age</th>
              <th scope="col">Department</th>
              <th scope="col">Designation</th>
              <th scope="col">Company Name</th>
              <th scope="col">Salary</th>
              <th scope="col">Date of Joining</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>1</td>
              <td>Biswa</td>
              <td>5434434545</td>
              <td>28</td>
              <td>IT</td>
              <td>B.Tech</td>
              <td>TCS</td>
              <td>15000</td>
              <td>24-08-2022</td>
            </tr>
            <tr>
              <td>2</td>
              <td>Manoj</td>
              <td>5434434548</td>
              <td>29</td>
              <td>Accounts</td>
              <td>B.Com</td>
              <td>Wipro</td>
              <td>45000</td>
              <td>25-08-2022</td>
            </tr>
            <tr>
              <td>3</td>
              <td>Bimal</td>
              <td>5434434549</td>
              <td>30</td>
              <td>Marketing</td>
              <td>BA</td>
              <td>Infosis</td>
              <td>50000</td>
              <td>26-08-2022</td>
            </tr>
            <tr>
              <td>4</td>
              <td>Biju</td>
              <td>5434434545</td>
              <td>31</td>
              <td>Sales</td>
              <td>MA</td>
              <td>IBM</td>
              <td>55000</td>
              <td>27-08-2022</td>
            </tr>
            <tr>
              <td>5</td>
              <td>Robin</td>
              <td>5434434542</td>
              <td>32</td>
              <td>Digital Marketing</td>
              <td>BA</td>
              <td>Web Infotech</td>
              <td>60000</td>
              <td>28-08-2022</td>
            </tr>
            <tr>
              <td>6</td>
              <td>Abhisekh</td>
              <td>5434434540</td>
              <td>33</td>
              <td>Management</td>
              <td>BSC</td>
              <td>Royal Global University</td>
              <td>65000</td>
              <td>29-08-2022</td>
            </tr>
            <tr>
              <td>7</td>
              <td>Raj</td>
              <td>5434434544</td>
              <td>34</td>
              <td>Construction</td>
              <td>BA</td>
              <td>Tata Cancer Hospital</td>
              <td>70000</td>
              <td>30-08-2022</td>
            </tr>
            <tr>
              <td>8</td>
              <td>Raj Singh</td>
              <td>5434434547</td>
              <td>35</td>
              <td>Sales & Marketing</td>
              <td>B.Tech</td>
              <td>HDFC Bank</td>
              <td>75000</td>
              <td>31-08-2022</td>
            </tr>
            <tr>
              <td>9</td>
              <td>Rupam</td>
              <td>5434434543</td>
              <td>36</td>
              <td>Accounts</td>
              <td>B.Com</td>
              <td>GNRC Hospital</td>
              <td>80000</td>
              <td>1-09-2022</td>
            </tr>
            <tr>
              <td>10</td>
              <td>Rinku</td>
              <td>5434434542</td>
              <td>37</td>
              <td>Customer Care</td>
              <td>BA</td>
              <td>Reliance</td>
              <td>25000</td>
              <td>02-09-2022</td>
            </tr>
          </tbody>
    </table>
  </div>
    <script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.23/fh-3.1.8/datatables.min.js"></script>
    <script>
        $('.cell-border').DataTable( {
            "paging": true,
            "fixedHeader": true,
            "bDestroy": true,
            "scrollY": '200px',
            "sScrollX": "100%",
            "scrollCollapse": true,
            "responsive": true
        });
    </script>
  </body>
</html>

Explanation of the Javascript code

  • “paging”: true, will enable the pagination which means a number of table rows per page to be included.
  • “fixedHeader”: true, will fix the header position of the table which means you can not move the header except the table data.
  • “scrollY”: ‘200px’, will set the vertical width of the scrolling element.
  • “sScrollX”: “100%”, will enable the horizontal scrollbar to the table.
  • “responsive”: true will make the table responsive.

Conclusion:- I hope this tutorial will help you to understand the overview. If there is any doubt then please leave a comment below


Leave a Comment

ceri188

ceri188

ceri188

KUPU178

HOKI311

RATU311

KUPU178

RATU311

RATU311

kupu178

kupu178

HOKI311

togel

kupu178

ratu311

mix parlay

ceri188

slot online

kupu178

THOR311

KUPU178

RATU311

THOR311

THOR311

THOR311

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

kupu178

hoki311

THOR311

hoki311

togel online

kupu178

slot mahjong

ceri188

slot server thailand

ratu311

ceri188

ceri188

RATU311

punjabi virasat

mimcord.com

Centrum Medyczne

www.vrcorporate.in

phbalance.vn

THOR311

KUPU178

daftar slot gacor

hoki311

togel hongkong

kupu178

slot mahjong

ratu311

slot gacor

kupu178

kupu178

ceri188

kupu178

ceri188

THOR311

KUPU178

KUPU178

KUPU178

RATU311

kupu178

RATU311

RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

slot online

kupu178

thor311

thor311

kupu178

kupu178

RATU311

KUPU178

THOR311

THOR311

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

ratu311

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

ceri188

THOR311

THOR311

RATU311

toto macau

KUPU178

slot gacor

HOKI311

sabung ayam online

RATU311

SLOT LUCKY NEKO

CERI188

Slot Gacor

KUPU178

RATU311

RATU311

Link Spaceman

CERI188

Slot Gacor Online

THOR311

Mahjong ways

KUPU178

THOR311

THOR311

Live Casino

HOKI311

CERI188

CERI188

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

CERI188

HOKI311 SLOT

TOTO TOGEL TOKYO

CERI188

CERI188 SITE

CERI188

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

Toto Togel

KUPU178

Slot Mahjong

CERI188

CERI188

CERI188

Slot Sweet Bonanza 2500

KUPU178

SLOT ONLINE

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 10 GAME SLOT TERBAIK

KUPU178

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

SITUS BOLA

CERI188

KUPU178

THOR311

RATU311

THOR311

THOR311

HOKI311

KUPU178

RATU311

THOR311

CERI188

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

Slot Gacor Online

KUPU178

Mix Parlay

CERI188

CERI188

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

KUPU178

KUPU178

RATU311

CERI188

RATU311: Alternatif karnpuracollege

RATU311

RATU311

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311 PRO

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

RATU311

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

situs judi bola

HOKI311

RATU311

THOR311

HOKI311

KUPU178

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

slot thailand

judi bola

KUPU178

slot gacor

slot gacor

yakuza303

slot gacor

RATU311

THOR311