I've tried every possible solution to get this to work, but nothing seems to be working.
I have this webpage I've created, and I have a table shown here, and I want to have my header fixed. That part I have mastered, but the thead columns dont align to the tbody columns.
I have tried assigning width to my td's manually, that didn't work either.
This is my code so far:
.tableSqlContent {
table-layout: fixed;
border-collapse: collapse;
}
.tableSqlContent th, td {
padding: 7px;
text-align: left;
}
.tableSqlContent thead {
background-color: #003265;
color: white;
font-weight: bold;
cursor: default;
}
.tableSqlContent thead tr {
display:inline-block;
position: relative;
height: 37px;
}
.tableSqlContent tbody tr td:nth-child(1) {min-width: 33%;}
.tableSqlContent tbody tr td:nth-child(2) {min-width: 20%;}
.tableSqlContent tbody tr td:nth-child(3) {min-width: 20%;}
.tableSqlContent tbody tr td:nth-child(4) {min-width: 10%;}
.tableSqlContent tbody tr td:nth-child(5) {min-width: 10%;}
.tableSqlContent tbody tr td:nth-child(6) {min-width: 7%;}
.tableSqlContent thead tr th:nth-child(1) {min-width: 33%;}
.tableSqlContent thead tr th:nth-child(2) {min-width: 20%;}
.tableSqlContent thead tr th:nth-child(3) {min-width: 20%;}
.tableSqlContent thead tr th:nth-child(4) {min-width: 10%;}
.tableSqlContent thead tr th:nth-child(5) {min-width: 10%;}
.tableSqlContent thead tr th:nth-child(6) {min-width: 7%;}
.tableSqlContent tbody {
display: block;
height: 700px;
overflow: auto;
}
.tableSqlContent tbody tr:nth-child(even) {
background-color: #eeeeee;
}
.tableSqlContent tbody tr:hover {
color: #003265;
cursor: pointer;
background-color: #dddddd;
}
<table Class="tableSqlContent">
<thead>
<tr>
<th>Connectionstring</th>
<th>Klinik Navn</th>
<th>IP_Adresse</th>
<th>P-nummer</th>
<th>Systemtype</th>
<th>Version</th>
</tr>
</thead>
<tbody>
<?php
foreach($dbh->query($query) as $rows){
?>
<tr>
<td><?php echo $rows['ConnectionString']?></td>
<td><?php echo $rows['Name']?></td>
<td><?php echo $rows['IP_Adresse']?></td>
<td><?php echo $rows['Ydernummer']?></td>
<td><?php echo $rows['Systemtype']?></td>
<td><?php echo $rows['Version']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
This is how it looks:
This seems to be working.
.fixed_header {
width: 100%;
table-layout: fixed;
border-collapse: collapse;
}
.fixed_header tbody {
display: block;
overflow: auto;
height: 200px;
width: 100%;
}
.fixed_header thead {
background: black;
color: #fff;
}
.fixed_header thead tr {
display: block;
}
.fixed_header th,
.fixed_header td {
text-align: left;
width: 100px;
max-width: 100px;
}
<table class="fixed_header">
<thead>
<tr>
<th>Connection string</th>
<th>Klinik Navn</th>
<th>IP_Adresse</th>
<th>P-nummer</th>
<th>Systemtype</th>
<th>Version</th>
</tr>
</thead>
<tbody>
<tr>
<td>row 1-0</td>
<td>row 1-1</td>
<td>row 1-2</td>
<td>row 1-3</td>
<td>row 1-4</td>
</tr>
<tr>
<td>row 2-0</td>
<td>row 2-1</td>
<td>row 2-2</td>
<td>row 2-3</td>
<td>row 2-4</td>
</tr>
<tr>
<td>row 3-0</td>
<td>row 3-1</td>
<td>row 3-2</td>
<td>row 3-3</td>
<td>row 3-4</td>
</tr>
<tr>
<td>row 4-0</td>
<td>row 4-1</td>
<td>row 4-2</td>
<td>row 4-3</td>
<td>row 4-4</td>
</tr>
<tr>
<td>row 5-0</td>
<td>row 5-1</td>
<td>row 5-2</td>
<td>row 5-3</td>
<td>row 5-4</td>
</tr>
<tr>
<td>row 6-0</td>
<td>row 6-1</td>
<td>row 6-2</td>
<td>row 6-3</td>
<td>row 6-4</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
<td>row 7-3</td>
<td>row 7-4</td>
</tr>
<tr>
<td>row 8-0</td>
<td>row 8-1</td>
<td>row 8-2</td>
<td>row 8-3</td>
<td>row 8-4</td>
</tr>
<tr>
<td>row 9-0</td>
<td>row 9-1</td>
<td>row 9-2</td>
<td>row 9-3</td>
<td>row 9-4</td>
</tr>
<tr>
<td>row 10-0</td>
<td>row 10-1</td>
<td>row 10-2</td>
<td>row 10-3</td>
<td>row 10-4</td>
</tr>
<tr>
<td>row 11-0</td>
<td>row 11-1</td>
<td>row 11-2</td>
<td>row 11-3</td>
<td>row 11-4</td>
</tr>
<tr>
<td>row 12-0</td>
<td>row 12-1</td>
<td>row 12-2</td>
<td>row 12-3</td>
<td>row 12-4</td>
</tr>
<tr>
<td>row 13-0</td>
<td>row 13-1</td>
<td>row 13-2</td>
<td>row 13-3</td>
<td>row 13-4</td>
</tr>
<tr>
<td>row 14-0</td>
<td>row 14-1</td>
<td>row 14-2</td>
<td>row 14-3</td>
<td>row 14-4</td>
</tr>
<tr>
<td>row 15-0</td>
<td>row 15-1</td>
<td>row 15-2</td>
<td>row 15-3</td>
<td>row 15-4</td>
</tr>
<tr>
<td>row 16-0</td>
<td>row 16-1</td>
<td>row 16-2</td>
<td>row 16-3</td>
<td>row 16-4</td>
</tr>
<tr>
<td>row 17-0</td>
<td>row 17-1</td>
<td>row 17-2</td>
<td>row 17-3</td>
<td>row 17-4</td>
</tr>
<tr>
<td>row 18-0</td>
<td>row 18-1</td>
<td>row 18-2</td>
<td>row 18-3</td>
<td>row 18-4</td>
</tr>
<tr>
<td>row 19-0</td>
<td>row 19-1</td>
<td>row 19-2</td>
<td>row 19-3</td>
<td>row 19-4</td>
</tr>
<tr>
<td>row 20-0</td>
<td>row 20-1</td>
<td>row 20-2</td>
<td>row 20-3</td>
<td>row 20-4</td>
</tr>
<tr>
<td>row 21-0</td>
<td>row 21-1</td>
<td>row 21-2</td>
<td>row 21-3</td>
<td>row 21-4</td>
</tr>
</tbody>
</table>
You need to remove some css because is in conflict:
.tableSqlContent {
table-layout: fixed;
border-collapse: collapse;
overflow: auto;
}
.tableSqlContent th, td {
padding: 7px;
text-align: left;
}
.tableSqlContent thead {
background-color: #003265;
color: white;
font-weight: bold;
cursor: default;
}
.tableSqlContent tbody tr:nth-child(even) {
background-color: #eeeeee;
}
.tableSqlContent tbody tr:hover {
color: #003265;
cursor: pointer;
background-color: #dddddd;
}
thead th {
position: sticky;
position: -webkit-sticky;
top: 0px;
z-index: 999999;
background-color: #003265;
}
<table class="tableSqlContent">
<thead>
<tr>
<th>Connectionstring</th>
<th>Klinik Navn</th>
<th>IP_Adresse</th>
<th>P-nummer</th>
<th>Systemtype</th>
<th>Version</th>
</tr>
</thead>
<tbody>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
<tr>
<td>1111</td>
<td>2222</td>
<td>3333</td>
<td>4444</td>
<td>5555</td>
<td>6666</td>
</tr>
</tbody>
</table>
hope it helps
-- added css for sticky header.
Related
I'm trying to achieve the following effect: I have a large table, generated from a mysql query, and I want the top rows to stay on the page, while the body of the table scrolls vertically.
I've managed to get that working, BUT I also want the entire table to be horizontally scrollable, because part of it is off the right edge of the window.
I've been working on this for days, and I'd be grateful for some help.
Here's what I've come up with so far:
My table looks like this:
<div id="topLines">
<div id="controlLine">
<div id="testheader">
This text is fixed!
</div>
</div>
</div>
<div class="resetFloat"></div>
<div id="memberTable">
<div id="tableHeader">
<div class="columnTitle" style="width: 100px;">Title 1</div>
<div class="columnTitle" style="width: 100px;">Title 2</div>
<div class="columnTitle" style="width: 100px;">Title 3</div>
.
.
.
<div class="columnTitle" style="width: 100px;">Title n</div>
</div>
<div class="resetFloat"></div>
<div id="tableBody">
<div class="tableRow">
<div class="tableCell" style="width: 100px;">Cell 1-1</div>
<div class="tableCell" style="width: 100px;">Cell 1-2</div>
.
.
</div>
<div class="resetFloat"></div>
<div class="tableRow">
<div class="tableCell" style="width: 100px;">Cell 2-1</div>
.
.
</div>
<div class="resetFloat"></div>
<div class="tableRow">
<div class="tableCell" style="width: 100px;">Cell 3-1</div>
.
.
</div>
<div class="resetFloat"></div>
<div class="tableRow">
<div class="tableCell" style="width: 100px;">Cell 4-1</div>
.
.
</div>
<div class="resetFloat"></div>
.
.
</div>
</div>
Here's my CSS:
.resetFloat{
clear: both;
}
#topLines{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 50px;
overflow: hidden;
}
#controlLine{
float: left;
width: 5000px;
height: 50px;
}
#memberTable{
float: left;
position: fixed;
top: 50px;
width: 5000px;
overflow-x: scroll;
overflow-y: hidden;
}
#tableHeader{
position: fixed;
top: 60px;
float: left;
height: 45px;
width: 8000px;
border-bottom: solid 1px white;
}
.columnTitle{
float: left;
text-align: center;
border-right: solid 1px white;
height: 45px;
}
#tableBody{
position: fixed;
top: 120px; /*Set top value to HeightOfTopFrameDiv*/
left: 0;
bottom: 0;
overflow-y: scroll;
width: 5000px;
}
.tableRow{
float: left;
width 5000px;
}
.tableCell{
float: left;
height: 30px;
border-right: solid 1px white;
}
body{
margin:0}
/* table with a fixed head*/
.table-holder {
position:fixed;
top:40pt;
left:0;right:0;
bottom:0;
overflow-y: scroll;
}
.table-fixed-head {
position: relative;
width: 100%
}
.table-fixed-head th {
position: sticky;
top: 0;
background-color: #efefef;
text-align: left;
}
/* make top lines fixed */
#topLines{
height:40pt;
position:fixed;
left:0;
right:0;
background:#aaa;
z-index:20
}
<!--options-->
<div id="topLines">
<div id="controlLine">
<div id="testheader">
This text is fixed!
</div>
</div>
</div>
<!--table-->
<div class="table-holder">
<table class="table-fixed-head">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>row 1</td>
<td>row 1</td>
<td>row 1</td>
<td>row 1</td>
</tr>
<tr>
<td>row 2</td>
<td>row 2</td>
<td>row 2</td>
<td>row 2</td>
</tr>
<tr>
<td>row 2</td>
<td>row 2</td>
<td>row 2</td>
<td>row 2</td>
</tr>
<tr>
<td>row 2</td>
<td>row 2</td>
<td>row 2</td>
<td>row 2</td>
</tr>
<tr>
<td>row 2</td>
<td>row 2</td>
<td>row 2</td>
<td>row 2</td>
</tr>
<tr>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
</tr>
<tr>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
</tr>
<tr>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
</tr>
<tr>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
</tr>
<tr>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
</tr>
<tr>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
</tr>
<tr>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
</tr>
<tr>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
</tr>
<tr>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
</tr>
<tr>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
</tr>
<tr>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
<td>row 3</td>
</tr>
</tbody>
</table>
</div>
I am building a table using only divs with the help of the css table display property(ies). I need my table to be flexible so I cannot use <table>.
Everything works fine except for .table-body. I set the display property to table-row-group so it behaves like a <tbody> but I want it to be scrollable. I set its overflow property to auto and it still does not work.
For example, I have this table:
<div class="table">
<div class="table-header">
<div class="table-row">
<div class="table-cell">Header 1</div>
<div class="table-cell">Header 2</div>
<div class="table-cell">Header 3</div>
</div>
</div>
<div class="table-body">
<div class="table-row">
<div class="table-cell">cell 1</div>
<div class="table-cell">cell 2</div>
<div class="table-cell">cell 3</div>
</div>
</div>
</div>
And this is the css for .table-body. I added a display property of table-row-group so it behaves like a <tbody>. It still does not have a scroll even with overflow set to auto.
.table-body {
display: table-row-group; // makes it behave like <tbody>
height: 100%;
overflow: auto; // does nothing
}
We need the following steps in order to achieve vertical scrolling:
1. Setting <tbody> to display:block so that we can apply the height and overflow property.
2. Setting the <thead><tr> to display:block.
But, since you are already using display property to set to table-related display values, I am not sure how can we apply two display values to a div.
You can achieve what you need using a slightly different approach though, like:
.fixed_header{
width: 400px;
table-layout: fixed;
border-collapse: collapse;
}
.fixed_header tbody{
display:block;
width: 100%;
overflow: auto;
height: 100px;
}
.fixed_header thead tr {
display: block;
}
.fixed_header thead {
background: black;
color:#fff;
}
.fixed_header th, .fixed_header td {
padding: 5px;
text-align: left;
width: 200px;
}
<table class="fixed_header">
<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>row 1-0</td>
<td>row 1-1</td>
<td>row 1-2</td>
<td>row 1-3</td>
<td>row 1-4</td>
</tr>
<tr>
<td>row 2-0</td>
<td>row 2-1</td>
<td>row 2-2</td>
<td>row 2-3</td>
<td>row 2-4</td>
</tr>
<tr>
<td>row 3-0</td>
<td>row 3-1</td>
<td>row 3-2</td>
<td>row 3-3</td>
<td>row 3-4</td>
</tr>
<tr>
<td>row 4-0</td>
<td>row 4-1</td>
<td>row 4-2</td>
<td>row 4-3</td>
<td>row 4-4</td>
</tr>
<tr>
<td>row 5-0</td>
<td>row 5-1</td>
<td>row 5-2</td>
<td>row 5-3</td>
<td>row 5-4</td>
</tr>
<tr>
<td>row 6-0</td>
<td>row 6-1</td>
<td>row 6-2</td>
<td>row 6-3</td>
<td>row 6-4</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
<td>row 7-3</td>
<td>row 7-4</td>
</tr>
</tbody>
</table>
Hope this helps.
I have an issue with material tables actually, but I think it is a problem with other tables also.
Don't know why but position sticky is not working in fiddle, so I didn't include it, but on my solution it works perfectly in Chrome Browser.
So, what I need:
I have couple of tables (2-4) which should have vertical-x scroll visible every time. It should not go out of screen;
All headers should be fixed to top while scrolling y axis;
I have some data above the tables so some viewport should be in place;
Of course this should be working in IE, because in Chrome it is working with position: sticky as I mentioned.
Please, help. I can't make one table from multiple ones and struggling wih this already for couple days.
Here is my fiddle: https://jsfiddle.net/genyklemberg/n5s8h3mj/7/
CSS:
.container {
overflow-y: scroll;
overflow-x: hidden;
height: 200px;
width: 80%;
}
.wrapper {
display: flex;
flex-direction: row;
overflow-x: scroll;
}
html {
font-family: verdana;
font-size: 10pt;
line-height: 25px;
}
table {
border-collapse: collapse;
}
thead {
background-color: #EFEFEF;
}
thead tr {
position: sticky;
}
thead, tbody {
display: block;
}
td, th {
min-width: 50px;
height: 25px;
border: dashed 1px lightblue;
overflow-x: hidden;
text-overflow: ellipsis;
max-width: 100px;
}
Html:
<div>
<h2>Another div here</h2>
</div>
<div class="container">
<div class="wrapper">
<table>
<thead>
<tr>
<th>One</th>
<th>Two</th>
<th>Three</th>
<th>Four</th>
<th>Five</th>
</tr>
</thead>
<tbody>
<tr>
<td>AAAAAA</td>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
</tr>
<tr>
<td>Row 3</td>
<td>Row 3</td>
<td>Row 3</td>
<td>Row 3</td>
<td>Row 3</td>
</tr>
<tr>
<td>Row 4</td>
<td>Row 4</td>
<td>Row 4</td>
<td>Row 4</td>
<td>Row 4</td>
</tr>
<tr>
<td>Row 5</td>
<td>Row 5</td>
<td>Row 5</td>
<td>Row 5</td>
<td>Row 5</td>
</tr>
<tr>
<td>Row 6</td>
<td>Row 6</td>
<td>Row 6</td>
<td>Row 6</td>
<td>Row 6</td>
</tr>
<tr>
<td>Row 7</td>
<td>Row 7</td>
<td>Row 7</td>
<td>Row 7</td>
<td>Row 7</td>
</tr>
<tr>
<td>Row 8</td>
<td>Row 8</td>
<td>Row 8</td>
<td>Row 8</td>
<td>Row 8</td>
</tr>
<tr>
<td>Row 9</td>
<td>Row 9</td>
<td>Row 9</td>
<td>Row 9</td>
<td>Row 9</td>
</tr>
<tr>
<td>Row 10</td>
<td>Row 10</td>
<td>Row 10</td>
<td>Row 10</td>
<td>Row 10</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>One</th>
<th>Two</th>
<th>Three</th>
<th>Four</th>
<th>Five</th>
</tr>
</thead>
<tbody>
<tr>
<td>AAAAAAAAAAAAAAAAAAAAAAAAAA</td>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
</tr>
<tr>
<td>Row 3</td>
<td>Row 3</td>
<td>Row 3</td>
<td>Row 3</td>
<td>Row 3</td>
</tr>
<tr>
<td>Row 4</td>
<td>Row 4</td>
<td>Row 4</td>
<td>Row 4</td>
<td>Row 4</td>
</tr>
<tr>
<td>Row 5</td>
<td>Row 5</td>
<td>Row 5</td>
<td>Row 5</td>
<td>Row 5</td>
</tr>
<tr>
<td>Row 6</td>
<td>Row 6</td>
<td>Row 6</td>
<td>Row 6</td>
<td>Row 6</td>
</tr>
<tr>
<td>Row 7</td>
<td>Row 7</td>
<td>Row 7</td>
<td>Row 7</td>
<td>Row 7</td>
</tr>
<tr>
<td>Row 8</td>
<td>Row 8</td>
<td>Row 8</td>
<td>Row 8</td>
<td>Row 8</td>
</tr>
<tr>
<td>Row 9</td>
<td>Row 9</td>
<td>Row 9</td>
<td>Row 9</td>
<td>Row 9</td>
</tr>
<tr>
<td>Row 10</td>
<td>Row 10</td>
<td>Row 10</td>
<td>Row 10</td>
<td>Row 10</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>One</th>
<th>Two</th>
<th>Three</th>
<th>Four</th>
<th>Five</th>
</tr>
</thead>
<tbody>
<tr>
<td>AAAAAAAAAAAAAAAAAAAAAAAAAA</td>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
</tr>
<tr>
<td>Row 3</td>
<td>Row 3</td>
<td>Row 3</td>
<td>Row 3</td>
<td>Row 3</td>
</tr>
<tr>
<td>Row 4</td>
<td>Row 4</td>
<td>Row 4</td>
<td>Row 4</td>
<td>Row 4</td>
</tr>
<tr>
<td>Row 5</td>
<td>Row 5</td>
<td>Row 5</td>
<td>Row 5</td>
<td>Row 5</td>
</tr>
<tr>
<td>Row 6</td>
<td>Row 6</td>
<td>Row 6</td>
<td>Row 6</td>
<td>Row 6</td>
</tr>
<tr>
<td>Row 7</td>
<td>Row 7</td>
<td>Row 7</td>
<td>Row 7</td>
<td>Row 7</td>
</tr>
<tr>
<td>Row 8</td>
<td>Row 8</td>
<td>Row 8</td>
<td>Row 8</td>
<td>Row 8</td>
</tr>
<tr>
<td>Row 9</td>
<td>Row 9</td>
<td>Row 9</td>
<td>Row 9</td>
<td>Row 9</td>
</tr>
<tr>
<td>Row 10</td>
<td>Row 10</td>
<td>Row 10</td>
<td>Row 10</td>
<td>Row 10</td>
</tr>
</tbody>
</table>
</div>
</div>
I saw this answer Fixed header table with horizontal scrollbar and vertical scrollbar on but couldn't figure out how it could be implemented for multiple tables scroll. Its better also to have VanillaJS answer if some script is needed.
Can someone advise on why the columns aren't resizing according to their %s in colgroup?
PROBLEM: Make something like 50% first column 25% second column and third. While preserving display block on the tbody, such that we have scroll on the body not the whole table. SOLUTION: Leave in all the code like is below, but use td:nth-child property to manually set % width instead of using colgroup (because it required that display is not block but table-row-group).
I have tried using '3*','1*','1*' for the col width as well, to no avail. I think it must have to do something with the fact that I am placing the table inside a div container or due to display:block property, perhaps it has to be display: table. But when I do display: table, then table takes up only 50% of the container space and floats to left and columns are still of equal width. Thanks for any help!
EDIT: I have also tried style="width: 100%" on the table.
EDIT EDIT: removing display:block from .fixed_header thead tr and .fixed_header tbody fixes the issue for the header. Also, setting width:100% in .fixed_header th, .fixed_header td almost fixes it, it is a little bit misalligned.
.table-container {
position: absolute;
display: block;
height: 94%;
width: 100%;
margin-top: 10px;
padding: 10px;
top: 15px;
}
// https://codepen.io/GhostRider/pen/GHaFw
.style-2::-webkit-scrollbar-track
{
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
border-radius: 10px;
background-color: #F5F5F5;
}
.style-2::-webkit-scrollbar
{
width: 12px;
background-color: #F5F5F5;
}
.style-2::-webkit-scrollbar-thumb
{
border-radius: 10px;
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
background-color: #F4F7F7;
}
table, tbody {
width: 100%;
height: 100%;
position: relative;
display: inline-block;
table-layout: fixed;
}
.fixed_header{
width: 100%;
table-layout: fixed;
border-collapse: collapse;
}
.fixed_header tbody{
display:block;
width: 100%;
overflow-y: auto;
height: 90%;
}
.fixed_header thead tr {
display: block;
border-bottom: 1px solid black;
}
.fixed_header th, .fixed_header td {
padding: 5px;
text-align: left;
width: 200px;
}
<div class="table-container">
<table class="fixed_header">
<colgroup style="width: 100%;">
<col span="1" style="width: 50%;">
<col span="1" style="width: 25%;">
<col span="1" style="width: 25%;">
</colgroup>
<thead>
<tr>
<th>Factor</th>
<th>y_i</th>
<th>F_i</th>
</tr>
</thead>
<tbody class="style-2">
<tr>
<td>row 1-0</td>
<td>row 1-1</td>
<td>row 1-2</td>
</tr>
<tr>
<td>row 2-0</td>
<td>row 2-1</td>
<td>row 2-2</td>
</tr>
<tr>
<td>row 3-0</td>
<td>row 3-1</td>
<td>row 3-2</td>
</tr>
<tr>
<td>row 4-0</td>
<td>row 4-1</td>
<td>row 4-2</td>
</tr>
<tr>
<td>row 5-0</td>
<td>row 5-1</td>
<td>row 5-2</td>
</tr>
<tr>
<td>row 6-0</td>
<td>row 6-1</td>
<td>row 6-2</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
</tr>
</tbody>
</table>
</div>
Removing the below classes solved the issue.
table, tbody {
width: 100%;
height: 100%;
position: relative;
display: inline-block;
table-layout: fixed;
}
.fixed_header tbody{
display:block;
width: 100%;
overflow-y: auto;
height: 90%;
}
.fixed_header thead tr {
display: block;
border-bottom: 1px solid black;
}
I see the default display table-row-group of the tbody has been replaced with display:inline-block . My guess is that these display properties seen below make elements behave like tables and their children and consequently apply table grouping rules on them. Fiddle here
display: table;
display: table-cell;
display: table-column;
display: table-colgroup;
display: table-header-group;
display: table-row-group;
display: table-footer-group;
display: table-row;
display: table-caption;
<table style="width: 100%">
<colgroup>
<col span="1" style="width: 50%;">
<col span="1" style="width: 25%;">
<col span="1" style="width: 25%;">
</colgroup>
<!-- Put <thead>, <tbody>, and <tr>'s here! -->
<tbody>
<tr>
<td style="background-color: #777">50%</td>
<td style="background-color: #aaa">25%</td>
<td style="background-color: #777">25%</td>
</tr>
<tr>
<td>row 1-0</td>
<td>row 1-1</td>
<td>row 1-2</td>
</tr>
<tr>
<td>row 2-0</td>
<td>row 2-1</td>
<td>row 2-2</td>
</tr>
<tr>
<td>row 3-0</td>
<td>row 3-1</td>
<td>row 3-2</td>
</tr>
<tr>
<td>row 4-0</td>
<td>row 4-1</td>
<td>row 4-2</td>
</tr>
<tr>
<td>row 5-0</td>
<td>row 5-1</td>
<td>row 5-2</td>
</tr>
<tr>
<td>row 6-0</td>
<td>row 6-1</td>
<td>row 6-2</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
</tr>
</tbody>
Try this! I hope this help. Is working for me.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div class="table-container">
<table class="fixed_header" style="width: 100%;">
<thead>
<tr>
<th style="width: 50%;">Factor</th>
<th style="width: 25%;">y_i</th>
<th style="width: 25%;">F_i Factor </th>
</tr>
</thead>
<tbody class="style-2">
<tr>
<td>row 1-0</td>
<td>row 1-1</td>
<td>row 1-2</td>
</tr>
<tr>
<td>row 2-0</td>
<td>row 2-1</td>
<td>row 2-2</td>
</tr>
<tr>
<td>row 3-0</td>
<td>row 3-1</td>
<td>row 3-2</td>
</tr>
<tr>
<td>row 4-0</td>
<td>row 4-1</td>
<td>row 4-2</td>
</tr>
<tr>
<td>row 5-0</td>
<td>row 5-1</td>
<td>row 5-2</td>
</tr>
<tr>
<td>row 6-0</td>
<td>row 6-1</td>
<td>row 6-2</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
</tr>
<tr>
<td>row 7-0</td>
<td>row 7-1</td>
<td>row 7-2</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Please add this CSS :
Is working for me
*{ text-align: left; }
table{ width: 100%; max-width: 600px; }
I want to color the rows of my table with differents colors so I'm using this
table#news tr:nth-child(even) {
background-color: red;
}
table#news tr:nth-child(odd) {
background-color: green;
}
It works well with this kind of structure
<table id="news">
<tr><td>Row 1</td></tr>
<tr><td>Row 2</td></tr>
<tr><td>Row 3</td></tr>
</table>
But now I work with this kind of table.
<table id="news">
<tr>
<td>
<tr><td>1</td></tr>
<tr><td>2</td></tr>
</td>
</tr>
</table>
The style is also applied for the rows inside the rows.
How can I proceed to apply the alternate color only for the first level of <tr> ?
Try this:
table#news > tr:nth-child(even) {
background-color: red;
}
table#news > tr:nth-child(odd) {
background-color: green;
}
> Means that it's a #news has to be a direct parent of the tr.
In order for this to work, you also have to add the tbody selector.
table#news tbody > tr:nth-child(even) {
background-color: red;
}
table#news tbody > tr:nth-child(odd) {
background-color: green;
}
tbody element is automatically added by the browser.
Anyway, the above still won't work because you can not nest a tr inside a td. What you have to do is create an entire new table inside the td. See this: Html table tr inside td
Run the code snippet below and look at the source code.
table.news > tbody > tr:nth-child(even) {
background-color: red;
}
table.news > tbody > tr:nth-child(odd) {
background-color: green;
}
<table class="news">
<tr>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
</tr>
<tr>
<td>Row 3</td>
</tr>
</table>
<table class="news">
<tr>
<td>
Test
<tr>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
</tr>
<tr>
<td>Row 3</td>
</tr>
</td>
</tr>
</table>
<h1> Fixed </h1>
<table class="news">
<tr>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
</tr>
<tr>
<td>Row 3</td>
</tr>
</table>
<table class="news">
<tr>
<td>
Test
<table>
<tr>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
</tr>
<tr>
<td>Row 3</td>
</tr>
</table>
</td>
</tr>
</table>