Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table with fixed height and header and colum width with Bootstrap 3

I would like to have a table with the following features:

  • fixed header,
  • fixed height (with visible scrollbars)
  • 3 fixed width column (one large column and two narrow column).

I started using:

tbody { display:block; overflow:auto; height:100px; }

To get at least the fixed height but this rule squeezes the table on the left (I will provide a screenshot if necessary).

The table is contained in a Bootstrap 3 column.

Here is an example http://jsfiddle.net/PGEdK/

Any ideas? Thanks in advance.

like image 831
raben Avatar asked Jan 09 '14 10:01

raben


People also ask

How do you set the size of a column in a bootstrap responsive table?

On the table you now need to enable flex box by adding the class d-flex , and drop the xs to allow bootstrap to automatically detect the viewport. Table column width use the same layout as grids do; using col-[viewport]-[size] . Remember the column sizes should total 12; 1 + 3 + 3 + 5 = 12 in this example.

How do I set the width of a bootstrap table?

Add . w-auto class to the table element to set an auto width to the table column. The width of the columns will automatically adjust to the content of the column. That means it will always take a minimum width required to present its content.

How do you set a fixed height table?

The height of rows 'tr' in a table can be fixed very easily. This can be done by adding the height attribute in the tr tag. If the height is not specified, the height of the row changes according to the content. The height can be specified either in pixels, or percentage.

How do I make my table header fixed while scrolling?

By setting postion: sticky and top: 0, we can create a fixed header on a scroll in HTML tables.


1 Answers

Here's the JS FIDDLE DEMO which is working just like you have expected of. Acutally, there's no problem with your tbody style. You just need to add few more styles for your th and td. So, your final code will be something like this:

tr {display: block; }
th, td { width: 300px; }
tbody { display: block; height: 100px; overflow: auto;} 

If you look at the fiddle, you will notice that I have wrapped th with tr so that I can give proper width to th after giving display:block style to tr. So, if you want to target that tr which wraps th only, you can do that as well.

For demo purpose, I have wrapped the table with .tablecontainer class so that we have certain width to work with.

like image 145
Dhiraj Avatar answered Oct 21 '22 11:10

Dhiraj