I would like to have a table with the following features:
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.
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.
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.
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.
By setting postion: sticky and top: 0, we can create a fixed header on a scroll in HTML tables.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With