I would like some help, please, to align data in a table starting at specific columns and aligning to the left.
All data needs to be aligned to the left in the 3 specific columns shown, and should wrap any data that doesn't align.
Please see the current code below.
How can I change this?
I would like the following configured in the Table:
Column 1 to be a maximum of 10 characters.
Column 2 a maximum of 50 characters.
Column 3 a maximum of 10 characters.
All left aligned.
And any data that is inserted beyond the characters above should wrap.
body {
background-color: powderblue;
}
code {
background: #2db34a;
border-radius: 6px;
color: #fff;
display: block;
font: 14px/24px "Source Code Pro", Inconsolata, "Lucida Console", Terminal, "Courier New", Courier;
padding: 24px 15px;
text-align: center;
}
header,
section,
aside,
footer {
margin: 0 1.5% 24px 1.5%;
}
section {
float: left;
width: 30%;
}
footer {
clear: both;
margin-bottom: 0;
}
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<table>
<tr>
<th>Code</th>
<th>Description</th>
<th>Quantity</th>
<th class="right">Item 4</th>
</tr>
<tr>
<th>123245</th>
<th>This is a description of item 1</th>
<th>100</th>
<th class="right">Item 4</th>
</tr>
<tr>
<th>678910111213</th>
<th>This is a description of item 2 and it is longer than description 1</th>
<th>1000</th>
<th class="right">Item 4</th>
</tr>
</table>
Thanks in advance for any help provided.
I've added a bit of CSS to make this work:
th {
text-align: left;
}
tr a {
display: block;
word-wrap: break-word;
}
tr th:nth-child(1) a {
width: 10ch;
}
tr th:nth-child(2) a {
width: 42ch; // Adjusted
}
tr th:nth-child(3) a {
width: 10ch;
}
The ch unit which is based on the 0 glyph width is not perfect, which is why the 50-character limit is actually 42ch in the CSS code.
body {background-color: powderblue;}
code {
background: #2db34a;
border-radius: 6px;
color: #fff;
display: block;
font: 14px/24px "Source Code Pro", Inconsolata, "Lucida Console", Terminal, "Courier New", Courier;
padding: 24px 15px;
text-align: center;
}
header,
section,
aside,
footer {
margin: 0 1.5% 24px 1.5%;
}
section {
float: left;
width: 30%;
}
footer {
clear: both;
margin-bottom: 0;
}
th {
text-align: left;
}
tr a {
display: block;
word-wrap: break-word;
}
tr th:nth-child(1) a {
width: 10ch;
}
tr th:nth-child(2) a {
width: 42ch; // Adjusted
}
tr th:nth-child(3) a {
width: 10ch;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<table>
<tr>
<th>Code</th>
<th>Description</th>
<th>Quantity</th>
<th class="right">Item 4</th>
</tr>
<tr>
<th>Thisis10ch</th>
<th>This is an easy good nice well 50 character string</th>
<th>100</th>
<th class="right">Item 4</th>
</tr>
<tr>
<th>Thisis10ch wrapped</th>
<th>This is an easy good nice well 50 character string wrapped</th>
<th>1000</th>
<th class="right">Item 4</th>
</tr>
</table>
</body>
</html>
You may take a look at table-layout and vertical-align properties .
Possible example:
body {
background-color: powderblue;
}
table {
table-layout: fixed;
text-align: left;
border-spacing:0.5em;
}
tr>* {
max-width: 10ch;
word-break: break-all;
vertical-align:top;
}
tr>*:nth-child(2) {
max-width: 50ch;
}
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<table>
<tr>
<th>Code</th>
<th>Description</th>
<th>Quantity</th>
<th class="right">Item 4</th>
</tr>
<tr>
<th>123245</th>
<th>This is a description of item 1</th>
<th>100</th>
<th class="right">Item 4</th>
</tr>
<tr>
<th>678910111213</th>
<th>This is a description of item 2 and it is longer than description 1</th>
<th>1000</th>
<th class="right">Item 4</th>
</tr>
</table>
One approach is as follows:
body {
background-color: powderblue;
}
/* to allow the browser to split words at any point when
required to allow for a line-break: */
td {
word-break: break-all;
}
/* selecting any element that is a <th> or a <td> that
does not match the '.right' selector: */
:is(th, td):not(.right) {
/* aligning text to the logical inline start,
in a left-to-right language (such as English)
that is equivalen to 'text-align: left': */
text-align: start;
}
/* selecting all <th> and <td> elements that are
the :first-child, or the :nth-child(3),
of it's/their parent: */
:is(th, td):is(:first-child, :nth-child(3)) {
/* there are no CSS sizes exactly equal to '1 character'
as characters vary enormously in their sizing; the 'ex'
unit is equal to the width of the lower-case 'x'
character, which allows for a near-match on average to
10 characters wide (though ch is another option): */
max-width: 10ex;
}
/* selecting all <th> and <td> elements that are the second-
child of their parent: */
:is(th, td):nth-child(2) {
/* setting the width to 50ex, the width of 50 lowercase 'x'
characters to approximate a 50 character length: */
max-width: 50ex;
}
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<table>
<thead>
<tr>
<th>Code</th>
<th>Description</th>
<th>Quantity</th>
<th class="right">Item 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>123245</td>
<td>This is a description of item 1</td>
<td>100</td>
<td class="right">Item 4</td>
</tr>
<tr>
<td>678910111213</td>
<td>This is a description of item 2 and it is longer than description 1</td>
<td>1000</td>
<td class="right">Item 4</td>
</tr>
</tbody>
</table>
References:
CSS Values and Units: Lengths.
:first-chil.
:is.
:not().
:nth-child().
text-align.
word-break.
You can idea from here and try to learn HTML table. it's a very important thing.
HTML:
<table>
<thead>
<tr>
<th>Code</th>
<th>Description</th>
<th>Quantity</th>
<th>Item 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>123245
<td>This is a hello world description of item 1</td>
<td>10045467643456457647345456363653</td>
<td>Item 4</td>
</tr>
<tr>
<td>5463463
<td><a href="#">Visitors will now see your public and anonymized private
contributions.
Visitors will now see your public and anonymized private contributions.
</a></td>
<td>1004546764345645764734</td>
<td>Hello test 4</td>
</tr>
</tbody>
</table>
CSS:
body {
background-color: powderblue;
}
table * {
text-align: left;
word-break: break-all;
}
table thead th:first-child,
table tbody tr td:first-child {
width: 50px;
}
table thead th:nth-child(1) {
width: 200px;
}
table thead th:nth-child(2) {
width: 200px;
}
table thead th:nth-child(3) {
width: 200px;
}
table thead th:last-child,
table tbody td:last-child {
width: 100px;
text-align: right;
}
I got a table on my webpage, and for low resolutions, it has a horizontal scrollbar. However, the scrollbar is fixed on the bottom of the table, and with many entries, it's a bit unnecessary to always scroll down, scroll left / right and scroll back to where we were again.
My table CSS looks like that:
th,
td {
white-space: nowrap;
}
table {
table-layout: auto;
}
How is it possible to have the scrollbar always appear at the bottom of the screen, if the bottom of the table is out of view? Most questions I've found were more like "how to always show the scrollbar, even if it isn't needed", but still only at the bottom of the table.
Thanks in advance!
You could rather wrap your table in a div with overflow:auto property and make sure the bottom edge of this container never leaves viewport.
In this example the table wrap has a maximum height of 80vh.
body {
font-size: 1em;
font-family: 'Segoe UI';
margin: 1em;
}
.main {
display: flex;
flex-direction: column;
height: 80vh;
resize: both;
position: relative;
overflow: auto;
border: 1px solid red;
padding:0 1em 1em 0;
}
.main:after {
content: '';
display:block;
border: 1em solid #eee;
border-top-color: transparent;
border-left-color: transparent;
position:absolute;
bottom:0;
right:0;
}
table {
border-collapse: collapse;
white-space: nowrap;
}
th {
background-color: #eee;
}
thead {
position: sticky;
top: 0
}
th,
td {
vertical-align: top;
text-align: left;
padding-right: 0.3em;
border-bottom: 1px solid #000;
border-right: 4px solid #fff;
}
.table-wrp {
position: relative;
overflow: auto;
padding: 0 1em 1em 0;
}
<div class="main">
<div class="table-wrp">
<table>
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Col 5</th>
</tr>
</thead>
<tbody>
<tr>
<td> </td>
<td>Knocky</td>
<td>Flor</td>
<td>Ella</td>
<td>Juan</td>
</tr>
<tr>
<td>Breed</td>
<td>Jack Russell</td>
<td>Poodle</td>
<td>Streetdog</td>
<td>Cocker Spaniel</td>
</tr>
<tr>
<td>Age</td>
<td>16</td>
<td>9</td>
<td>10</td>
<td>5</td>
</tr>
<tr>
<td>Owner</td>
<td>Mother-in-law</td>
<td>Me</td>
<td>Me</td>
<td>Sister-in-law</td>
</tr>
<tr>
<td>Eating Habits</td>
<td>Eats everyone's leftovers</td>
<td>Nibbles at food</td>
<td>Hearty eater</td>
<td>Will eat till he explodes</td>
</tr>
<tr>
<td> </td>
<td>Knocky</td>
<td>Flor</td>
<td>Ella</td>
<td>Juan</td>
</tr>
<tr>
<td>Breed</td>
<td>Jack Russell</td>
<td>Poodle</td>
<td>Streetdog</td>
<td>Cocker Spaniel</td>
</tr>
<tr>
<td>Age</td>
<td>16</td>
<td>9</td>
<td>10</td>
<td>5</td>
</tr>
<tr>
<td>Owner</td>
<td>Mother-in-law</td>
<td>Me</td>
<td>Me</td>
<td>Sister-in-law</td>
</tr>
<tr>
<td>Eating Habits</td>
<td>Eats everyone's leftovers</td>
<td>Nibbles at food</td>
<td>Hearty eater</td>
<td>Will eat till he explodes</td>
</tr>
<tr>
<td> </td>
<td>Knocky</td>
<td>Flor</td>
<td>Ella</td>
<td>Juan</td>
</tr>
<tr>
<td>Breed</td>
<td>Jack Russell</td>
<td>Poodle</td>
<td>Streetdog</td>
<td>Cocker Spaniel</td>
</tr>
<tr>
<td>Age</td>
<td>16</td>
<td>9</td>
<td>10</td>
<td>5</td>
</tr>
<tr>
<td>Owner</td>
<td>Mother-in-law</td>
<td>Me</td>
<td>Me</td>
<td>Sister-in-law</td>
</tr>
<tr>
<td>Eating Habits</td>
<td>Eats everyone's leftovers</td>
<td>Nibbles at food</td>
<td>Hearty eater</td>
<td>Will eat till he explodes</td>
</tr>
</tbody>
</table>
</div>
</div>
try this :
table {
overflow: auto;
}
or
table {
overflow: scroll;
}
You could use floating-scroll jquery plugin for this.
The only solution I have found is to fill in the table with empty table cells (Shown in ROW1 and ROW7)
ROW3 through ROW6 contain only a single table cell which does not span the full table width. I've tried a fixed layout table and everything I can think of on the table-row to get it to fill in to 100% width without luck.
The reason I need the row to be full width is so that I can show top and bottom borders on the row, spanning the full width of the table.
I'm working on updating software for a client and must use tables and must find a solution that is compatible in Internet Explorer 10 minimum.
All table related elements use their natural display types, table, table-row, and table-cell
The rows are generated programmatically and colspan is a problematic solution.
Thank you for reading.
You cannot apply a border on table rows, but you can apply an outline:
tr {
outline: 1px solid black;
}
Example 1:
table {
border: 1px solid black;
border-spacing: 0;
empty-cells: show;
}
td, th {
padding: 0.5em;
border-right: 1px solid #ddd;
}
tr {
outline: 1px solid black;
}
<table>
<tr><th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Col 5</th>
<th>Col 6</th>
<th>Col 7</th>
</tr>
<tr><td>Row1</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><td>Row2</td>
</tr>
<tr><td>Row3</td>
</tr>
</table>
As you pointed out in the comments, an outline surrounds the entire element.
To have a single border at the bottom of the row, put it on a pseudo-element:
table {
position: relative; /* pseudo-element will be based on the table's position */
overflow: hidden; /* don't let the pseudo-element overflow the table */
}
tr:before {
content: ''; /* required for rendering */
position: absolute; /* absolute positioned */
width: 100%; /* span the entire table */
border-bottom: 1px solid black; /* the magic border */
}
Example 2:
table {
border: 1px solid black;
border-spacing: 0;
empty-cells: show;
}
td, th {
padding: 0.5em;
border-right: 1px solid #ddd;
}
table {
position: relative; /* the pseudo-element will be based on the table */
overflow: hidden; /* don't let the pseudo-element overflow */
}
tr:before {
content: ''; /* required for rendering */
position: absolute; /* absolute positioned */
width: 100%; /* span the entire table */
border-bottom: 1px solid black; /* the magic border */
}
<table>
<tr><th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Col 5</th>
<th>Col 6</th>
<th>Col 7</th>
</tr>
<tr><td>Row1</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><td>Row2</td>
</tr>
<tr><td>Row3</td>
</tr>
</table>
Apply colspan on the cell:
<table border="1">
<tr>
<td>oi</td>
<td>oi</td>
<td>oi</td>
<td>oi</td>
<td>oi</td>
</tr>
<tr>
<td colspan="5">oi</td>
</tr>
<tr>
<td>oi</td>
<td>oi</td>
<td>oi</td>
<td>oi</td>
<td>oi</td>
</tr>
</table>
I am writing a page where I need an html table to maintain a set size. I need the headers at the top of the table to stay there at all times but I also need the body of the table to scroll no matter how many rows are added to the table.
I want it to look like method 2 in this url: http://www.cssplay.co.uk/menu/tablescroll.html
I have tried doing this but no scrollbar appears:
tbody {
height: 80em;
overflow: scroll;
}
<table border=1 id="qandatbl" align="center">
<tr>
<th class="col1">Question No</th>
<th class="col2">Option Type</th>
<th class="col1">Duration</th>
</tr>
<tbody>
<tr>
<td class='qid'></td>
<td class="options"></td>
<td class="duration"></td>
</tr>
</tbody>
</table>
Something like this?
http://jsfiddle.net/TweNm/
The idea is to wrap the <table> in a non-statically positioned <div> which has an overflow:auto CSS property. Then position the elements in the <thead> absolutely.
#table-wrapper {
position:relative;
}
#table-scroll {
height:150px;
overflow:auto;
margin-top:20px;
}
#table-wrapper table {
width:100%;
}
#table-wrapper table * {
background:yellow;
color:black;
}
#table-wrapper table thead th .text {
position:absolute;
top:-20px;
z-index:2;
height:20px;
width:35%;
border:1px solid red;
}
<div id="table-wrapper">
<div id="table-scroll">
<table>
<thead>
<tr>
<th><span class="text">A</span></th>
<th><span class="text">B</span></th>
<th><span class="text">C</span></th>
</tr>
</thead>
<tbody>
<tr> <td>1, 0</td> <td>2, 0</td> <td>3, 0</td> </tr>
<tr> <td>1, 1</td> <td>2, 1</td> <td>3, 1</td> </tr>
<tr> <td>1, 2</td> <td>2, 2</td> <td>3, 2</td> </tr>
<tr> <td>1, 3</td> <td>2, 3</td> <td>3, 3</td> </tr>
<tr> <td>1, 4</td> <td>2, 4</td> <td>3, 4</td> </tr>
<tr> <td>1, 5</td> <td>2, 5</td> <td>3, 5</td> </tr>
<tr> <td>1, 6</td> <td>2, 6</td> <td>3, 6</td> </tr>
<tr> <td>1, 7</td> <td>2, 7</td> <td>3, 7</td> </tr>
<tr> <td>1, 8</td> <td>2, 8</td> <td>3, 8</td> </tr>
<tr> <td>1, 9</td> <td>2, 9</td> <td>3, 9</td> </tr>
<tr> <td>1, 10</td> <td>2, 10</td> <td>3, 10</td> </tr>
<!-- etc... -->
<tr> <td>1, 99</td> <td>2, 99</td> <td>3, 99</td> </tr>
</tbody>
</table>
</div>
</div>
You have to insert your <table> into a <div> that it has fixed size, and in <div> style you have to set overflow: scroll.
Update:
The original answer was written 10 years ago. These days there are lots of good UI components for table views and showing in proper ways. So my suggestion is to go for one of these free or paid components to make sure you already support lots of edge cases which is already implemented in these components.
The accepted answer provided a good starting point, but if you resize the frame, change the column widths, or even change the table data, the headers will get messed up in various ways. Every other example I've seen has similar issues, or imposes some serious restrictions on the table's layout.
I think I've finally got all these problems solved, though. It took a lot of CSS, but the final product is about as reliable and easy to use as a normal table.
Here's an example that has all the required features to replicate the table referenced by the OP: jsFiddle
The colors and borders would have to be changed to make it identical to the reference. Information on how to make those kinds of changes is provided in the CSS comments.
Here's the code:
/*the following html and body rule sets are required only if using a % width or height*/
/*html {
width: 100%;
height: 100%;
}*/
body {
box-sizing: border-box;
width: 100%;
height: 100%;
margin: 0;
padding: 0 20px 0 20px;
text-align: center;
background: white;
}
.scrollingtable {
box-sizing: border-box;
display: inline-block;
vertical-align: middle;
overflow: hidden;
width: auto; /*if you want a fixed width, set it here, else set to auto*/
min-width: 0/*100%*/; /*if you want a % width, set it here, else set to 0*/
height: 188px/*100%*/; /*set table height here; can be fixed value or %*/
min-height: 0/*104px*/; /*if using % height, make this large enough to fit scrollbar arrows + caption + thead*/
font-family: Verdana, Tahoma, sans-serif;
font-size: 15px;
line-height: 20px;
padding: 20px 0 20px 0; /*need enough padding to make room for caption*/
text-align: left;
}
.scrollingtable * {box-sizing: border-box;}
.scrollingtable > div {
position: relative;
border-top: 1px solid black;
height: 100%;
padding-top: 20px; /*this determines column header height*/
}
.scrollingtable > div:before {
top: 0;
background: cornflowerblue; /*header row background color*/
}
.scrollingtable > div:before,
.scrollingtable > div > div:after {
content: "";
position: absolute;
z-index: -1;
width: 100%;
height: 100%;
left: 0;
}
.scrollingtable > div > div {
min-height: 0/*43px*/; /*if using % height, make this large enough to fit scrollbar arrows*/
max-height: 100%;
overflow: scroll/*auto*/; /*set to auto if using fixed or % width; else scroll*/
overflow-x: hidden;
border: 1px solid black; /*border around table body*/
}
.scrollingtable > div > div:after {background: white;} /*match page background color*/
.scrollingtable > div > div > table {
width: 100%;
border-spacing: 0;
margin-top: -20px; /*inverse of column header height*/
/*margin-right: 17px;*/ /*uncomment if using % width*/
}
.scrollingtable > div > div > table > caption {
position: absolute;
top: -20px; /*inverse of caption height*/
margin-top: -1px; /*inverse of border-width*/
width: 100%;
font-weight: bold;
text-align: center;
}
.scrollingtable > div > div > table > * > tr > * {padding: 0;}
.scrollingtable > div > div > table > thead {
vertical-align: bottom;
white-space: nowrap;
text-align: center;
}
.scrollingtable > div > div > table > thead > tr > * > div {
display: inline-block;
padding: 0 6px 0 6px; /*header cell padding*/
}
.scrollingtable > div > div > table > thead > tr > :first-child:before {
content: "";
position: absolute;
top: 0;
left: 0;
height: 20px; /*match column header height*/
border-left: 1px solid black; /*leftmost header border*/
}
.scrollingtable > div > div > table > thead > tr > * > div[label]:before,
.scrollingtable > div > div > table > thead > tr > * > div > div:first-child,
.scrollingtable > div > div > table > thead > tr > * + :before {
position: absolute;
top: 0;
white-space: pre-wrap;
color: white; /*header row font color*/
}
.scrollingtable > div > div > table > thead > tr > * > div[label]:before,
.scrollingtable > div > div > table > thead > tr > * > div[label]:after {content: attr(label);}
.scrollingtable > div > div > table > thead > tr > * + :before {
content: "";
display: block;
min-height: 20px; /*match column header height*/
padding-top: 1px;
border-left: 1px solid black; /*borders between header cells*/
}
.scrollingtable .scrollbarhead {float: right;}
.scrollingtable .scrollbarhead:before {
position: absolute;
width: 100px;
top: -1px; /*inverse border-width*/
background: white; /*match page background color*/
}
.scrollingtable > div > div > table > tbody > tr:after {
content: "";
display: table-cell;
position: relative;
padding: 0;
border-top: 1px solid black;
top: -1px; /*inverse of border width*/
}
.scrollingtable > div > div > table > tbody {vertical-align: top;}
.scrollingtable > div > div > table > tbody > tr {background: white;}
.scrollingtable > div > div > table > tbody > tr > * {
border-bottom: 1px solid black;
padding: 0 6px 0 6px;
height: 20px; /*match column header height*/
}
.scrollingtable > div > div > table > tbody:last-of-type > tr:last-child > * {border-bottom: none;}
.scrollingtable > div > div > table > tbody > tr:nth-child(even) {background: gainsboro;} /*alternate row color*/
.scrollingtable > div > div > table > tbody > tr > * + * {border-left: 1px solid black;} /*borders between body cells*/
<div class="scrollingtable">
<div>
<div>
<table>
<caption>Top Caption</caption>
<thead>
<tr>
<th><div label="Column 1"></div></th>
<th><div label="Column 2"></div></th>
<th><div label="Column 3"></div></th>
<th>
<!--more versatile way of doing column label; requires 2 identical copies of label-->
<div><div>Column 4</div><div>Column 4</div></div>
</th>
<th class="scrollbarhead"/> <!--ALWAYS ADD THIS EXTRA CELL AT END OF HEADER ROW-->
</tr>
</thead>
<tbody>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
</tbody>
</table>
</div>
Faux bottom caption
</div>
</div>
<!--[if lte IE 9]><style>.scrollingtable > div > div > table {margin-right: 17px;}</style><![endif]-->
Not sure why no one mentioned to just use the built-in sticky header style for elements. Worked great for me.
.tableContainerDiv {
overflow: auto;
max-height: 80em;
}
th {
position: sticky;
top: 0;
background: white;
}
Put a min-width on the in #media if you need to make responsive (or similar).
see Table headers position:sticky or Position Sticky and Table Headers
I resolved this problem by separating my content into two tables.
One table is the header row.
The seconds is also <table> tag, but wrapped by <div> with static height and overflow scroll.
Worth noting, that depending on your purpose (mine was the autofill results of a searchbar) you may want the height to be changeable, and for the scrollbar to only exist if the height exceeds that.
If you want that, replace height: x; with max-height: x;, and overflow:scroll with overflow:auto.
Additionally, you can use overflow-x and overflow-y if you want, and obviously the same thing works horizontally with width : x;
If you get to the point where all the mentioned solutions don't work (as it got for me), do this:
Create two tables. One for the header and another for the body
Give the two tables different parent containers/divs
Style the second table's div to allow vertical scroll of its contents.
Like this, in your HTML
<div class="table-header-class">
<table>
<thead>
<tr>
<th>Ava</th>
<th>Alexis</th>
<th>Mcclure</th>
</tr>
</thead>
</table>
</div>
<div class="table-content-class">
<table>
<tbody>
<tr>
<td>I am the boss</td>
<td>No, da-da is not the boss!</td>
<td>Alexis, I am the boss, right?</td>
</tr>
</tbody>
</table>
</div>
Then style the second table's parent to allow vertical scroll, in your CSS
.table-content-class {
overflow-y: scroll; // use auto; or scroll; to allow vertical scrolling;
overflow-x: hidden; // disable horizontal scroll
}
Very easy, just wrap the table in a div that has overflow-y:scroll; and overflow-x:scroll properties, and make the div have a width and length smaller than the table.
IT WILL WORK!!!
The CSS:
div{ overflow-y:scroll; overflow-x:scroll; width:20px; height:30px; } table{ width:50px; height:50px; }
You can make the table and the DIV around the table be any size you want, just make sure that the DIV is smaller than the table.
You MUST contain the table inside of the DIV.
just add on table
style="overflow-x:auto;"
<table border=1 id="qandatbl" align="center" style="overflow-x:auto;">
<tr>
<th class="col1">Question No</th>
<th class="col2">Option Type</th>
<th class="col1">Duration</th>
</tr>
<tbody>
<tr>
<td class='qid'></td>
<td class="options"></td>
<td class="duration"></td>
</tr>
</tbody>
</table>
style="overflow-x:auto;"`
Here is my CSS code for table:
table.t_data
{
/* border: 1px; - **EDITED** - doesn't seem like influences here */
background-color: #080;
border-spacing: 1px;
margin: 0 auto 0 auto;
}
table.t_data thead th, table.t_data thead td
{
background-color: #9f9;
/* border: #080 0px solid; - **EDITED** - doesn't seem like influences here */
padding: 5px;
margin: 1px;
}
table.t_data tbody th, table.t_data tbody td
{
background-color: #fff;
/* border: #080 0px solid; - **EDITED** - doesn't seem like influences here */
padding: 2px;
}
I need to display the following HTML:
<table class="t_data">
<thead style="padding:1px">
<tr>
<th>#</th>
<th>Team</th>
<th>Stadium Name</th>
<th>Size</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Team 1</td>
<td>Name 1</td>
<td>Size 1-1, 2-1, 3-1</td>
</tr>
<tr>
<td>2</td>
<td>Team 1</td>
<td>Name 1</td>
<td>Size 1-1, 2-1, 3-1</td>
</tr>
<tr>
<td>3</td>
<td>Team 1</td>
<td>Name 1</td>
<td>Size 1-1, 2-1, 3-1</td>
</tr>
</tbody>
</table>
It display perfect (as expected) table in Mozilla Firefox and in IE8:
But there are issues in other browsers/modes:
In Chrome the line between head and body is double width:
In IE8 (switched into IE7 compatibility mode) all lines are double width:
Question: what are CSS options to make each line boldness same (1px)?
table.t_data { border-collapse:collapse }