Fixed Header & Fixed Column Table - html

I have a problem with my fixed header and fixed column table. The header is fixed but the column isn't. So how do I fix this problem? I have tried to give the first column position: fixed but it doesn't work right. If possible without Javascript. (I have tried to find solutions from earlier questions of the same topic but none of those helped me). Here is my Codepen.
body {
background-image: url(http://absfreepic.com/absolutely_free_photos/small_photos/green-grass-football-lawn-4020x2261_98957.jpg);
}
h3 {
margin: auto;
text-align: center;
padding: 10px;
color: white;
}
.table {
background-color: white;
margin: auto;
width: 600px;
border-collapse: separate;
display: block;
overflow-x: scroll;
}
thead,
tbody {
display: inline-block;
}
tbody {
overflow-y: scroll;
overflow-x: hidden;
max-height: 70px;
position: relative;
}
th {
background-color: lightgrey;
}
td,
th {
min-width: 100px;
max-width: 100px;
word-wrap: break-word;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<h3>World Cup 2018</h3>
<table class="table table-bordered">
<thead>
<tr>
<th></th>
<th colspan="6">Europe</th>
<th colspan="3">South-America</th>
<th colspan="2">Asia</th>
</tr>
<tr>
<th>Countries</th>
<th>Germany</th>
<th>Spain</th>
<th>Portugal</th>
<th>France</th>
<th>England</th>
<th>Croatia</th>
<th>Argentina</th>
<th>Brazil</th>
<th>Colombia</th>
<th>Japan</th>
<th>Russia</th>
</tr>
</thead>
<tbody>
<tr>
<th>Goalkeeper</th>
<td>Neuer</td>
<td>De Gea</td>
<td>Beto</td>
<td>Lloris</td>
<td>Butland</td>
<td>Subasic</td>
<td>Caballero</td>
<td>Alisson</td>
<td>Ospina</td>
<td>Kawashima</td>
<td>Lunev</td>
</tr>
<tr>
<th>Defender</th>
<td>Boateng</td>
<td>Ramos</td>
<td>Pepe</td>
<td>Varane</td>
<td>Walker</td>
<td>Lovren</td>
<td>Otamendi</td>
<td>Marcelo</td>
<td>Sanchez</td>
<td>Makino</td>
<td>Granat</td>
</tr>
<tr>
<th>Midfielder</th>
<td>Kroos</td>
<td>Iniesta</td>
<td>Ronaldo</td>
<td>Tolisso</td>
<td>Lingard</td>
<td>Modric</td>
<td>Messi</td>
<td>Paulinho</td>
<td>Rodriguez</td>
<td>Honda</td>
<td>Golovin</td>
</tr>
<tr>
<th>Forward</th>
<td>Gomez</td>
<td>Asensio</td>
<td>Quaresma</td>
<td>Griezmann</td>
<td>Welbeck</td>
<td>Perisic</td>
<td>Dybala</td>
<td>Neymar</td>
<td>Bacca</td>
<td>Osako</td>
<td>Smolov</td>
</tr>
</tbody>
</table>

body {
background-image: url(http://absfreepic.com/absolutely_free_photos/small_photos/green-grass-football-lawn-4020x2261_98957.jpg);
}
h3 {
margin: auto;
text-align: center;
padding: 10px;
color: white;
}
.table {
background-color: white;
margin: auto;
width: 600px;
border-collapse: separate;
display: block;
overflow-x: scroll;
}
thead,
tbody {
display: inline-block;
}
thead {
position: sticky;
top: 1px;
z-index: 9999;
}
tbody {
height: 80px;
}
th {
background-color: lightgrey;
}
td,
th {
min-width: 100px;
max-width: 100px;
word-wrap: break-word;
}
.fixed {
position: sticky;
width: 5em;
left: 0;
top: auto;
z-index: 999;
}
td:not(.fixed) {
z-index: 0;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="conatiner">
<table class="table table-bordered">
<thead>
<tr>
<th></th>
<th colspan="6">Europe</th>
<th colspan="3">South-America</th>
<th colspan="2">Asia</th>
</tr>
<tr>
<th class="fixed">Countries</th>
<th>Germany</th>
<th>Spain</th>
<th>Portugal</th>
<th>France</th>
<th>England</th>
<th>Croatia</th>
<th>Argentina</th>
<th>Brazil</th>
<th>Colombia</th>
<th>Japan</th>
<th>Russia</th>
</tr>
</thead>
<tbody>
<tr>
<th class="fixed">Goalkeeper</th>
<td>Neuer</td>
<td>De Gea</td>
<td>Beto</td>
<td>Lloris</td>
<td>Butland</td>
<td>Subasic</td>
<td>Caballero</td>
<td>Alisson</td>
<td>Ospina</td>
<td>Kawashima</td>
<td>Lunev</td>
</tr>
<tr>
<th class="fixed">Defender</th>
<td>Boateng</td>
<td>Ramos</td>
<td>Pepe</td>
<td>Varane</td>
<td>Walker</td>
<td>Lovren</td>
<td>Otamendi</td>
<td>Marcelo</td>
<td>Sanchez</td>
<td>Makino</td>
<td>Granat</td>
</tr>
<tr>
<th class="fixed">Midfielder</th>
<td>Kroos</td>
<td>Iniesta</td>
<td>Ronaldo</td>
<td>Tolisso</td>
<td>Lingard</td>
<td>Modric</td>
<td>Messi</td>
<td>Paulinho</td>
<td>Rodriguez</td>
<td>Honda</td>
<td>Golovin</td>
</tr>
<tr>
<th class="fixed">Forward</th>
<td>Gomez</td>
<td>Asensio</td>
<td>Quaresma</td>
<td>Griezmann</td>
<td>Welbeck</td>
<td>Perisic</td>
<td>Dybala</td>
<td>Neymar</td>
<td>Bacca</td>
<td>Osako</td>
<td>Smolov</td>
</tr>
</tbody>
</table>
</div>

Related

HTML CSS Table border-radius property not applying

I have a table in this format:
.main-table {
margin-top: 1vw;
margin-left: 1vw;
height: 30vw;
overflow-y: auto;
overflow-x: hidden;
}
table {
border-collapse: separate;
border-radius: 3vw;
border-spacing: 0;
margin-top: 1vw;
width: 87vw;
}
td,
th {
border: none;
}
th {
height: 2vw;
background: #deb724;
font-weight: bold;
}
td {
height: 3vw;
background: white;
text-align: center;
}
tr td:hover {
background: #d5d5d5;
}
<div class="main-table">
<table>
<thead>
<tr>
<th> Hello </th>
<th> World </th>
</tr>
</thead>
<tbody>
<tr>
<td> Wow look </td>
<td> What is this </td>
</tr>
</tbody>
</table>
</div>
There is a border-radius property on the table, however it's not applying. I have been looking up and all I can find is set border-collapse as separate, but that didn't do it either. Any help?
Add overflow: hidden to table.
.main-table {
margin-top: 1vw;
margin-left: 1vw;
height: 30vw;
overflow-y: auto;
overflow-x: hidden;
}
table {
border-collapse: separate;
border-radius: 3vw;
border-spacing: 0;
margin-top: 1vw;
width: 87vw;
overflow: hidden;
}
td,
th {
border: none;
}
th {
height: 2vw;
background: #deb724;
font-weight: bold;
}
td {
height: 3vw;
background: white;
text-align: center;
}
tr td:hover {
background: #d5d5d5;
}
<div class="main-table">
<table>
<thead>
<tr>
<th> Hello </th>
<th> World </th>
</tr>
</thead>
<tbody>
<tr>
<td> Wow look </td>
<td> What is this </td>
</tr>
</tbody>
</table>
</div>
Actually it is applied, but overflows it outside. We can see that by adding border to table in the below snippet. That's why we should add overflow: hidden
.main-table {
margin-top: 1vw;
margin-left: 1vw;
height: 30vw;
overflow-y: auto;
overflow-x: hidden;
}
table {
border-collapse: separate;
border-radius: 3vw;
border-spacing: 0;
margin-top: 1vw;
width: 87vw;
border: 2px solid red;
}
td,
th {
border: none;
}
th {
height: 2vw;
background: #deb724;
font-weight: bold;
}
td {
height: 3vw;
background: white;
text-align: center;
}
tr td:hover {
background: #d5d5d5;
}
<div class="main-table">
<table>
<thead>
<tr>
<th> Hello </th>
<th> World </th>
</tr>
</thead>
<tbody>
<tr>
<td> Wow look </td>
<td> What is this </td>
</tr>
</tbody>
</table>
</div>

Responsive table, unable to line up thead and tbody

I have created a table however I cannot get the <thead>s to line up with the <tbody>s.
The table is responsive and works as I want it to when resized, however it's the initial screen size that is the issue.
I have added float:left to the <thead>s so they align properly instead of overlapping each other and would like the table data to a-line in the same way.
table {
border: 1px solid #ccc;
border-collapse: collapse;
margin: 0;
padding: 0;
width: 100%;
table-layout: fixed;
}
table caption {
font-size: 1.5em;
margin: .5em 0 .75em;
}
table tr {
background: #f8f8f8;
border: 1px solid #ddd;
padding: .35em;
}
table th,
table td {
padding: .625em;
text-align: center;
}
table td {
font-size: 0.60em;
}
table th {
font-size: .60em;
letter-spacing: .1em;
text-transform: uppercase;
}
#media screen and (max-width: 600px) {
table {
border: 0;
}
table caption {
font-size: 1.3em;
}
table thead {
border: none;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
table tr {
border-bottom: 3px solid #ddd;
display: block;
margin-bottom: .625em;
}
table td {
border-bottom: 1px solid #ddd;
display: block;
font-size: .8em;
text-align: right;
}
table td:before {
/*
* aria-label has no advantage, it won't be read inside a table
content: attr(aria-label);
*/
content: attr(data-label);
float: left;
font-weight: bold;
text-transform: uppercase;
}
table td:last-child {
border-bottom: 0;
}
#table-wrapper {
position: relative;
}
#table-scroll {
overflow: auto;
}
#table-wrapper table {
width: 100%;
}
#table-wrapper table * {
color: black;
font-size: 12px;
}
#table-wrapper table thead th .text {
text-align: center;
}
}
<div class="col-12 offset-0">
<div style="overflow-x:auto">
<div id="table-wrapper">
<div id="table-scroll">
<table class="table table-striped" border="0" cellspacing="0" cellpadding="0" align="center">
<h4>Success Stream</h4>
<thead style="float:left">
<tr>
<th scope="col">Year</th>
<th scope="col">{{n1}}'s age</th>
<th scope="col">{{n2}}'s age</th>
<th scope="col">Income</th>
<th scope="col">Personal Contribution to Pension</th>
<th scope="col">Company Contribution to Pension</th>
<th scope="col">Personal Contribution to ISA</th>
<th scope="col">Expenses</th>
<th scope="col">Cash Flow</th>
<th scope="col">Growth of Pension</th>
<th scope="col">Growth of ISA</th>
<th scope="col">Growth of Other</th>
<th scope="col">Withdrawals from Pension</th>
<th scope="col">Withdrawals from ISA</th>
<th scope="col">Withdrawals from Other</th>
<th scope="col">Pension Total</th>
<th scope="col">ISA Total</th>
<th scope="col">Other Total</th>
<th scope="col">Full Total</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Year"> 2018</td>
<td data-label="{{n1}}'s age">55</td>
<td data-label="{{n2}}'s age">55</td>
<td data-label="Income">57,000</td>
<td data-label="Personal Contribution to Pension">5,000</td>
<td data-label="Company Contribution to Pension">0</td>
<td data-label="Personal Contribution to ISA">0</td>
<td data-label="Expenses">50,500</td>
<td data-label="Cash Flow">1,500</td>
<td data-label="Growth of Pension">57,737</td>
<td data-label="Growth of ISA">0</td>
<td data-label="Growth of Other">0</td>
<td data-label="Withdrawals from Pension">0</td>
<td data-label="Withdrawals from ISA">0</td>
<td data-label="Withdrawals from Other">0</td>
<td data-label="Pension Total">862,737</td>
<td data-label="ISA Total">1</td>
<td data-label="Other Total">1,501</td>
<td data-label="Full Total">864,239</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
Instead of using float:left for thead just change
table {
table-layout: fixed;
}
to
table {
table-layout: auto;
}

Frozen columns not aligned with scrollable columns (HTML+CSS)

I intend to do this:
Left Side Column ---> Frozen
Right Side Column ---> Scrollable
The problem that I'm facing currently is that only <td> part is getting frozen. and the <th> has no effect of the code. I couldn't figure out how to get this fixed? Any help would be much appreciated.
Thanks in advance.
HTML code:
<div class="padding-right-5 max_width_300 margin_bottom_30">
<div class="innerDiv">
<table id="fix_table" >
<tr>
<th class="height_26"></th>
</tr>
<tr>
<th class="header long"><h3>Sheet</h3></th>
<th class="header"><h3>Part Type</h3></th>
<th class="header"><h3>Options</h3></th>
<th class="header"><h3>Customer</h3></th>
<th class="header"><h3>Code</h3></th>
<th class="header"><h3>Address</h3></th>
</tr>
<tr>
<td class="row-content headcol"><h3>ABC01</h3></td>
<td class="row-content"><h3>QQQ</h3></td>
<td class="row-content" style="min-width:150px;">
<h3>POP with camera
With primer - cam
Alt of 'WS with camera'
Mobileye
Alt of 'QPS with camera'</h3>
</td>
<td class="row-content">
<h3>Renault</h3>
</td>
<td class="row-content">
<h3>Xcent</h3>
</td>
<td class="row-content">
<h3>Canada</h3>
</td>
<tr>
</table>
</div>
</div>
CSS code:
.padding-right-5 {
padding-right: px;
}
.max_width_300 {
max_width : 300px;
}
.margin_bottom_30 {
margin-bottom:30px;
}
.innerDiv {
overflow-x : scroll;
height: 300px;
overflow-y: visible;
padding: 0;
}
.header {
border :1px solid;
min-width: 100px;
height: 75px;
background-color: #C4C4C4;
}
.row-content {
min-width: 100px;
min-height: 100px;
vertical-align: middle;
height: 50px;
border: 1px solid;
height: 57px;
}
.headcol {
position: absolute;
}
#fix_table{
border-collapse: separate;
border-spacing: 0;
}
td, th {
margin: 0;
white-space: nowrap;
border-top-width: 0px;
}
Added few fixes. Hope this will get you started.
.padding-right-5 {
padding-right: 5px;
}
.max_width_300 {
max-width : 300px;
}
.long{
position: absolute; /* added this */
}
.margin_bottom_30 {
margin-bottom:30px;
}
.innerDiv {
overflow-x : scroll;
height: 300px;
overflow-y: visible;
padding: 0;
}
.header {
border :1px solid;
min-width: 100px;
height: 75px;
background-color: #C4C4C4;
}
.row-content {
min-width: 100px;
min-height: 100px;
vertical-align: middle;
height: 50px;
border: 1px solid;
height: 57px;
}
.headcol {
position: absolute;
background: #fff; /*added this */
}
#fix_table{
border-collapse: separate;
border-spacing: 0;
}
td, th {
margin: 0;
white-space: nowrap;
border-top-width: 0px;
}
<div class="padding-right-5 max_width_300 margin_bottom_30">
<div class="innerDiv">
<table id="fix_table">
<tr>
<th class="height_26"></th>
</tr>
<tr>
<th class="header long">
<h3>Sheet</h3></th>
<th class="header">
<h3>Part Type</h3></th>
<th class="header">
<h3>Options</h3></th>
<th class="header">
<h3>Customer</h3></th>
<th class="header">
<h3>Code</h3></th>
<th class="header">
<h3>Address</h3></th>
</tr>
<tr>
<td class="row-content headcol">
<h3>ABC01</h3></td>
<td class="row-content">
<h3>QQQ</h3></td>
<td class="row-content" style="min-width:150px;">
<h3>POP with camera
With primer - cam
Alt of 'WS with camera'
Mobileye
Alt of 'QPS with camera'</h3>
</td>
<td class="row-content">
<h3>Renault</h3>
</td>
<td class="row-content">
<h3>Xcent</h3>
</td>
<td class="row-content">
<h3>Canada</h3>
</td>
<tr>
</table>
</div>
</div>

How to put a scrollbar in a tbody without breaking columns alignment

I am working on on a table right now and, I need to include a scrollbar on the y axis. My problem is that the scrollbar is breaking the alignment of my <tbody> and <thead> columns.
I have tried some stuff with calc() to correct the width of the <td> but, it wasn't a very successful attempt. I also tried making the <th> in thead larger than 100% in width when you add them so that it overflows over the scrollbar but, it did not work.
I don't really know what to do, if any of you guys see what could solve the problem, please tell me.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
html, body, .main-container {
background-color: #fbe1c8;
}
/**** Table design ****/
table.tableSection thead {
float: left;
width: 100%
}
table.tableSection tbody {
float: left;
width: 100%
}
table.tableSection tbody {
overflow-y: scroll;
height: 150px
}
.lbl {
padding-left: 5px;
}
.libelleTableauTop {
color: black;
font-size: 8pt;
font-family: Arial;
border-color: #fbe1c8;
margin-top: 0em;
margin-bottom: 0em;
background-color: #ffa863;
}
.libelleTableauBot th {
border-color: #fbe1c8;
margin-top: 0em;
margin-bottom: 0em;
background-color: #ffa863;
}
/*** Parent row ***/
.parent {
height: 5px;
background-color: #fbe1c8;
font-weight:bold;
}
.parent:hover {
background-color:#4c94e8
}
.myCheckbox {
width: 1%
}
.garantie {
width: 29%
}
.valPrec {
width: 14%
}
.cotisPrec {
width: 14%
}
.indiceComp {
width: 7%
}
.valTerme {
width: 14%
}
.cotisTerme {
width: 14%
}
.varIndice {
width: 7%
}
th:hover {
background-color:#4c94e8
}
table, th, td {
border: 1px solid #fbe1c8;
}
<table class="tableSection">
<thead class="libelleTableauTop">
<tr>
<th colSpan="2">Garanties</th>
<th colSpan="2">SITUATION PRECEDENTE</th>
<th rowSpan="2">Mesure Terme<br><p>Indice compris</p></th>
<th colSpan="2">SITUATION TERME</th>
<th rowSpan="2">Variation Indice</th>
</tr>
<tr class="libelleTableauBot">
<th colSpan="2">Libellé Assiette</th>
<th>Valeur Assiette</th>
<th>Cotis. TTC Taux</th>
<th>Valeur Assiette</th>
<th>Cotis. TTC Taux</th>
</tr>
<tr>
<th width="1%"></th>
<th width="29%"></th>
<th width="14%"></th>
<th width="14%"></th>
<th width="7%"></th>
<th width="14%"></th>
<th width="14%"></th>
<th width="7%"></th>
</tr>
</thead>
<tbody>
<tr class="parent">
<td class="myCheckbox"></td>
<td class="lbl garantie">Responsabilité civile ava</td>
<td class="valPrec"></td>
<td class="garNbr cotisPrec">150,00</td>
<td class="garNbr indiceComp">2,00 %</td>
<td class="valTerme"></td>
<td class="garNbr cotisTerme">150,00</td>
<td class="varIndice"></td>
</tr>
</tbody>
</table>

Table with fixed headers with CSS

I am trying to implement a table with fixed headers using the concepts from this page:
http://www.imaputz.com/cssStuff/bigFourVersion.html#
When I implemented the CSS from that page, my table columns are not aligned with the table headers.
HTML:
<div id="tableContainer" class="tableContainer">
<table class="sortable" border=1 frame=void cellspacing=0 cellpadding=4>
<thead class="fixedHeader">
<tr>
<th id="Column1" align=center valign=bottom>Column 1</th>
<th id="Column2" align=center valign=bottom>Column 2</th>
<th id="Column " align=center valign=bottom>Column 3</th>
</tr>
</thead>
<tbody class="scrollContent">
<tr>
<td align=right>ABCD</td>
<td aligh=right>XY</td>
<td aligh=right>12345678</td>
</tr>
<tr>
<td align=right>BAR</td>
<td aligh=right>BAZ</td>
<td aligh=right>12345678</td>
</tr>
<tr>
<td align=right>BAR</td>
<td aligh=right>BAZ</td>
<td aligh=right>12345678</td>
</tr>
<tr>
<td align=right>CD</td>
<td aligh=right>FOO</td>
<td aligh=right>12345678</td>
</tr>
<tr>
<td align=right>BAR</td>
<td aligh=right>BAZ</td>
<td aligh=right>12345678</td>
</tr>
</tbody>
</table>
CSS:
html>body tbody.scrollContent {
display: block;
height: 100px;
overflow: auto;
}
thead.fixedHeader tr {
position: relative
}
html>body thead.fixedHeader tr {
display: block
}
div.tableContainer {
clear: both;
border: 1px solid #963;
height: 150px;
overflow: auto;
width: 300px
}
html>body div.tableContainer {
overflow: hidden;
}
div.tableContainer table {
float: left;
}
Here is that on jsfiddle:
http://jsfiddle.net/softvar/yL84C/
I was able to 'fix' this by explicitly setting the cell size:
CSS:
* {
box-sizing: border-box;
}
th, td {
width: 100px;
}
html>body tbody.scrollContent {
display: block;
height: 100px;
overflow: auto;
}
thead.fixedHeader tr {
position: relative
}
html>body thead.fixedHeader tr {
display: block
}
div.tableContainer {
clear: both;
border: 1px solid #963;
height: 150px;
overflow: auto;
width: 300px
}
html>body div.tableContainer {
overflow: hidden;
}
div.tableContainer table {
}
Here is that one jsfiddle:
http://jsfiddle.net/cFr38/
But what I really want is the cells to be automagically sized based on the data (which is what my table does now without the new CSS)? How can I do that?