Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stick table header(thead) on top while scrolling down the table rows with fixed header(navbar) in bootstrap 3?

Bootstrap layout with fixed-navbar. Having table with so many rows in body.

Issue? As i scroll the page navigation-bar will be there because it is fixed. as i scroll more i want table header to be fixed under navigation-bar and the content of table(table-body) scrolls without scroll bar!

Something like This - Codepen


**Fiddle ** Bootstrap table


Working Fiddle after referring to the answer!


HTML


<!-- Wrap all page content here --> <div id="wrap">    <!-- Fixed navbar -->   <div class="navbar navbar-default navbar-fixed-top">     <div class="container">       <div class="navbar-header">         <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">           <span class="icon-bar"></span>           <span class="icon-bar"></span>           <span class="icon-bar"></span>         </button>         <a class="navbar-brand" href="#">Project name</a>       </div>       <div class="collapse navbar-collapse">         <ul class="nav navbar-nav">           <li class="active"><a href="#">Home</a></li>           <li><a href="#about">About</a></li>           <li><a href="#contact">Contact</a></li>           <li class="dropdown">             <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>             <ul class="dropdown-menu">               <li><a href="#">Action</a></li>               <li><a href="#">Another action</a></li>               <li><a href="#">Something else here</a></li>               <li class="divider"></li>               <li class="dropdown-header">Nav header</li>               <li><a href="#">Separated link</a></li>               <li><a href="#">One more separated link</a></li>             </ul>           </li>         </ul>       </div><!--/.nav-collapse -->     </div>   </div>    <!-- Begin page content -->   <div class="container">     <div class="page-header">       <h1>Sticky footer with fixed navbar</h1>     </div>     <p class="lead">Pin a fixed-height footer to the bottom of the viewport in desktop browsers with this custom HTML and CSS. A fixed navbar has been added within <code>#wrap</code> with <code>padding-top: 60px;</code> on the <code>.container</code>.</p>    </div> <table class="table">         <thead>           <tr>             <th>#</th>             <th>First Name</th>             <th>Last Name</th>             <th>Username</th>           </tr>         </thead>         <tbody>           <tr>             <td>1</td>             <td>Mark</td>             <td>Otto</td>             <td>@mdo</td>           </tr>           <tr>             <td>2</td>             <td>Jacob</td>             <td>Thornton</td>             <td>@fat</td>           </tr>           <tr>             <td>3</td>             <td>Larry</td>             <td>the Bird</td>             <td>@twitter</td>           </tr>           <tr>             <td>1</td>             <td>Mark</td>             <td>Otto</td>             <td>@mdo</td>           </tr>           <tr>             <td>2</td>             <td>Jacob</td>             <td>Thornton</td>             <td>@fat</td>           </tr>           <tr>             <td>3</td>             <td>Larry</td>             <td>the Bird</td>             <td>@twitter</td>           </tr>           <tr>             <td>1</td>             <td>Mark</td>             <td>Otto</td>             <td>@mdo</td>           </tr>           <tr>             <td>2</td>             <td>Jacob</td>             <td>Thornton</td>             <td>@fat</td>           </tr>           <tr>             <td>3</td>             <td>Larry</td>             <td>the Bird</td>             <td>@twitter</td>           </tr>           <tr>             <td>1</td>             <td>Mark</td>             <td>Otto</td>             <td>@mdo</td>           </tr>           <tr>             <td>2</td>             <td>Jacob</td>             <td>Thornton</td>             <td>@fat</td>           </tr>           <tr>             <td>3</td>             <td>Larry</td>             <td>the Bird</td>             <td>@twitter</td>           </tr>         </tbody>       </table> </div>  <div id="footer">   <div class="container">     <p class="text-muted credit">Example courtesy <a href="http://martinbean.co.uk">Martin Bean</a> and <a href="http://ryanfait.com/sticky-footer/">Ryan Fait</a>.</p>   </div> </div> 
like image 538
Suresh Karia Avatar asked Jul 19 '14 11:07

Suresh Karia


People also ask

How do I keep my table header fixed while scrolling in HTML?

To freeze the row/column we can use a simple HTML table and CSS. HTML: In HTML we can define the header row by <th> tag or we can use <td> tag also. Below example is using the <th> tag. We also put the table in DIV element to see the horizontal and vertical scrollbar by setting the overflow property of the DIV element.

How do I make my table scrollable with fixed header?

Create a Table That Has a Fixed Header. We can create an HTML table that has a fixed header with some CSS. We set the height of the table element to 120px to make restrict the height of it so we can make it scrollable. To make it scrollable, we set the overflow CSS property to scroll .


1 Answers

This can now be done without JS, just pure CSS. So, anyone trying to do this for modern browsers should look into using position: sticky instead.

Currently, both Edge and Chrome have a bug where position: sticky doesn't work on thead or tr elements, however it's possible to use it on th elements, so all you need to do is just add this to your code:

th {   position: sticky;   top: 50px;  /* 0px if you don't have a navbar, but something is required */   background: white; } 

Note: you'll need a background color for them, or you'll be able to see through the sticky title bar.

This has very good browser support.

Demo with your code (HTML unaltered, above 5 lines of CSS added, all JS removed):

body {      padding-top:50px;  }  table.floatThead-table {      border-top: none;      border-bottom: none;      background-color: #fff;  }    th {    position: sticky;    top: 50px;    background: white;  }
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">    <!-- Fixed navbar -->  <div class="navbar navbar-default navbar-fixed-top">      <div class="container">          <div class="navbar-header">              <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span>   <span class="icon-bar"></span>   <span class="icon-bar"></span>                </button> <a class="navbar-brand" href="#">Project name</a>            </div>          <div class="collapse navbar-collapse">              <ul class="nav navbar-nav">                  <li class="active"><a href="#">Home</a>                    </li>                  <li><a href="#about">About</a>                    </li>                  <li><a href="#contact">Contact</a>                    </li>                  <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>                        <ul class="dropdown-menu">                          <li><a href="#">Action</a>                            </li>                          <li><a href="#">Another action</a>                            </li>                          <li><a href="#">Something else here</a>                            </li>                          <li class="divider"></li>                          <li class="dropdown-header">Nav header</li>                          <li><a href="#">Separated link</a>                            </li>                          <li><a href="#">One more separated link</a>                            </li>                      </ul>                  </li>              </ul>          </div>          <!--/.nav-collapse -->      </div>  </div>  <!-- Begin page content -->  <div class="container">      <div class="page-header">           <h1>Sticky Table Headers</h1>        </div>      <p class="lead">If the page is tall and all of the table is visible, then it won't stick. Make your viewport short.</p>      <p class="lead">If the page is tall and all of the table is visible, then it won't stick. Make your viewport short.</p>      <p class="lead">If the page is tall and all of the table is visible, then it won't stick. Make your viewport short.</p>      <table class="table table-striped sticky-header">          <thead>              <tr>                  <th>#</th>                  <th>First Name</th>                  <th>Last Name</th>                  <th>Username</th>              </tr>          </thead>          <tbody>              <tr>                  <td>1</td>                  <td>Mark</td>                  <td>Otto</td>                  <td>@mdo</td>              </tr>              <tr>                  <td>2</td>                  <td>Jacob</td>                  <td>Thornton</td>                  <td>@fat</td>              </tr>              <tr>                  <td>3</td>                  <td>Larry</td>                  <td>the Bird</td>                  <td>@twitter</td>              </tr>              <tr>                  <td>1</td>                  <td>Mark</td>                  <td>Otto</td>                  <td>@mdo</td>              </tr>              <tr>                  <td>2</td>                  <td>Jacob</td>                  <td>Thornton</td>                  <td>@fat</td>              </tr>              <tr>                  <td>3</td>                  <td>Larry</td>                  <td>the Bird</td>                  <td>@twitter</td>              </tr>              <tr>                  <td>1</td>                  <td>Mark</td>                  <td>Otto</td>                  <td>@mdo</td>              </tr>              <tr>                  <td>2</td>                  <td>Jacob</td>                  <td>Thornton</td>                  <td>@fat</td>              </tr>              <tr>                  <td>3</td>                  <td>Larry</td>                  <td>the Bird</td>                  <td>@twitter</td>              </tr>              <tr>                  <td>1</td>                  <td>Mark</td>                  <td>Otto</td>                  <td>@mdo</td>              </tr>              <tr>                  <td>2</td>                  <td>Jacob</td>                  <td>Thornton</td>                  <td>@fat</td>              </tr>              <tr>                  <td>3</td>                  <td>Larry</td>                  <td>the Bird</td>                  <td>@twitter</td>              </tr>          </tbody>      </table>      <p class="lead">If the page is tall and all of the table is visible, then it won't stick. Make your viewport short.</p>      <p class="lead">If the page is tall and all of the table is visible, then it won't stick. Make your viewport short.</p>      <p class="lead">If the page is tall and all of the table is visible, then it won't stick. Make your viewport short.</p>      <p class="lead">If the page is tall and all of the table is visible, then it won't stick. Make your viewport short.</p>      <p class="lead">If the page is tall and all of the table is visible, then it won't stick. Make your viewport short.</p>      <p class="lead">If the page is tall and all of the table is visible, then it won't stick. Make your viewport short.</p>      <p class="lead">If the page is tall and all of the table is visible, then it won't stick. Make your viewport short.</p>       <h3>Table 2</h3>        <table class="table table-striped sticky-header">          <thead>              <tr>                  <th>#</th>                  <th>New Table</th>                  <th>Last Name</th>                  <th>Username</th>              </tr>          </thead>          <tbody>              <tr>                  <td>1</td>                  <td>Mark</td>                  <td>Otto</td>                  <td>@mdo</td>              </tr>              <tr>                  <td>2</td>                  <td>Jacob</td>                  <td>Thornton</td>                  <td>@fat</td>              </tr>              <tr>                  <td>3</td>                  <td>Larry</td>                  <td>the Bird</td>                  <td>@twitter</td>              </tr>              <tr>                  <td>1</td>                  <td>Mark</td>                  <td>Otto</td>                  <td>@mdo</td>              </tr>              <tr>                  <td>2</td>                  <td>Jacob</td>                  <td>Thornton</td>                  <td>@fat</td>              </tr>              <tr>                  <td>3</td>                  <td>Larry</td>                  <td>the Bird</td>                  <td>@twitter</td>              </tr>              <tr>                  <td>1</td>                  <td>Mark</td>                  <td>Otto</td>                  <td>@mdo</td>              </tr>              <tr>                  <td>2</td>                  <td>Jacob</td>                  <td>Thornton</td>                  <td>@fat</td>              </tr>              <tr>                  <td>3</td>                  <td>Larry</td>                  <td>the Bird</td>                  <td>@twitter</td>              </tr>              <tr>                  <td>1</td>                  <td>Mark</td>                  <td>Otto</td>                  <td>@mdo</td>              </tr>              <tr>                  <td>2</td>                  <td>Jacob</td>                  <td>Thornton</td>                  <td>@fat</td>              </tr>              <tr>                  <td>3</td>                  <td>Larry</td>                  <td>the Bird</td>                  <td>@twitter</td>              </tr>          </tbody>      </table>  </div>
like image 121
Okku Avatar answered Sep 17 '22 01:09

Okku