How to keep thead and tbody width at 100% using block display - css

So I have an HTML table, with a thead and tbody. I would like to have the body part of the table to scroll, while keeping the header part where it is. I found a solution to that by having the thead and tbody as a block display, however once I do that, the content gets push to the left, instead of stretching the whole table. The scrollbar is where it should be (all the way on the right) but the contents is crushed and then the table data doesn't line up with the headers. If I take away display:block from the thead and tbody, everything lines up perfectly however I can't get a scrollbar to appear. Any suggestions?
This is my full code. If you take out the display:block in both the thead and tbody, that's how it should look (at least the width of it and alignment)
table {
width:800px;
margin-top:30px;
margin-bottom:30px;
border-collapse:collapse;
}
th {
background-color:rgb(216,191,216);
text-align:center;
margin:0px;
}
td {
text-align:center;
border-bottom:1px dashed black;
padding-top:5px;
padding-bottom:5px;
}
.tableHeading {
font-size:25px;
}
.leftBorder {
border-left:1px dashed black;
}
.rightBorder {
border-right:1px dashed black;
}
thead {
display:block;
}
tbody {
display:block;
overflow-y:auto;
max-height:100px;
}
<div class='center'>
<table>
<thead>
<tr><th colspan="5" class='tableHeading'>Receipts</th></tr>
<tr>
<th>Name</th>
<th>Price</th>
<th>Date</th>
<th>Category</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>One</td>
<td>1</td>
<td>Mon</td>
<td>Uncategorized</td>
<td></td>
</tr>
<tr>
<td>Two</td>
<td>2</td>
<td>Mon</td>
<td>Uncategorized</td>
<td></td>
</tr>
<tr>
<td>Three</td>
<td>3</td>
<td>Mon</td>
<td>Uncategorized</td>
<td></td>
</tr>
<tr>
<td>Four</td>
<td>4</td>
<td>Mon</td>
<td>Uncategorized</td>
<td></td>
</tr>
<tr>
<td>Five</td>
<td>5</td>
<td>Mon</td>
<td>Uncategorized</td>
<td></td>
</tr>
</table>
</div>

Related

CSS table - different color for the first td element of a table, striped

I manage to have striped rows, I also manage to color only the first element of a table, but i don't manage to do both at the same time!
Here is my code:
.raceresultscss>tbody>tr>td:first-child:nth-of-type(even){
background-color:black !important;
color:white;
}
.raceresultscss>tbody>tr>td:first-child:nth-of-type(odd){
background-color:#94c946 !important;
color:red;
}
this .raceresultscss>tbody>tr>td:first-child:nth-of-type(even) doesn't exist.. the first child is always odd...
Did you mean like that?
.raceresultscss>tbody>tr:nth-of-type(even)>td:first-child{
background-color:black !important;
color:white;
}
.raceresultscss>tbody>tr:nth-of-type(odd)>td:first-child{
background-color:#94c946 !important;
color:red;
}
<table class="raceresultscss">
<tbody>
<tr>
<td>0</td><td>A</td><td>B</td><td>C</td>
</tr>
<tr>
<td>1</td><td>A1</td><td>B1</td><td>C1</td>
</tr>
<tr>
<td>2</td><td>A2</td><td>B2</td><td>C2</td>
</tr>
<tr>
<td>3</td><td>A3</td><td>B3</td><td>C3</td>
</tr>
</tbody>
</table>
Even rows + first cell highlight.
table {
width: 100%;
border-collapse: collapse;
}
table td {
height: 3rem;
border: yellow 1px dashed;
}
table tr:nth-child(odd) td:first-child {
background-color: green !important;
}
table tr:nth-child(even) td:first-child {
background-color: blue !important;
}
table tr:nth-child(even) {
background-color: lightgray;
}
<table>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>

Firefox table border collapse bleed in

I have a table with a thead and a tbody. I add a border-bottom to the thead and a background-color on the td in the tbody there seems to be some bleed in. This only happends in firefox :
Requirements :
It needs to look exactly the same in chrome firefox
You can't use empty cells
border-collapse cannot be changed
table {
border-collapse:collapse;
font-size:20px;
}
thead {
border-bottom:20px solid green;
}
thead td {
border-right :2px solid blue;
}
tbody td {
background-color:red;
}
<table>
<thead>
<tr>
<td rowspan="2">lorem</td>
<td>lorem</td>
<td>lorem</td>
<td>lorem</td>
</tr>
<tr>
<td>lorem</td>
<td>lorem</td>
<td>lorem</td>
</tr>
</thead>
<tbody>
<tr>
<td colspan="4">lorem</td>
</tr>
</tbody>
</table>
The bug seems to be related by the fact that the td's in the first tr in the tbody, don't share the same borders as the td's in the last tr in the thead. This is happening because of the colspan="4".
A way to circumvent this issue is to put in a tr at the top of the tbody, that no-one can see ( because it has no height ).
table {
border-collapse:collapse;
font-size:20px;
}
thead {
border-bottom:20px solid green;
}
thead td {
border-right :2px solid blue;
}
tbody td {
background-color:red;
}
tbody>tr:first-child{
height: 0px;
}
<table>
<thead>
<tr>
<td rowspan="2">lorem</td>
<td>lorem</td>
<td>lorem</td>
<td>lorem</td>
</tr>
<tr>
<td>lorem</td>
<td>lorem</td>
<td>lorem</td>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="4">lorem</td>
</tr>
</tbody>
</table>
Or maybe.. this alternative where I removed the bottom-border, but replaced it by a green row.
table {
border-collapse:collapse;
font-size:20px;
}
thead td {
border-right :2px solid blue;
}
tbody td {
background-color:red;
}
th:last-child{
height: 20px;
background-color:green;
}
<table>
<thead>
<tr>
<td rowspan="2">lorem</td>
<td>lorem</td>
<td>lorem</td>
<td>lorem</td>
</tr>
<tr>
<td>lorem</td>
<td>lorem</td>
<td>lorem</td>
</tr>
<tr>
<th colspan="4"></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="4">lorem</td>
</tr>
</tbody>
</table>
PS: You should use th instead of td in headers

How does one select elements in a table, but none of the elements in subtables inside of that table? [duplicate]

This question already has answers here:
Why doesn't table > tr > td work when using the child selector?
(2 answers)
Closed 5 years ago.
I have searched and searched, but can't seem to figure out why this is not working.
Goal: set the background color to red for the first td in the first table, while not setting any background colors for the second table.
#table1 > tr > td:nth-child(1)
{
background-color: red;
}
/*Ignore this*/
table td{
padding: 10px;
border: 1px solid black;
border-collapse: collapse;
}
<table id='table1'>
<tr>
<td>1</td>
<td>2
<table>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
</td>
</tr>
</table>
#table1>tbody>tr>td:first-child
{
background-color: red;
}
table td{
padding: 10px;
}
<table id='table1' border=1>
<tbody>
<tr>
<td>1</td>
<td>2
<table border=1>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
Adding <tbody> fixed it (Thanks Mike!)
#table1 > tbody > tr > td:nth-child(1)
{
background-color: red;
}
/*Ignore this*/
table td{
padding: 10px;
border: 1px solid black;
border-collapse: collapse;
}
<table id='table1'>
<tbody>
<tr>
<td>1</td>
<td>2
<table>
<tbody>
<tr>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>

Removing table border which is inside a cell

I have this table inside another table, i want to remove the border from the cells of child table(around A and B).
Below is the source code for this.
<style>
.withBorders tr td{
border: 1px solid black !important ;
}
.withBorders table.withoutBorders tr td {
border:0px
}
</style>
<table class="withBorders">
<tr>
<td>
<table class="withoutBorders">
<tbody>
<tr>
<td>A</td>
<td>B</td>
</tr>
</tbody>
</table>
</td>
<td>C</td>
</tr>
<tr>
<td>D</td>
<td>E</td>
</tr>
</table>
The problem is this isn't working, i have tried many changes with child table's css selector but i am unable to override parent table css property.
Could someone please advice on this?
Note: I can not make any changes to the parent table css selector.
.withBorders tr td{
border: 1px solid black;
}
.withBorders tr td table tr td {
border:none;
}
<table class="withBorders">
<tr>
<td>
<table class="withoutBorders">
<tbody>
<tr>
<td>A</td>
<td>B</td>
</tr>
</tbody>
</table>
</td>
<td>C</td>
</tr>
<tr>
<td>D</td>
<td>E</td>
</tr>
</table>
repeat what you typed, don't change your typing like this
.withBorders table.withoutBorders tr td {
border:0px
}
avaoid !important in your css. it's not good.
You can easily use the code like this:
.withBorders tr td{
border: 1px solid black;
}
.withBorders table.withoutBorders tr td {
border:0px
}
<table class="withBorders">
<tr>
<td>
<table class="withoutBorders">
<tbody>
<tr>
<td>A</td>
<td>B</td>
</tr>
</tbody>
</table>
</td>
<td>C</td>
</tr>
<tr>
<td>D</td>
<td>E</td>
</tr>
</table>
"!important" means that if it is some choice between 2 styles, it will be choosen the style with higher priority. (the style which contains "!important")

Materialize scroll tbody

I am facing problem while setting tbody height width overflow-y: scroll.
I tried this CSS
.table-status-sheet tbody{
min-height: 300px;
overflow-y: auto;
}
This is my table code
<div class="responsive-table table-status-sheet">
<table class="bordered">
<thead>
<tr>
<th class="center">No.</th>
<th class="center">Category</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Category1</td>
</tr>
<tr>
<td>2</td>
<td>Category2</td>
</tr>
<tr>
<td>3</td>
<td>Category3</td>
</tr>
<tr>
<td>4</td>
<td>Category4</td>
</tr>
</tbody>
</table>
</div>
This code is working in bootstrap but not worked in 'Materialized' theme.
Please help me fix this issue.
Here is how you can do it.
JSFiddle DEMO
tbody {
display: block;
height: 150px;
overflow: auto;
}
thead, tbody tr {
display: table;
width: 100%;
table-layout: fixed;
}
thead {
width: calc( 100% - 1em )
}
table {
width: 100%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/css/materialize.min.css" rel="stylesheet"/>
<div class="responsive-table table-status-sheet">
<table class="bordered">
<thead>
<tr>
<th class="center">No.</th>
<th class="center">Category</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Category1</td>
</tr>
<tr>
<td>2</td>
<td>Category2</td>
</tr>
<tr>
<td>3</td>
<td>Category3</td>
</tr>
<tr>
<td>4</td>
<td>Category4</td>
</tr>
</tbody>
</table>
</div>
solution found in another Q: How to set tbody height with overflow scroll
you have to set tbody to -> display:block;
table ,tr td{}
tbody {
display:block;
height:50px;
overflow:auto;
}
thead, tbody tr {
display:table;
width:100%;
table-layout:fixed;
}
thead {
width: calc( 100% - 1em );
}
table {
width:400px;
}

Resources