How can I set for <table>
100% width and put only inside <tbody>
vertical scroll for some height?
table {_x000D_
width: 100%;_x000D_
display:block;_x000D_
}_x000D_
thead {_x000D_
display: inline-block;_x000D_
width: 100%;_x000D_
height: 20px;_x000D_
}_x000D_
tbody {_x000D_
height: 200px;_x000D_
display: inline-block;_x000D_
width: 100%;_x000D_
overflow: auto;_x000D_
}_x000D_
_x000D_
<table>_x000D_
<thead>_x000D_
<tr>_x000D_
<th>Head 1</th>_x000D_
<th>Head 2</th>_x000D_
<th>Head 3</th>_x000D_
<th>Head 4</th>_x000D_
<th>Head 5</th>_x000D_
</tr>_x000D_
</thead>_x000D_
<tbody>_x000D_
<tr>_x000D_
<td>Content 1</td>_x000D_
<td>Content 2</td>_x000D_
<td>Content 3</td>_x000D_
<td>Content 4</td>_x000D_
<td>Content 5</td>_x000D_
</tr>_x000D_
</tbody>_x000D_
</table>
_x000D_
I want to avoid adding some additional div, all I want is simple table like this and when I trying to change display, table-layout
, position
and much more things in CSS table not working good with 100% width only with fixed width in px.
This question is related to
html
css
vertical-scrolling
try below approach, very simple easy to implement
Below is the jsfiddle link
http://jsfiddle.net/v2t2k8ke/2/
HTML:
<table border='1' id='tbl_cnt'>
<thead><tr></tr></thead><tbody></tbody>
CSS:
#tbl_cnt{
border-collapse: collapse; width: 100%;word-break:break-all;
}
#tbl_cnt thead, #tbl_cnt tbody{
display: block;
}
#tbl_cnt thead tr{
background-color: #8C8787; text-align: center;width:100%;display:block;
}
#tbl_cnt tbody {
height: 100px;overflow-y: auto;overflow-x: hidden;
}
Jquery:
var data = [
{
"status":"moving","vehno":"tr544","loc":"bng","dri":"ttt"
}, {
"status":"stop","vehno":"tr54","loc":"che", "dri":"ttt"
},{ "status":"idle","vehno":"yy5499999999999994","loc":"bng","dri":"ttt"
},{
"status":"moving","vehno":"tr544","loc":"bng", "dri":"ttt"
}, {
"status":"stop","vehno":"tr54","loc":"che","dri":"ttt"
},{
"status":"idle","vehno":"yy544","loc":"bng","dri":"ttt"
}
];
var sth = '';
$.each(data[0], function (key, value) {
sth += '<td>' + key + '</td>';
});
var stb = '';
$.each(data, function (key, value) {
stb += '<tr>';
$.each(value, function (key, value) {
stb += '<td>' + value + '</td>';
});
stb += '</tr>';
});
$('#tbl_cnt thead tr').append(sth);
$('#tbl_cnt tbody').append(stb);
setTimeout(function () {
var col_cnt=0
$.each(data[0], function (key, value) {col_cnt++;});
$('#tbl_cnt thead tr').css('width', ($("#tbl_cnt tbody") [0].scrollWidth)+ 'px');
$('#tbl_cnt thead tr td,#tbl_cnt tbody tr td').css('width', ($('#tbl_cnt thead tr ').width()/Number(col_cnt)) + 'px');}, 100)
For using "overflow: scroll" you must set "display:block" on thead and tbody. And that messes up column widths between them. But then you can clone the thead row with Javascript and paste it in the tbody as a hidden row to keep the exact col widths.
$('.myTable thead > tr').clone().appendTo('.myTable tbody').addClass('hidden-to-set-col-widths');
http://jsfiddle.net/Julesezaar/mup0c5hk/
<table class="myTable">
<thead>
<tr>
<td>Problem</td>
<td>Solution</td>
<td>blah</td>
<td>derp</td>
</tr>
</thead>
<tbody></tbody>
</table>
<p>
Some text to here
</p>
The css:
table {
background-color: #aaa;
width: 100%;
}
thead,
tbody {
display: block; // Necessary to use overflow: scroll
}
tbody {
background-color: #ddd;
height: 150px;
overflow-y: scroll;
}
tbody tr.hidden-to-set-col-widths,
tbody tr.hidden-to-set-col-widths td {
visibility: hidden;
height: 0;
line-height: 0;
padding-top: 0;
padding-bottom: 0;
}
td {
padding: 3px 10px;
}
Create two tables one after other, put second table in a div of fixed height and set the overflow property to auto. Also keep all the td's inside thead in second table empty.
<div>
<table>
<thead>
<tr>
<th>Head 1</th>
<th>Head 2</th>
<th>Head 3</th>
<th>Head 4</th>
<th>Head 5</th>
</tr>
</thead>
</table>
</div>
<div style="max-height:500px;overflow:auto;">
<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
</tr>
</tbody>
</table>
</div>
I'm using display:block
for thead
and tbody
.
Because of that the width of the thead
columns is different from the width of the tbody
columns.
table {
margin:0 auto;
border-collapse:collapse;
}
thead {
background:#CCCCCC;
display:block
}
tbody {
height:10em;overflow-y:scroll;
display:block
}
To fix this I use small jQuery
code but it can be done in JavaScript
only.
var colNumber=3 //number of table columns
for (var i=0; i<colNumber; i++) {
var thWidth=$("#myTable").find("th:eq("+i+")").width();
var tdWidth=$("#myTable").find("td:eq("+i+")").width();
if (thWidth<tdWidth)
$("#myTable").find("th:eq("+i+")").width(tdWidth);
else
$("#myTable").find("td:eq("+i+")").width(thWidth);
}
Here is my working demo: http://jsfiddle.net/gavroche/N7LEF/
Does not work in IE 8
var colNumber=3 //number of table columns_x000D_
_x000D_
_x000D_
for (var i=0; i<colNumber; i++)_x000D_
{_x000D_
var thWidth=$("#myTable").find("th:eq("+i+")").width();_x000D_
var tdWidth=$("#myTable").find("td:eq("+i+")").width(); _x000D_
if (thWidth<tdWidth) _x000D_
$("#myTable").find("th:eq("+i+")").width(tdWidth);_x000D_
else_x000D_
$("#myTable").find("td:eq("+i+")").width(thWidth); _x000D_
}
_x000D_
table {margin:0 auto; border-collapse:separate;}_x000D_
thead {background:#CCCCCC;display:block}_x000D_
tbody {height:10em;overflow-y:scroll;display:block}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>_x000D_
<table id="myTable" border="1">_x000D_
<thead>_x000D_
<tr>_x000D_
<th>A really Very Long Header Text</th>_x000D_
<th>Normal Header</th>_x000D_
<th>Short</th> _x000D_
</tr> _x000D_
</thead>_x000D_
<tbody>_x000D_
<tr>_x000D_
<td>_x000D_
Text shorter than header_x000D_
</td>_x000D_
<td>_x000D_
Text is longer than header_x000D_
</td>_x000D_
<td>_x000D_
Exact_x000D_
</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>_x000D_
Text shorter than header_x000D_
</td>_x000D_
<td>_x000D_
Text is longer than header_x000D_
</td>_x000D_
<td>_x000D_
Exact_x000D_
</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>_x000D_
Text shorter than header_x000D_
</td>_x000D_
<td>_x000D_
Text is longer than header_x000D_
</td>_x000D_
<td>_x000D_
Exact_x000D_
</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>_x000D_
Text shorter than header_x000D_
</td>_x000D_
<td>_x000D_
Text is longer than header_x000D_
</td>_x000D_
<td>_x000D_
Exact_x000D_
</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>_x000D_
Text shorter than header_x000D_
</td>_x000D_
<td>_x000D_
Text is longer than header_x000D_
</td>_x000D_
<td>_x000D_
Exact_x000D_
</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>_x000D_
Text shorter than header_x000D_
</td>_x000D_
<td>_x000D_
Text is longer than header_x000D_
</td>_x000D_
<td>_x000D_
Exact_x000D_
</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>_x000D_
Text shorter than header_x000D_
</td>_x000D_
<td>_x000D_
Text is longer than header_x000D_
</td>_x000D_
<td>_x000D_
Exact_x000D_
</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>_x000D_
Text shorter than header_x000D_
</td>_x000D_
<td>_x000D_
Text is longer than header_x000D_
</td>_x000D_
<td>_x000D_
Exact_x000D_
</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>_x000D_
Text shorter than header_x000D_
</td>_x000D_
<td>_x000D_
Text is longer than header_x000D_
</td>_x000D_
<td>_x000D_
Exact_x000D_
</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>_x000D_
Text shorter than header_x000D_
</td>_x000D_
<td>_x000D_
Text is longer than header_x000D_
</td>_x000D_
<td>_x000D_
Exact_x000D_
</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>_x000D_
Text shorter than header_x000D_
</td>_x000D_
<td>_x000D_
Text is longer than header_x000D_
</td>_x000D_
<td>_x000D_
Exact_x000D_
</td>_x000D_
</tr>_x000D_
</tbody>_x000D_
</table>
_x000D_
This solution still requires the th
widths to be calculated and set by jQuery
table.scroll tbody,
table.scroll thead { display: block; }
table.scroll tbody {
overflow-y: auto;
overflow-x: hidden;
max-height: 300px;
}
table.scroll tr {
display: flex;
}
table.scroll tr > td {
flex-grow: 1;
flex-basis: 0;
}
And the Jquery / Javascript
var $table = $('#the_table_element'),
$bodyCells = $table.find('tbody tr:first').children(),
colWidth;
$table.addClass('scroll');
// Adjust the width of thead cells when window resizes
$(window).resize(function () {
// Get the tbody columns width array
colWidth = $bodyCells.map(function () {
return $(this).width();
}).get();
// Set the width of thead columns
$table.find('thead tr').children().each(function (i, v) {
$(v).width(colWidth[i]);
});
}).resize(); // Trigger resize handler
In modern browsers, you can simply use css:
th {
position: sticky;
top: 0;
z-index: 2;
}
This is the code that works for me to create a sticky thead on a table with a scrollable tbody:
table ,tr td{
border:1px solid red
}
tbody {
display:block;
height:50px;
overflow:auto;
}
thead, tbody tr {
display:table;
width:100%;
table-layout:fixed;/* even columns width , fix width of table too*/
}
thead {
width: calc( 100% - 1em )/* scrollbar is average 1em/16px width, remove it from thead width */
}
table {
width:400px;
}
for Chrome, Firefox, Edge (and other evergreen browsers)
Simply position: sticky; top: 0;
your th
elements:
/* Fix table head */
.tableFixHead { overflow-y: auto; height: 100px; }
.tableFixHead th { position: sticky; top: 0; }
/* Just common table stuff. */
table { border-collapse: collapse; width: 100%; }
th, td { padding: 8px 16px; }
th { background:#eee; }
_x000D_
<div class="tableFixHead">
<table>
<thead>
<tr><th>TH 1</th><th>TH 2</th></tr>
</thead>
<tbody>
<tr><td>A1</td><td>A2</td></tr>
<tr><td>B1</td><td>B2</td></tr>
<tr><td>C1</td><td>C2</td></tr>
<tr><td>D1</td><td>D2</td></tr>
<tr><td>E1</td><td>E2</td></tr>
</tbody>
</table>
</div>
_x000D_
PS: if you need borders for TH elements th {box-shadow: 1px 1px 0 #000; border-top: 0;}
will help (since the default borders are not painted correctly on scroll).
For a variant of the above that uses just a bit of JS in order to accommodate for IE11 see this answer Table fixed header and scrollable body
In following solution, table occupies 100% of the parent container, no absolute sizes required. It's pure CSS, flex layout is used.
Here is how it looks:
Possible disadvantages:
HTML (shortened):
<div class="table-container">
<table>
<thead>
<tr>
<th>head1</th>
<th>head2</th>
<th>head3</th>
<th>head4</th>
</tr>
</thead>
<tbody>
<tr>
<td>content1</td>
<td>content2</td>
<td>content3</td>
<td>content4</td>
</tr>
<tr>
<td>content1</td>
<td>content2</td>
<td>content3</td>
<td>content4</td>
</tr>
...
</tbody>
</table>
</div>
CSS, with some decorations omitted for clarity:
.table-container {
height: 10em;
}
table {
display: flex;
flex-flow: column;
height: 100%;
width: 100%;
}
table thead {
/* head takes the height it requires,
and it's not scaled when table is resized */
flex: 0 0 auto;
width: calc(100% - 0.9em);
}
table tbody {
/* body takes all the remaining available space */
flex: 1 1 auto;
display: block;
overflow-y: scroll;
}
table tbody tr {
width: 100%;
}
table thead, table tbody tr {
display: table;
table-layout: fixed;
}
Same code in LESS so you can mix it in:
.table-scrollable() {
@scrollbar-width: 0.9em;
display: flex;
flex-flow: column;
thead,
tbody tr {
display: table;
table-layout: fixed;
}
thead {
flex: 0 0 auto;
width: ~"calc(100% - @{scrollbar-width})";
}
tbody {
display: block;
flex: 1 1 auto;
overflow-y: scroll;
tr {
width: 100%;
}
}
}
Adding a fixed width to td,th after making tbody & thead display block works perfectly and also we can use slimscroll plugin to make the scroll bar beautiful.
<!DOCTYPE html>_x000D_
<html>_x000D_
_x000D_
<head>_x000D_
<title> Scrollable table </title>_x000D_
<style>_x000D_
body {_x000D_
font-family: sans-serif;_x000D_
font-size: 0.9em;_x000D_
}_x000D_
table {_x000D_
border-collapse: collapse;_x000D_
border-bottom: 1px solid #ddd;_x000D_
}_x000D_
thead {_x000D_
background-color: #333;_x000D_
color: #fff;_x000D_
}_x000D_
thead,tbody {_x000D_
display: block;_x000D_
}_x000D_
th,td {_x000D_
padding: 8px 10px;_x000D_
border: 1px solid #ddd;_x000D_
width: 117px;_x000D_
box-sizing: border-box;_x000D_
}_x000D_
tbody {_x000D_
height: 160px;_x000D_
overflow-y: scroll_x000D_
}_x000D_
</style>_x000D_
</head>_x000D_
_x000D_
<body>_x000D_
_x000D_
<table class="example-table">_x000D_
<thead>_x000D_
<tr>_x000D_
<th> Header 1 </th>_x000D_
<th> Header 2 </th>_x000D_
<th> Header 3 </th>_x000D_
<th> Header 4 </th>_x000D_
</tr>_x000D_
</thead>_x000D_
<tbody>_x000D_
<tr>_x000D_
<td> Row 1- Col 1 </td>_x000D_
<td> Row 1- Col 2 </td>_x000D_
<td> Row 1- Col 3 </td>_x000D_
<td> Row 1- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 2- Col 1 </td>_x000D_
<td> Row 2- Col 2 </td>_x000D_
<td> Row 2- Col 3 </td>_x000D_
<td> Row 2- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 3- Col 1 </td>_x000D_
<td> Row 3- Col 2 </td>_x000D_
<td> Row 3- Col 3 </td>_x000D_
<td> Row 3- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 4- Col 1 </td>_x000D_
<td> Row 4- Col 2 </td>_x000D_
<td> Row 4- Col 3 </td>_x000D_
<td> Row 4- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 5- Col 1 </td>_x000D_
<td> Row 5- Col 2 </td>_x000D_
<td> Row 5- Col 3 </td>_x000D_
<td> Row 5- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 6- Col 1 </td>_x000D_
<td> Row 6- Col 2 </td>_x000D_
<td> Row 6- Col 3 </td>_x000D_
<td> Row 6- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 7- Col 1 </td>_x000D_
<td> Row 7- Col 2 </td>_x000D_
<td> Row 7- Col 3 </td>_x000D_
<td> Row 7- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 8- Col 1 </td>_x000D_
<td> Row 8- Col 2 </td>_x000D_
<td> Row 8- Col 3 </td>_x000D_
<td> Row 8- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 9- Col 1 </td>_x000D_
<td> Row 9- Col 2 </td>_x000D_
<td> Row 9- Col 3 </td>_x000D_
<td> Row 9- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 10- Col 1 </td>_x000D_
<td> Row 10- Col 2 </td>_x000D_
<td> Row 10- Col 3 </td>_x000D_
<td> Row 10- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 11- Col 1 </td>_x000D_
<td> Row 11- Col 2 </td>_x000D_
<td> Row 11- Col 3 </td>_x000D_
<td> Row 11- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 12- Col 1 </td>_x000D_
<td> Row 12- Col 2 </td>_x000D_
<td> Row 12- Col 3 </td>_x000D_
<td> Row 12- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 13- Col 1 </td>_x000D_
<td> Row 13- Col 2 </td>_x000D_
<td> Row 13- Col 3 </td>_x000D_
<td> Row 13- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 14- Col 1 </td>_x000D_
<td> Row 14- Col 2 </td>_x000D_
<td> Row 14- Col 3 </td>_x000D_
<td> Row 14- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 15- Col 1 </td>_x000D_
<td> Row 15- Col 2 </td>_x000D_
<td> Row 15- Col 3 </td>_x000D_
<td> Row 15- Col 4 </td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td> Row 16- Col 1 </td>_x000D_
<td> Row 16- Col 2 </td>_x000D_
<td> Row 16- Col 3 </td>_x000D_
<td> Row 16- Col 4 </td>_x000D_
</tr>_x000D_
</tbody>_x000D_
</table>_x000D_
_x000D_
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery-slimScroll/1.3.8/jquery.slimscroll.min.js"></script>_x000D_
<script>_x000D_
$('.example-table tbody').slimscroll({_x000D_
height: '160px',_x000D_
alwaysVisible: true,_x000D_
color: '#333'_x000D_
})_x000D_
</script>_x000D_
</body>_x000D_
_x000D_
</html>
_x000D_
Try this jsfiddle. This is using jQuery and made from Hashem Qolami's answer. At first, make a regular table then make it scrollable.
const makeScrollableTable = function (tableSelector, tbodyHeight) {
let $table = $(tableSelector);
let $bodyCells = $table.find('tbody tr:first').children();
let $headCells = $table.find('thead tr:first').children();
let headColWidth = 0;
let bodyColWidth = 0;
headColWidth = $headCells.map(function () {
return $(this).outerWidth();
}).get();
bodyColWidth = $bodyCells.map(function () {
return $(this).outerWidth();
}).get();
$table.find('thead tr').children().each(function (i, v) {
$(v).css("width", headColWidth[i]+"px");
$(v).css("min-width", headColWidth[i]+"px");
$(v).css("max-width", headColWidth[i]+"px");
});
$table.find('tbody tr').children().each(function (i, v) {
$(v).css("width", bodyColWidth[i]+"px");
$(v).css("min-width", bodyColWidth[i]+"px");
$(v).css("max-width", bodyColWidth[i]+"px");
});
$table.find('thead').css("display", "block");
$table.find('tbody').css("display", "block");
$table.find('tbody').css("height", tbodyHeight+"px");
$table.find('tbody').css("overflow-y", "auto");
$table.find('tbody').css("overflow-x", "hidden");
};
Then you can use this function as follows:
makeScrollableTable('#test-table', 250);
I got it finally right with pure CSS by following these instructions:
http://tjvantoll.com/2012/11/10/creating-cross-browser-scrollable-tbody/
The first step is to set the <tbody>
to display: block so an overflow and height can be applied. From there the rows in the <thead>
need to be set to position: relative and display: block so that they’ll sit on top of the now scrollable <tbody>
.
tbody, thead { display: block; overflow-y: auto; }
Because the <thead>
is relatively positioned each table cell needs an explicit width
td:nth-child(1), th:nth-child(1) { width: 100px; }
td:nth-child(2), th:nth-child(2) { width: 100px; }
td:nth-child(3), th:nth-child(3) { width: 100px; }
But unfortunately that is not enough. When a scrollbar is present browsers allocate space for it, therefore, the <tbody>
ends up having less space available than the <thead>
. Notice the slight misalignment this creates...
The only workaround I could come up with was to set a min-width on all columns except the last one.
td:nth-child(1), th:nth-child(1) { min-width: 100px; }
td:nth-child(2), th:nth-child(2) { min-width: 100px; }
td:nth-child(3), th:nth-child(3) { width: 100px; }
Whole codepen example below:
CSS:
.fixed_headers {
width: 750px;
table-layout: fixed;
border-collapse: collapse;
}
.fixed_headers th {
text-decoration: underline;
}
.fixed_headers th,
.fixed_headers td {
padding: 5px;
text-align: left;
}
.fixed_headers td:nth-child(1),
.fixed_headers th:nth-child(1) {
min-width: 200px;
}
.fixed_headers td:nth-child(2),
.fixed_headers th:nth-child(2) {
min-width: 200px;
}
.fixed_headers td:nth-child(3),
.fixed_headers th:nth-child(3) {
width: 350px;
}
.fixed_headers thead {
background-color: #333333;
color: #fdfdfd;
}
.fixed_headers thead tr {
display: block;
position: relative;
}
.fixed_headers tbody {
display: block;
overflow: auto;
width: 100%;
height: 300px;
}
.fixed_headers tbody tr:nth-child(even) {
background-color: #dddddd;
}
.old_ie_wrapper {
height: 300px;
width: 750px;
overflow-x: hidden;
overflow-y: auto;
}
.old_ie_wrapper tbody {
height: auto;
}
Html:
<!-- IE < 10 does not like giving a tbody a height. The workaround here applies the scrolling to a wrapped <div>. -->
<!--[if lte IE 9]>
<div class="old_ie_wrapper">
<!--<![endif]-->
<table class="fixed_headers">
<thead>
<tr>
<th>Name</th>
<th>Color</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>Apple</td>
<td>Red</td>
<td>These are red.</td>
</tr>
<tr>
<td>Pear</td>
<td>Green</td>
<td>These are green.</td>
</tr>
<tr>
<td>Grape</td>
<td>Purple / Green</td>
<td>These are purple and green.</td>
</tr>
<tr>
<td>Orange</td>
<td>Orange</td>
<td>These are orange.</td>
</tr>
<tr>
<td>Banana</td>
<td>Yellow</td>
<td>These are yellow.</td>
</tr>
<tr>
<td>Kiwi</td>
<td>Green</td>
<td>These are green.</td>
</tr>
<tr>
<td>Plum</td>
<td>Purple</td>
<td>These are Purple</td>
</tr>
<tr>
<td>Watermelon</td>
<td>Red</td>
<td>These are red.</td>
</tr>
<tr>
<td>Tomato</td>
<td>Red</td>
<td>These are red.</td>
</tr>
<tr>
<td>Cherry</td>
<td>Red</td>
<td>These are red.</td>
</tr>
<tr>
<td>Cantelope</td>
<td>Orange</td>
<td>These are orange inside.</td>
</tr>
<tr>
<td>Honeydew</td>
<td>Green</td>
<td>These are green inside.</td>
</tr>
<tr>
<td>Papaya</td>
<td>Green</td>
<td>These are green.</td>
</tr>
<tr>
<td>Raspberry</td>
<td>Red</td>
<td>These are red.</td>
</tr>
<tr>
<td>Blueberry</td>
<td>Blue</td>
<td>These are blue.</td>
</tr>
<tr>
<td>Mango</td>
<td>Orange</td>
<td>These are orange.</td>
</tr>
<tr>
<td>Passion Fruit</td>
<td>Green</td>
<td>These are green.</td>
</tr>
</tbody>
</table>
<!--[if lte IE 9]>
</div>
<!--<![endif]-->
EDIT: Alternative solution for table width 100% (above actually is for fixed width and didn't answer the question):
HTML:
<table>
<thead>
<tr>
<th>Name</th>
<th>Color</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>Apple</td>
<td>Red</td>
<td>These are red.</td>
</tr>
<tr>
<td>Pear</td>
<td>Green</td>
<td>These are green.</td>
</tr>
<tr>
<td>Grape</td>
<td>Purple / Green</td>
<td>These are purple and green.</td>
</tr>
<tr>
<td>Orange</td>
<td>Orange</td>
<td>These are orange.</td>
</tr>
<tr>
<td>Banana</td>
<td>Yellow</td>
<td>These are yellow.</td>
</tr>
<tr>
<td>Kiwi</td>
<td>Green</td>
<td>These are green.</td>
</tr>
</tbody>
</table>
CSS:
table {
width: 100%;
text-align: left;
min-width: 610px;
}
tr {
height: 30px;
padding-top: 10px
}
tbody {
height: 150px;
overflow-y: auto;
overflow-x: hidden;
}
th,td,tr,thead,tbody { display: block; }
td,th { float: left; }
td:nth-child(1),
th:nth-child(1) {
width: 20%;
}
td:nth-child(2),
th:nth-child(2) {
width: 20%;
float: left;
}
td:nth-child(3),
th:nth-child(3) {
width: 59%;
float: left;
}
/* some colors */
thead {
background-color: #333333;
color: #fdfdfd;
}
table tbody tr:nth-child(even) {
background-color: #dddddd;
}
Source: Stackoverflow.com