I have a table with different number of <td>, something like this:
<table>
<tbody><tr><td>1</td><td>2</td><td>3</td><td>4</td>
</tr></tbody>
<tbody><tr><td>1</td>
</tr></tbody>
<tbody><tr><td>1</td><td>2</td>
</tr></tbody>
<tbody><tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td>
</tr></tbody>
</table>
I want to draw a line under every row, I tried these styles:
table{border-collapse: collapse;empty-cells: show;}
tbody{border-bottom:1px solid #000;}
tr{border-bottom:1px solid #000;}
td{border-bottom:1px solid #000;}
This is what I get:
Lines are not reaching the end of the table, this is the expected result:
Is this possible using css only?
table{border-collapse: collapse;empty-cells: show;}
tbody{border-bottom:1px solid #000;}
tr{border-bottom:1px solid #000;}
td{border-bottom:1px solid #000;}
<table>
<tbody><tr><td>1</td><td>2</td><td>3</td><td>4</td>
</tr></tbody>
<tbody><tr><td>1</td>
</tr></tbody>
<tbody><tr><td>1</td><td>2</td>
</tr></tbody>
<tbody><tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td>
</tr></tbody>
</table>
Use a pseudo element to create a long line and hide the overflow:
table {
border-collapse: collapse;
empty-cells: show;
overflow: hidden;
}
td {
position: relative;
padding: 5px;
}
td:first-child:before {
content: "";
position: absolute;
bottom: 0;
left: 0;
right: -100vw;
;
height: 1px;
background: #000;
}
<table>
<tbody>
<tr>
<td>111</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>33</td>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</tbody>
</table>
Your new code:
table {
border-collapse: collapse;
empty-cells: show;
}
tr {
border-bottom: 1px solid #000;
display: block;
}
<table>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</tbody>
<tbody>
<tr>
<td>1</td>
</tr>
</tbody>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
</tbody>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</tbody>
</table>
Just changed the display CSS property of the rows to block.
This isn't the perfect solution for dynamic data so I would suggest making a table out of divisions and CSS.
I also suggest that you accept #Temani Afif answer since it is better than mine in the way that you can keep your table structure as well using the pseudo elements.
Related
I've created a simple table using html and a bit of bootstrap, but the last rowspan doesn't work as I thought it will, here's code:
I wanted 4 red-marked cells to be one, so I've replaced first <td>group1</td> with <td rowspan="4">group1</td> and removed remaining 3 <td>group1</td> but it has messed up whole table.
Also it is placed it <div class="col-lg-7 mb-4"> div, but I've also tried without any div - the effect was the same. I'm not sure what is causing that problem, considering that the rest rowspans is working just fine.
/* I don't think CSS is needed, but just in case: */
table.table-bordered {
border: 1px solid #2f8dff!important;
box-shadow: 0px 0px 100px 0px #2f8dff;
margin-top: 20px;
text-transform: uppercase;
font-size: 12px;
color: white;
}
table.table-bordered>thead>tr>th {
border: 1px solid #2f8dff!important;
}
table.table-bordered>tbody>tr>td {
border: 1px solid #2f8dff!important;
}
<table class="table table-bordered">
<thead>
<tr>
<th></th>
<th>mon</th>
<th>tue</th>
<th>wed</th>
<th>thu</th>
<th>fri</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">17:00-18:00</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
<tr>
<td rowspan="2">18:00-19:00</td>
<td>1</td>
<td rowspan="2">group3</td>
<td>3</td>
<td>4</td>
<td rowspan="2">group3</td>
</tr>
<tr>
<td rowspan="2">group2</td>
<td>8</td>
<td rowspan="2">group2</td>
</tr>
<tr>
<td rowspan="2">19:00-20:00</td>
<td rowspan="4">group1</td>
<td rowspan="2">group1</td>
<td>group1</td>
</tr>
<tr>
<td rowspan="3">group1</td>
<td rowspan="3">group1</td>
<td>group1</td>
</tr>
<tr>
<td rowspan="2">20:00-21:00</td>
<td>3</td>
<td>group1</td>
</tr>
<tr>
<td>7</td>
<td>group1</td>
</tr>
</tbody>
</table>
What shall I do?
Thanks in advance!
I don't really know what you mean by "messed up whole table". After adding the row-span="4" and removing the three following td tags, the table looked just fine for me:
The only thing I can see is the changing height of that table cell. This can be prevented by adding this CSS:
tr {
height: 1rem;
}
It makes every row equal height and produces following result:
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I would like to create for example below html tables, It bsically come from square tables.
I tried sometimes, but I couldn't figure out how to change height of each cells.
If someone has opinion, please let me know.
Thanks
table {
border-collapse:collapse;}
td {
padding:5px;
border:solid black 1px;}
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>
Maybe with the :empty CSS pseudo-class?
Removing the padding of empty table cells:
table {
border-collapse:collapse;
}
td {
padding:5px;
border:solid black 1px;
}
td:empty {
padding: 0;
}
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>
Not displaying empty table cells.
table {
border-collapse:collapse;
}
td {
padding:5px;
border:solid black 1px;
}
td:empty {
display: none !important;
}
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>
If I'm reading you right my solution is to add specific classes to the cells that you'd like to be different, that way you can specify the different in padding of the cells with numbers.
table {
border-collapse:collapse;}
td {
padding:25px;
border:solid black 1px;}
.tall {
padding-right:100px;
padding-bottom:100px;
}
<table>
<tr>
<td class="tall">1</td>
<td class="tall">2</td>
<td class="tall">3</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="tall">4</td>
<td class="tall">5</td>
<td class="tall">6</td>
</tr>
</table>
Check out my codepen here:
https://codepen.io/danhebdon/pen/RwPzzvm
Here is a duplicate version of the original table:
table {
width: 100%;
border: 3px solid gray;
border-collapse:collapse;
font-family: 'Arial', sans-serif;
font-size: 36px;
}
td {
border: 3px solid gray;
height: 200px;
text-align: top;
padding: 12px;
}
.smallheight {
height: 50px;
}
<table>
<tr>
<td valign="top">1</td>
<td valign="top">2</td>
<td valign="top">3</td>
</tr>
<tr>
<td class="smallheight"></td>
<td class="smallheight"></td>
<td class="smallheight"></td>
</tr>
<tr>
<td class="smallheight"></td>
<td class="smallheight"></td>
<td class="smallheight"></td>
</tr>
<tr>
<td class="smallheight"></td>
<td class="smallheight"></td>
<td class="smallheight"></td>
</tr>
<tr>
<td valign="top">4</td>
<td valign="top">5</td>
<td valign="top">6</td>
</tr>
</table>
CodePen: https://codepen.io/marchmello/pen/KKpjjvz
I am facing issues with designing this table below as per image:
I tried below table basic html table tr > td structure but nothing is helping me to get desired output.
Use the rowspan attribute like so:
#table1 {
width: 100%;
border-collapse: collapse;
}
#table1 td {
border: 1px solid black;
}
<table id="table1">
<tr>
<td>Estimated Time</td>
<td>1</td>
</tr>
<tr>
<td rowspan="2">Delivery Charge</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
</tr>
<tr>
<td>Payment Method</td>
<td>4</td>
</tr>
</table>
Is the first row of an HTML table with th tags meant to be row 0? Because if I style the table rows with
.t01 tr:nth-child(even) {
color: red;
}
.t01 tr:nth-child(odd) {
color: white;
}
both the first row and the second row have the text color white. But the second row should be red, because 2 is even.
Without your Code it's hard to help, it shoud work as it is.
My guess is, you are using <thead> and <tbody> which results in 2 containers holding <tr> tags. So the nth-child of <thead> is odd and the nth-child of <tbody> is odd too
.container {
display: flex;
flex-direction: row;
justify-content: space-around;
}
.container div {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.container p {
font-size: 8pt;
}
.t01 {
background-color: #3d3d3d;
}
.t01 tr:nth-child(even) {
color: red;
}
.t01 tr:nth-child(odd) {
color: white;
}
.t02 {
background-color: #3d3d3d;
}
.t02 tr:nth-child(even) {
color: red;
}
.t02 tr:nth-child(odd) {
color: white;
}
.t03 {
background-color: #3d3d3d;
}
.t03 thead tr:first-child {
color: red;
}
.t03 tbody tr:nth-child(even) {
color: red;
}
.t03 tbody tr:nth-child(odd) {
color: white;
}
<div class="container">
<div>
<table class="t01">
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
<p>No <thead> & <tbody></p>
</div>
<div>
<table class="t02">
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</tbody>
</table>
<p>With <thead> & <tbody></p>
</div>
<div>
<table class="t03">
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</tbody>
</table>
<p>Fix for <thead> & <tbody></p>
</div>
</div>
You may need to reset the browser data on you browser. Especially if you've been playing around with the CSS, this can cause the page to look off.
Yes, that is true, the first row of the HTML table starts at 0.
For example :
// We have a table containing 3 rows
<table id="myTable">
<tr>
<td>Row1 cell1</td>
</tr>
<tr>
<td>Row2 cell1</td>
</tr>
<tr>
<td>Row3 cell1</td>
</tr>
</table>
We are retrieving 0th row
alert(document.getElementById("myTable").rows[0].innerHTML);
Output of this will be:
Row1 Cell1
I want to hide the border for a specific rows of a table.How to do it?
Any Idea?
Sample code is Highly Appreciated.
Use the CSS property border on the <td>s following the <tr>s you do not want to have the border.
In my example I made a class noBorder that I gave to one <tr>. Then I use a simple selector tr.noBorder td to make the border go away for all the <td>s that are inside of <tr>s with the noBorder class by assigning border: 0.
Note that you do not need to provide the unit (i.e. px) if you set something to 0 as it does not matter anyway. Zero is just zero.
table, tr, td {
border: 3px solid red;
}
tr.noBorder td {
border: 0;
}
<table>
<tr>
<td>A1</td>
<td>B1</td>
<td>C1</td>
</tr>
<tr class="noBorder">
<td>A2</td>
<td>B2</td>
<td>C2</td>
</tr>
<tr>
<td>A3</td>
<td>A3</td>
<td>A3</td>
</tr>
</table>
Here's the output as an image:
I use this with good results:
border-style:hidden;
It also works for:
border-right-style:hidden; /*if you want to hide just a border on a cell*/
Example:
<style type="text/css">
table, th, td {
border: 2px solid green;
}
tr.hide_right > td, td.hide_right{
border-right-style:hidden;
}
tr.hide_all > td, td.hide_all{
border-style:hidden;
}
}
</style>
<table>
<tr>
<td class="hide_right">11</td>
<td>12</td>
<td class="hide_all">13</td>
</tr>
<tr class="hide_right">
<td>21</td>
<td>22</td>
<td>23</td>
</tr>
<tr class="hide_all">
<td>31</td>
<td>32</td>
<td>33</td>
</tr>
</table>
Here is the result:
Add programatically noborder class to specific row to hide it
<style>
.noborder
{
border:none;
}
</style>
<table>
<tr>
<th>heading1</th>
<th>heading2</th>
</tr>
<tr>
<td>content1</td>
<td>content2</td>
</tr>
/*no border for this row */
<tr class="noborder">
<td>content1</td>
<td>content2</td>
</tr>
</table>
You can simply add these lines of codes here to hide a row,
Either you can write border:0 or border-style:hidden; border: none or it will happen the same thing
<style type="text/css">
table, th, td {
border: 1px solid;
}
tr.hide_all > td, td.hide_all{
border: 0;
}
}
</style>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr class= hide_all>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
running these lines of codes can solve the problem easily