Fixed header design with CSS Bootstrap

Can a table be created in bootstrap with fixed headers and scrollable content similar to the example shown here?

  • Is it possible for columns to adjust their size based on the content they contain (wider or narrower)?
  • Also, is it possible to have a horizontal scroll bar when there are more columns than can be displayed?
  • Are there options to set minimum and maximum widths for the columns?

After researching, I only found a solution that requires knowing the number of columns in order to size them correctly using a flexible approach like demonstrated here.

table {
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    height: 500px; /* this can vary */
}

table * {
    box-sizing: inherit;
    -moz-box-sizing: inherit;
}

thead {
    display: flex;
    flex-direction: column;
    align-items: stretch;
}

tbody {
    overflow-y: scroll;
    display: inline-block;
}

thead > tr, tbody > tr, tfoot > tr {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
}

thead, tfoot {
    flex-shrink: 0;
}

th, tbody td {
    width: 20%; /* this can vary */
    overflow-x: hidden;
    text-overflow: ellipsis;
    display: inline-block;
}

tfoot {
    display: inline-block;
}

tfoot td {
    width: 100%;
    display: inline-block;
}

Answer №1

Here is a sample code showcasing an example of what you might be searching for, along with instructions on how to incorporate filtering and sorting functionalities. Please note that this code is put together quickly and has not been thoroughly tested yet, but it should give you a general idea of what you are looking for.

HTML File:

<div class="tableWrapper">
    <table class="headerTable table-striped">
        <thead>
            <tr>
                <td ng-click="sortType = 'id'; sortReverse = !sortReverse">
                    Id
                    <span ng-show="sortType == 'id' && !sortReverse" class="glyphicon glyphicon-chevron-down"></span>
                    <span ng-show="sortType == 'id' && sortReverse" class="glyphicon glyphicon-chevron-up"></span>
                </td>
                <td ng-click="sortType = 'name'; sortReverse = !sortReverse">
                    Name
                    <span ng-show="sortType == 'name' && !sortReverse" class="glyphicon glyphicon-chevron-down"></span>
                    <span ng-show="sortType == 'name' && sortReverse" class="glyphicon glyphicon-chevron-up"></span>
                </td>
            </tr>
        </thead>
        <tr>
            <td><input ng-model="dataText.id"></td>
            <td><input ng-model="dataText.name"></td>
        </tr>                                   
    </table>
    <div class="scrollableContent">
        <table class="table table-striped table-bordered">
            <tbody>
                <tr ng-repeat="data in dataList | orderBy:sortType:sortReverse | filter:dataText">
                    <td>{{data.id}}</td>
                    <td>{{data.name}}</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

<script>
$scope.sortReverse = false;
$scope.sortType = 'id'; // set the default sort type
$scope.dataText = '';
$scope.dataList=....some dynamic JSON data request
</script>

CSS File:

.tableWrapper {
    height: 100%;
    overflow-x: auto;
}

.tableWrapper table { /*set wrapper for the table*/
    max-width: 10000px;
    table-layout: fixed;
    width: 100%;
    display: inline-table;
}

table tr td {
    margin: 0;
    padding: 5px;
    width: 200px; /*width of each column*/
    word-wrap: break-word;
}

.tableStriped tr td {
    background: #000000;
    color: white;
    height: 40px;
}

.scrollableContent {
    height: 550px;
    min-width: inherit;
    overflow-y: scroll !important;
    position: absolute; /* This is important */
}

.scrollableContent::-webkit-scrollbar {
    width: 0em;
    height: 2em;
}

I also want to mention that you can specify a fixed width inside the tableWrapper and include scrollable-x if necessary.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The images on my website are not displaying properly on different devices

My attempt at creating a website for fun and testing purposes is facing issues when viewed on mobile devices. The images and specially positioned elements are not displaying correctly, extending beyond the page boundaries. I have tried adding the viewport ...

Validation of forms in Angular using a pseudo submission method

On a webpage, there is a form with two buttons: one to calculate a price and the other to submit the form. Below is a basic example: <form novalidate name="formStep1"> <select ng-model="address" required> <option></option> ...

When attempting to make a GET request from AngularJS to Phalcon using the $http method, the params array

I am currently using Angular 1.6.2 with Phalcon framework. $scope.selectAction = function(Item){ $http({ method: 'GET', url: 'Mycontroller/new', params : {Item : Item}, headers : {'Accept' : 'application/json&ap ...

Is there a barrier I've reached with the amount of hashtags I'm using?

My goal is to limit the use of javascript and rely more on CSS to achieve desired effects on my website. One feature I have developed is a modal that opens using hashtags and targets, similar to this example: http://codepen.io/maccadb7/pen/nbHEg. While th ...

The alignment of the third column div is off and not displaying properly

I am having some trouble with aligning the divs on my page in a column layout. Despite setting the first two divs to float left, the third one does not align properly. I want the third div to be floated right and aligned with the first two. You can see an ...

The ADAL library experiences issues when paired with the SystemJS loader, resulting in a ReferenceError: AuthenticationContext not being defined

Recently, I constructed a small test application using Angular1 that utilizes ADAL for connecting to an Office365 tenant. As I wanted to integrate Angular2 components, I decided to employ JSPM and SystemJS for loading modules, following the examples outlin ...

The issue with Ajax functionality seems to be limited to affecting only the first element within the Bootstrap

I have implemented Bootstrap modal and Ajax to submit my Booking Form for different Products. The Ajax submission works fine for the first Product, but when trying to submit the form for other Products, it redirects to a 404 Error Page. I have spent hours ...

Learn the process of updating database information with AngularJS through a button click

As a newcomer to Angular, I am facing an issue in my mini project. The main goal of the project is to display data from a database. I have successfully set up the view to show current data by making API calls and connecting to the database. However, I am n ...

Concealed upper TD boundary in IE 8

I've been struggling all day to figure out this style issue without any success. http://jsfiddle.net/9HP9Q/1/ The table lines should have gray borders around them. It's working fine on all browsers except Internet Explorer 8. .order_table tab ...

AngularJS compatibility problem detected with IE Edge mode

I am facing an issue with my AngularJS Web application when it is deployed to a higher environment and accessed through IE Edge. The browser defaults to IE 8 mode, which does not support AngularJS, causing my application to not work as expected. To try an ...

What is the best way to adjust the footer width to match the sidebar using Bootstrap 5?

Currently working on a Bootstrap 5 template for my website, but being new to it makes it a bit challenging. Specifically struggling with aligning the footer to fit the full width of the sidebar. <!DOCTYPE html> <html lang="en"> <head&g ...

Customizing arrays in DataTables allows for creating new columns and displaying them in the table according to the specified customizations

Displaying dynamic data in a data table involves taking an array as input and customizing the fields within that array. ...

Ways to differentiate between a desktop browser width of 1024px and a tablet width of 1024px with the help of jquery

So here's the issue I'm dealing with: I have scroll-based animation functions set up for desktop browsers, and different animations in place for tablet browsers. The challenge is to make these functions work on a desktop with a 1024px dimension. ...

Clear all CSS styles applied to a targeted division

My website built on Wordpress links to several CSS files. One specific div has its own unique style that is separate from the main Wordpress styles. Unfortunately, the Wordpress CSS ends up interfering with my custom div's layout. Is there a way in HT ...

I am facing difficulties accessing an element within a controller in Angular

Struggling to access an element inside an AngularJS controller, I attempted the following: var imageInput = document.getElementById("myImage"); Unfortunately, this approach was unsuccessful as the element returned null. Curiously, when placing the statem ...

Handling Protractor tests effortlessly using a solitary Gulp task

Attempting to implement e2e testing in my Angular project using Gulp has been a bit of a challenge. I have managed to make it work, but only by manually starting a standalone Selenium server using webdriver-manager start in a separate terminal window. Id ...

Utilizing dependency injection within an Angular 1 TypeScript application

Seeking assistance with integrating angular-jwt with typescript in my angular1 project. The angular-jwt package was installed using the following command: typings install dt~angular-jwt --global The package is located in typings>globals>angular-jwt ...

Tips on aligning images of various sizes with text within a row using CSS

Looking for help aligning four different height (ranging from 160-180px) svg images with text positioned directly below each image. I want the images to be neatly aligned in a row, but I'm struggling to align the short text underneath them in a single ...

I am encountering difficulties when trying to integrate ng-grid into my application

On the webpage <meta name="viewport" content="width=device-width" /> <title>Welcome to Husys</title> <link href="~/CSS/layout.css" rel="stylesheet" /> <link href="~/CSS/style.css" rel="stylesheet" /> <script src="~/Scrip ...

How to Implement a Dropdown Menu in HTML and CSS for Navigation Bars

Currently, I am in the process of creating a website at www.bossfakeid.co.uk. I am looking to add a dropdown menu to the "Products" section of the navbar. I found a dropdown menu solution that I would like to implement from this link: http://codepen.io/wil ...