Table cells are not the same height within row - html

I am creating a table in html. Every table cell needs to be exactly the same height. When I put a div in the table cell the height is not the same between table cells in the row. The table cells that contain text are 61.6px tall while the table cells that contain divs are 61.2px tall. I am using Google Chrome and that is the only browser I need the table cells to be an even height for. How do I ensure all the table cells are the same height?
A pure CSS solution is preferred but a javascript solution is acceptable.
table {
border-collapse: collapse;
width: 600px;
}
tbody {
border: 1px solid black;
}
th {
padding: 0 6px;
text-align: center;
background: black;
color: white;
}
tr td {
background: darkgray;
}
tr:nth-child(even) td {
background: gray;
}
td.small {
font-size: 9px;
}
td.large {
font-size: 18px;
}
td {
padding: 6px;
border-color: black;
border-width: 0 1px;
border-style: solid;
height: 18px;
}
.evilobject {
height: 50px;
background: red;
}
<table>
<tbody>
<tr>
<th>Large Font</th>
<th>Small Font</th>
<th>Contains Div</th>
</tr>
<tr>
<td class="large">What the font!?</td>
<td class="small">What the font!?</td>
<td class="small"><div class="evilobject">Evil div object</div></td>
</tr>
<tr>
<td class="large">What the font!?</td>
<td class="small">What the font!?</td>
<td class="small"><div class="evilobject">Evil div object</div></td>
</tr>
<tr>
<td class="large">What the font!?</td>
<td class="small">What the font!?</td>
<td class="small"><div class="evilobject">Evil div object</div></td>
</tr>
</tbody>
</table>

Use tr instead of td and use universal selector`
*{
box-sizing: border-box;
padding: 0;
margin: 0;
}
table {
border-collapse: collapse;
width: 600px;
}
tbody {
border: 1px solid black;
}
th {
padding: 0 6px;
text-align: center;
background: black;
color: white;
}
tr td {
background: darkgray;
}
tr:nth-child(even) td {
background: gray;
}
td.small {
font-size: 9px;
}
td.large {
font-size: 18px;
}
tr{
padding: 6px;
border-color: black;
border-width: 0 1px;
border-style: solid;
height: 18px;
}
.evilobject {
height: 50px;
background: red;
}
<table >
<tbody>
<tr>
<th>Large Font</th>
<th>Small Font</th>
<th>Contains Div</th>
</tr>
<tr>
<td class="large">What the font!?</td>
<td class="small">What the font!?</td>
<td class="small"><div class="evilobject">Evil div object</div></td>
</tr>
<tr>
<td class="large">What the font!?</td>
<td class="small">What the font!?</td>
<td class="small"><div class="evilobject">Evil div object</div></td>
</tr>
<tr>
<td class="large">What the font!?</td>
<td class="small">What the font!?</td>
<td class="small"><div class="evilobject">Evil div object</div></td>
</tr>
</tbody>
</table>
`

Related

CSS Table with custom borders

I'm stuck with a simple table design as i'm from backend I need a table like this
What I have right now
table {
border-collapse: collapse;
width: 100%;
}
tr {
border: none;
}
.first{
width: 40%;
font-family: Quasimoda;
font-size: 0.75em;
color: #1D1D1E;
}
.middle{
width: 60%;
font-family: Quasimoda;
font-size: 0.75em;
color: #1D1D1E;
}
.last{
width: 40%;
font-family: Quasimoda;
font-size: 0.75em;
color: #1D1D1E;
}
td {
border-bottom: 1px solid #ddd;
border-top: 1px solid #ddd;
padding: 10px;
}
<table>
<tr>
<td class="University">January</td>
<td class="middle ">the progress bar will be placed here </td>
<td class="last">870</td>
</tr>
<tr>
<td class="first">January</td>
<td class="middle ">the progress bar will be placed here </td>
<td class="last">560</td>
</tr>
</table>
Please help. I have tried with multiple html table attributes all gone in vain.
Set the thead tr element bottom border, and the right border of the .first element:
table {
border-collapse: collapse;
width: 100%;
}
th {
text-align: left;
border-bottom: 1px solid #ddd;
}
td {
padding: 10px;
font-family: Quasimoda;
font-size: 0.75em;
color: #1D1D1E;
}
.first {
border-right: 1px solid #ddd;
}
.middle {
width: 100%;
}
<table>
<thead>
<tr>
<th colspan="3">Unique values:</th>
</tr>
</thead>
<tbody>
<tr>
<td class="first">January</td>
<td class="middle ">the progress bar will be placed here </td>
<td class="last">870</td>
</tr>
<tr>
<td class="first">January</td>
<td class="middle ">the progress bar will be placed here </td>
<td class="last">560</td>
</tr>
</tbody>
</table>
Actually, your picture is not that clear.
I was not clear where you want border actually.
But I try to do this, Run the code snippet and have a look, and let me know if this works for you or not.
.colspan2border
{
border-collapse: collapse;
border-bottom: 1px solid red;
}
.rightBorder
{
border-collapse: collapse;
border-right: 1px solid red;
}
.pr-left1
{
padding-left: 1rem;
}
table
{
border-collapse: collapse;
}
tr
{
border: none;
}
<table>
<tr colspan="2" class='colspan2border'><td>Unique values:</td></tr>
<tr>
<td class="rightBorder pr-left1">University</td>
<td>870</td>
</tr>
<tr>
<td class="rightBorder pr-left1">Custom</td>
<td>560</td>
</tr>
</table>

How to place image over this table and make the position responsive

I have placed the image but it breaks when we resize the window.
I want it in the center of the first column which is description below the email service.
I want it to remain in the center all the time.
I have given the td position relative and made the image absolute position. Its relative to the td................................
* {
font-family: sans-serif;
}
table{
width: 100%;
border: 1px solid #edf2fc;
border-collapse: collapse;
margin-top: 30px;
}
table th {
padding: 11px 6px 11px 6px;
word-break: break-all;
background: #413fa4;
color: #fff;
text-align: center;
}
tr td {
border: 1px solid #cbdaf1;
}
table tbody td {
text-align: center;
padding: 33px 0 33px 0;
word-break: break-all;
font-size: 18px;
}
tfoot tr td {
padding-top: 4px;
padding-bottom: 4px;
}
tfoot tr td:first-child{
padding-right: 22px;
}
.gray {
background-color: lightgray
}
<table width="100%">
<thead style="background-color: lightgray;">
<tr>
<th>Description</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
<tr>
<td style="position: relative">Email Service
<img style="position: absolute; left: 300px; top: 70px;" src="https://image.shutterstock.com/image-vector/paid-thank-you-grunge-rubber-260nw-564254104.jpg"width=130px;>
</td>
<td align="center">1400.00</td>
</tr>
</tbody>
<tfoot>
<tr>
<td align="right">Subtotal</td>
<td align="center">1635.00</td>
</tr>
<tr>
<td align="right">Tax</td>
<td align="center">294.3</td>
</tr>
<tr>
<td align="right">Discount</td>
<td align="center">294.3</td>
</tr>
<tr>
<td align="right">Total</td>
<td align="center" class="gray">$ 1929.3</td>
</tr>
</tfoot>
</table>
Wrap the text and the image inside a div with a flex box property, and then you can center it as you like
* {
font-family: sans-serif;
}
table{
width: 100%;
border: 1px solid #edf2fc;
border-collapse: collapse;
margin-top: 30px;
}
table th {
padding: 11px 6px 11px 6px;
word-break: break-all;
background: #413fa4;
color: #fff;
text-align: center;
}
tr td {
border: 1px solid #cbdaf1;
}
table tbody td {
text-align: center;
padding: 33px 0 33px 0;
word-break: break-all;
font-size: 18px;
}
tfoot tr td {
padding-top: 4px;
padding-bottom: 4px;
}
tfoot tr td:first-child{
padding-right: 22px;
}
.gray {
background-color: lightgray
}
.flex-b {
display:flex;
align-items:center;
justify-content:center;
}
<table width="100%">
<thead style="background-color: lightgray;">
<tr>
<th>Description</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="flex-b">
<span>Email Service</span>
<img src="https://image.shutterstock.com/image-vector/paid-thank-you-grunge-rubber-260nw-564254104.jpg"width=130px;>
</div>
</td>
<td align="center">1400.00</td>
</tr>
</tbody>
<tfoot>
<tr>
<td align="right">Subtotal</td>
<td align="center">1635.00</td>
</tr>
<tr>
<td align="right">Tax</td>
<td align="center">294.3</td>
</tr>
<tr>
<td align="right">Discount</td>
<td align="center">294.3</td>
</tr>
<tr>
<td align="right">Total</td>
<td align="center" class="gray">$ 1929.3</td>
</tr>
</tfoot>
</table>

How to put <td> color vertically? [duplicate]

This question already has answers here:
Can I color table columns using CSS without coloring individual cells?
(8 answers)
Closed 4 years ago.
I'm creating a table using html and add some designs using css
I already created a table and put background color horizontally/ by row like this but i want to make it vertical
and here's my html code
table {
width: 100%;
border-collapse: collapse;
border-spacing: 0;
margin-bottom: 20px;
}
table tr:nth-child(2n-1) td {
background: #F5F5F5;
}
table th,
table td {
text-align: center;
}
table th {
padding: 5px 20px;
color: #5D6975;
border-bottom: 1px solid #C1CED9;
white-space: nowrap;
font-weight: normal;
}
table .service,
table .desc {
text-align: left;
}
table td {
padding: 20px;
text-align: right;
}
table td.service,
table td.desc {
vertical-align: top;
}
table td.unit,
table td.qty,
table td.total {
font-size: 1.2em;
}
table td.grand {
border-top: 1px solid #5D6975;;
}
<main>
<table border="1">
<thead>
<tr>
</tr>
</thead>
<tbody>
<tr>
<td class="service"><span style="color:#ac0043">房间号 ( UNIT NO. )</span></td>
<td class="service"><span style="color:#ac0043">费用 ( RATE )</span></td>
</tr>
<tr>
<td class="service"></td>
<td class="service">AED: /-</td>
</tr>
<tr>
<td class="service"></td>
<td class="service"></td>
</tr>
<tr>
<td class="service">5% VAT(增值税)</td>
<td class="service">AED: /-</td>
</tr>
<tr>
<td class="service"></td>
<td class="service"></td>
</tr>
<tr>
<td class="service"><span style="color:#ac0043">TOTAL (共计):</span></td>
<td class="service"><span style="color:#ac0043">AED:</span> /-</td>
</tr>
</tbody>
</table>
</main>
I use this code to make background horizontal/by row
table tr:nth-child(2n-1) td {
background: #F5F5F5;
}
The question is how can i put the background vertically or by columns?
Thanks!
Just change the css target, like this:
put :nth-child(2n-1) on td, instead of tr
table tr td:nth-child(2n-1) {
background: #F5F5F5;
}
table {
width: 100%;
border-collapse: collapse;
border-spacing: 0;
margin-bottom: 20px;
}
table tr td:nth-child(2n-1) {
background: #F5F5F5;
}
table th,
table td {
text-align: center;
}
table th {
padding: 5px 20px;
color: #5D6975;
border-bottom: 1px solid #C1CED9;
white-space: nowrap;
font-weight: normal;
}
table .service,
table .desc {
text-align: left;
}
table td {
padding: 20px;
text-align: right;
}
table td.service,
table td.desc {
vertical-align: top;
}
table td.unit,
table td.qty,
table td.total {
font-size: 1.2em;
}
table td.grand {
border-top: 1px solid #5D6975;;
}
<main>
<table border="1">
<thead>
<tr>
</tr>
</thead>
<tbody>
<tr>
<td class="service"><span style="color:#ac0043">房间号 ( UNIT NO. )</span></td>
<td class="service"><span style="color:#ac0043">费用 ( RATE )</span></td>
</tr>
<tr>
<td class="service"></td>
<td class="service">AED: /-</td>
</tr>
<tr>
<td class="service"></td>
<td class="service"></td>
</tr>
<tr>
<td class="service">5% VAT(增值税)</td>
<td class="service">AED: /-</td>
</tr>
<tr>
<td class="service"></td>
<td class="service"></td>
</tr>
<tr>
<td class="service"><span style="color:#ac0043">TOTAL (共计):</span></td>
<td class="service"><span style="color:#ac0043">AED:</span> /-</td>
</tr>
</tbody>
</table>
</main>
table {
width: 100%;
border-collapse: collapse;
border-spacing: 0;
margin-bottom: 20px;
}
table tr td:nth-child(2n-1) {
background: #F5F5F5;
}
table th,
table td {
text-align: center;
}
table th {
padding: 5px 20px;
color: #5D6975;
border-bottom: 1px solid #C1CED9;
white-space: nowrap;
font-weight: normal;
}
table .service,
table .desc {
text-align: left;
}
table td {
padding: 20px;
text-align: right;
}
table td.service,
table td.desc {
vertical-align: top;
}
table td.unit,
table td.qty,
table td.total {
font-size: 1.2em;
}
table td.grand {
border-top: 1px solid #5D6975;;
}
<main>
<table border="1">
<thead>
<tr>
</tr>
</thead>
<tbody>
<tr>
<td class="service"><span style="color:#ac0043">房间号 ( UNIT NO. )</span></td>
<td class="service"><span style="color:#ac0043">费用 ( RATE )</span></td>
</tr>
<tr>
<td class="service"></td>
<td class="service">AED: /-</td>
</tr>
<tr>
<td class="service"></td>
<td class="service"></td>
</tr>
<tr>
<td class="service">5% VAT(增值税)</td>
<td class="service">AED: /-</td>
</tr>
<tr>
<td class="service"></td>
<td class="service"></td>
</tr>
<tr>
<td class="service"><span style="color:#ac0043">TOTAL (共计):</span></td>
<td class="service"><span style="color:#ac0043">AED:</span> /-</td>
</tr>
</tbody>
</table>
</main>

Unwanted table cell border [duplicate]

This question already has answers here:
Removing unwanted table cell borders with CSS
(8 answers)
Closed 6 years ago.
table {
border: solid 1px #eee;
text-align: right;
}
tr,
td,
th {
padding: 0;
margin: 0;
border: none;
}
th,
td {
width: 90px;
height: 24px;
line-height: 24px;
padding: 0;
}
th {
background-color: #f4f4f4;
}
.first-col {
text-align: left;
}
<table class="satisfaction">
<tr>
<th class="first-col">Kepuasan</th>
<th>1 bulan</th>
<th>6 bulan</th>
<th>12 bulan</th>
</tr>
<tr>
<td class="first-col"><span><img class="svg emo emo-smile" src="img/icon/emoticon-happy.svg">Positif</span>
</td>
<td>9393</td>
<td>9393</td>
<td>9393</td>
</tr>
<tr>
<td class="first-col"><span><img class="svg emo emo-neutral" src="img/icon/emoticon-neutral.svg">Netral</span>
</td>
<td>93</td>
<td>93</td>
<td>93</td>
</tr>
<tr class="last-row">
<td class="first-col"><span><img class="svg emo emo-frown" src="img/icon/emoticon-sad.svg">Negatif</span>
</td>
<td>39</td>
<td>39</td>
<td>39</td>
</tr>
</table>
I made a table and I keep seeing this white separator on the first row.
I already done make all margin and padding to zero but it doesn't work. So far this is my styling
table {
border: solid 1px #eee;
text-align: right;
}
tr, td, th {
padding: 0;
margin: 0;
border: none;
}
th, td {
width: 90px;
height: 24px;
line-height: 24px;
padding: 0;
}
th {
background-color: #f4f4f4;
}
.first-col {
text-align: left;
}
Is there anything I'm missing out? Any help appreciated!
Looking at your posted CSS it seems likely that the 'border' is appearing because you haven't collapsed the cell-borders; so with that in mind I'd suggest you add the rule:
border-collapse: collapse;
to the CSS rules for the table element:
table {
border-collapse: collapse;
border: solid 1px #eee;
text-align: right;
}
table {
border-collapse: collapse;
border: solid 1px #eee;
text-align: right;
}
tr,
td,
th {
padding: 0;
margin: 0;
border: none;
}
th,
td {
width: 90px;
height: 24px;
line-height: 24px;
padding: 0;
}
th {
background-color: #f4f4f4;
}
.first-col {
text-align: left;
}
<table class="satisfaction">
<tr>
<th class="first-col">Kepuasan</th>
<th>1 bulan</th>
<th>6 bulan</th>
<th>12 bulan</th>
</tr>
<tr>
<td class="first-col"><span><img class="svg emo emo-smile" src="img/icon/emoticon-happy.svg">Positif</span>
</td>
<td>9393</td>
<td>9393</td>
<td>9393</td>
</tr>
<tr>
<td class="first-col"><span><img class="svg emo emo-neutral" src="img/icon/emoticon-neutral.svg">Netral</span>
</td>
<td>93</td>
<td>93</td>
<td>93</td>
</tr>
<tr class="last-row">
<td class="first-col"><span><img class="svg emo emo-frown" src="img/icon/emoticon-sad.svg">Negatif</span>
</td>
<td>39</td>
<td>39</td>
<td>39</td>
</tr>
</table>
References:
border-collapse.

Prevent <td> from expanding when inserting content

I have a table of fixed width and height containing three rows and columns. Rows and columns are automatically equal size, but when I put innerHTML (or image) into table cell, cell with text inside expands at the cost of other columns. How to prevent table cells from expanding after inserting content inside? I've tried solutions from similar stack overflow questions, but nothing worked.
JS fiddle: https://jsfiddle.net/m20akmdx/14/
document.getElementById('8').innerHTML = 'R';
table {
width: 360px;
height: 360px;
border: 1px solid black;
border-collapse: collapse;
}
td {
border: 1px solid black;
text-align: center;
}
<table id="table">
<tr>
<td id="1"></td>
<td id="2"></td>
<td id="3"></td>
</tr>
<tr>
<td id="4"></td>
<td id="5"></td>
<td id="6"></td>
</tr>
<tr>
<td id="7"></td>
<td id="8"></td>
<td id="9"></td>
</tr>
</table>
Try using fixed table layout.
table {
table-layout: fixed;
...
}
And adding a no-break space into each cell.
td:after {
content: "\00A0";
}
document.getElementById('8').innerHTML = 'R';
table {
table-layout: fixed;
width: 360px;
height: 360px;
border: 1px solid black;
border-collapse: collapse;
}
td {
border: 1px solid black;
text-align: center;
}
td:after {
content: "\00A0";
}
<table id="table">
<tr>
<td id="1"></td>
<td id="2"></td>
<td id="3"></td>
</tr>
<tr>
<td id="4"></td>
<td id="5"></td>
<td id="6"></td>
</tr>
<tr>
<td id="7"></td>
<td id="8"></td>
<td id="9"></td>
</tr>
</table>
Set the width and height of the td elements rather than a width and height for the table and you will get the desired behaviour.
document.getElementById('8').innerHTML = 'Raaaa';
table{
border: 1px solid black;
border-collapse: collapse;
}
td{
width:120px;
height:120px;
border: 1px solid black;
text-align: center;
}
<table id="table">
<tr>
<td id="1"></td>
<td id="2"></td>
<td id="3"></td>
</tr>
<tr>
<td id="4"></td>
<td id="5"></td>
<td id="6"></td>
</tr>
<tr>
<td id="7"></td>
<td id="8"></td>
<td id="9"></td>
</tr>
</table>
set a max-width
td{
border: 1px solid black;
text-align: center;
max-width: 200px;
}
or
td{
border: 1px solid black;
text-align: center;
max-width: 30%;
}
You will need to specify the td width, and maybe the height as well:
https://jsfiddle.net/m20akmdx/23/
document.getElementById('8').innerHTML = 'my long text gets wrapped';
table {
width: 360px;
height: 360px;
border: 1px solid black;
border-collapse: collapse;
}
td {
width: calc(100% / 3);
height: calc(100% / 3);
border: 1px solid black;
text-align: center;
}
<table id="table">
<tr>
<td id="1"></td>
<td id="2"></td>
<td id="3"></td>
</tr>
<tr>
<td id="4"></td>
<td id="5"></td>
<td id="6"></td>
</tr>
<tr>
<td id="7"></td>
<td id="8"></td>
<td id="9"></td>
</tr>
</table>