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

后端 未结 10 2070
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 03:04

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

Issue? As i scroll the page navigation-bar will be there be

相关标签:
10条回答
  • Here is a jsfiddle HERE (not created by me) that does what you are looking for with pure css in a table. The thing to note here is the th header is set to a height of 0. Inside each th is and absolute positioned div that puts the header above the table and the scollable div that the table is in.

    <thead>
      <tr>
        <th>#<div>#</div></th>
        <th>First Name<div>First Name</div></th>
        <th>Last Name<div>Last Name</div></th>
        <th>Username<div>Username</div></th>
      </tr>
    </thead>
    
    0 讨论(0)
  • 2020-11-28 03:45

    You can do it easily with puse CSS without any kind of JS. you have to add position: sticky; top: 0; z-index:999; in table th . But this won't work on Chrome Browser but other browser. To work on chrome you have to add those code in table thead th

    .table-fixed {
      width: 100%;
    }
    
    /*This will work on every browser but Chrome Browser*/
    .table-fixed thead {
      position: sticky;
      position: -webkit-sticky;
      top: 0;
      z-index: 999;
      background-color: #000;
      color: #fff;
    }
    
    /*This will work on every browser*/
    .table-fixed thead th {
        position: sticky;
        position: -webkit-sticky;
        top: 0;
        z-index: 999;
        background-color: #000;
        color: #fff;
    }
    <table class="table-fixed">
      <thead>
        <tr>
            <th>Table Header 1</th>
            <th>Table Header 2</th>
            <th>Table Header 3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
        </tr>
      </tbody>
    </table>

    0 讨论(0)
  • 2020-11-28 03:47

    Okku's post worked for me in Edge. In order to get desired result on the iPhone, I had to use:

    <thead class="sticky-top">
    

    which didn't effect Edge.

    0 讨论(0)
  • 2020-11-28 03:49

    I faced the same issue and as majority of the answers indicated, you have to apply position: sticky; and top: 0; ( mostly but can vary if there is a navbar which is fixed as well) to 'th' element. These properties do not apply to thead or tr.

    One more thing, if it still doesn't work, you have to look for 'overflow' properties of the parent. If any parent component has an overflow set, i.e. overflow: hidden, then position: sticky just doesn't work. Make sure to remove all such parent properties. Chao!

    0 讨论(0)
  • 2020-11-28 03:50

    For anyone looking for this functionality in 2018, it's much cleaner to do this with just CSS using position: sticky.

    position: sticky doesn't work with some table elements (thead/tr) in Chrome. You can move sticky to tds/ths of tr you need to be sticky. Like this:

    thead tr:nth-child(1) th {
      background: white;
      position: sticky;
      top: 0;
      z-index: 10;
    }
    
    0 讨论(0)
  • 2020-11-28 03:51

    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>

    0 讨论(0)
提交回复
热议问题