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

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>

Related

Responsive Table Break

I have a website that is connected to a database. The website has a table that pulls out the values from the database. I want this table to be responsive. I have almost got it to work except one part. Everything is showing and working except for the titles(Band Name, City, Role, Style, SKill). They wont show. I think it has something to do with the last CSS code (content attribute). If someone has experience or can help out, I would highly appreciate it! Take a look at the photo to to see what I mean
screenshot from problem
.table td,
.table th {
padding: 12px 15px;
border: 1px solid #ddd;
text-align: center;
font-size: 16px;
}
.table tbody tr:nth-child(even) {
background-color: #e7e7e7;
}
#media(max-width: 500px) {
.table thead {
display: none;
}
.table,
.table tbody,
.table tr,
.table td {
display: block;
width: 100%;
}
.table tr {
margin-bottom: 15px;
}
.table td {
text-align: right;
padding-left: 50%;
text-align: right;
position: relative;
}
.table th::before {
content: attr(scope);
position: absolute;
left: 0;
width: 50%;
padding-left: 15px;
font-size: 15px;
font-weight: bold;
text-align: left;
}
}
<div class="band-finder-table">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Band Name</th>
<th scope="col">City</th>
<th scope="col">Role</th>
<th scope="col">Style</th>
<th scope="col">Skill</th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr th:each="band : ${bands}">
<td th:text="${band.bandName}">Band Name</td>
<td th:text="${band.city}">City</td>
<td th:text="${band.role.roleName}">Role Name</td>
<td th:text="${band.style.styleName}">style Name</td>
<td th:text="${band.skill.level}">Level</td>
<td><a th:href="#{/band(id=${band.id})}">Edit</a></td>
<td><a th:href="#{/band/delete(id=${band.id})}">Remove</a></td>
</tr>
</tbody>
</table>
I got the help of a friend to answer this question
The code that had to be changed is:
HTML:
<td data-label="Band Name"th:text="${band.bandName}">Band Name</td>
And
CSS:
.table td::before{
content: attr(data-label);

HTML CSS identical tables don't line up

I have two tables that I want to line up, as simple demo I have made the following reproducible code which illustrates my problem
#index_table,
#index_table_header {
text-align: left;
margin: 20px;
margin: 0 auto;
}
#index_table,
#index_table_header {
width: 800px;
}
#index_table_header th {
padding: 10px 8px;
width: 100px;
border: 1px solid black;
}
#index_table td {
padding: 10px 8px;
width: 100px;
border: 1px solid black;
}
#index_table td:nth-child(1),
#index_table_header th:nth-child(1) {
width: 60px;
}
#index_table td:nth-child(3),
#index_table_header th:nth-child(3) {
width: 70px;
}
#index_table td:nth-child(5),
#index_table_header th:nth-child(5) {
width: 200px;
}
#index_table td:nth-child(2),
#index_table_header th:nth-child(2) {
width: 250px;
}
<table id="index_table_header">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Item</th>
<th scope="col">Amount</th>
<th scope="col">Added</th>
<th scope="col">Nutritional Value ID</th>
<th scope="col">Actions</th>
</tr>
</thead>
</table>
<table id="index_table">
<tbody>
<tr>
<td>395</td>
<td>chicken liver</td>
<td>0.37</td>
<td>2019-10-14</td>
<td>67</td>
<td>
Delete
<br>
Update
</td>
</tr>
</tbody>
</table>
The first table has a slightly smaller in width first th than the seconds tables td, then looking along the cells, they are all out of sync.
Add table-layout: fixed; to your table rules. This will force the table to ignore the cell contents and instead wrap text according to the specified widths.
table {
table-layout: fixed;
}
#index_table,
#index_table_header {
text-align: left;
margin: 20px;
margin: 0 auto;
}
#index_table,
#index_table_header {
width: 800px;
}
#index_table_header th {
padding: 10px 8px;
width: 100px;
border: 1px solid black;
}
#index_table td {
padding: 10px 8px;
width: 100px;
border: 1px solid black;
}
#index_table td:nth-child(1),
#index_table_header th:nth-child(1) {
width: 60px;
}
#index_table td:nth-child(3),
#index_table_header th:nth-child(3) {
width: 70px;
}
#index_table td:nth-child(5),
#index_table_header th:nth-child(5) {
width: 200px;
}
#index_table td:nth-child(2),
#index_table_header th:nth-child(2) {
width: 250px;
}
<table id="index_table_header">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Item</th>
<th scope="col">Amount</th>
<th scope="col">Added</th>
<th scope="col">Nutritional Value ID</th>
<th scope="col">Actions</th>
</tr>
</thead>
</table>
<table id="index_table">
<tbody>
<tr>
<td>395</td>
<td>chicken liver</td>
<td>0.37</td>
<td>2019-10-14</td>
<td>67</td>
<td>
Delete
<br>
Update
</td>
</tr>
</tbody>
</table>

Fixed Header & Fixed Column Table

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>

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>