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

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>

Related

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>

Table cells are not the same height within row

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>
`

Adjust spacing between HTML table rows

I want to display records one by one in sequence, I do not know why the extra space appears between the records. Can anyone help me please?
{block name=head}
<style>
td {
border: none;
font-weight: bold;
font-size: 100%;
}
.h1 {
margin: 3px, 0px;
}
.content {
height: 270px;
border: 1px solid black;
font-size: 100%;
width: 80%;
margin: 0 auto;
border-top: none;
font-family: Times New Roman;
}
.footer {
height: 30px;
border: 1px solid black;
width: 80%;
margin: 0 auto;
font-family: Terminal;
}
.details-table {
border-collapse: collapse;
}
.details-table td {
border-bottom: 1px solid #666;
padding: 5px;
border-right: 1px solid #666;
}
.details-table td.no-right-border {
border-right: none;
}
.details-table td.no-bottom-border {
border-bottom: none;
}
</style>
{/block}
{block name=body}
<div class="footer">
<table align="center" width="80%">
<tr>
<td style="text-align:left;">Name : {$partyName}</td>
<td style="text-align:center;">Date : {$salesDate}</td>
<td style="text-align:right;">Fine : {$totFine|string_format:"%.3f"}</td>
</tr>
</table>
</div>
<div class="content">
<table align="center" width="100%" class="details-table">
<tr height="30">
<td>Item</td>
<td style="text-align:center;">Gross</td>
<td style="text-align:center;">Less</td>
<td style="text-align:center;">Net</td>
<td style="text-align:center;">Touch</td>
<td style="text-align:center;">Wastage</td>
<td style="text-align:center;">Fine</td>
</tr>
{section name="sec" loop=$dtArray}
<tr>
<td valign="top" class="no-bottom-border">{$dtArray[sec].itemId}</td>
<td valign="top" style="text-align:center;" class="no-bottom-border">{$hsnCode}</td>
<td valign="top" style="text-align:center;" class="no-bottom-border">{$fine}</td>
<td valign="top" style="text-align:center;" class="no-bottom-border">{$rate}</td>
<td valign="top" style="text-align:center;" class="no-bottom-border">{$amount|string_format:"%.2f"}</td>
<td valign="top" style="text-align:center;" class="no-bottom-border">{$amount|string_format:"%.2f"}</td>
<td valign="top" style="text-align:center;" class="no-bottom-border">{$amount|string_format:"%.2f"}</td>
</tr>
{/section}
</table>
</div>
{/block}
You have height:270px set on the <div class="content">, and height="100%" set on the <table>, and the table is an immediate child of that div, so it inherits the height. That's why you're seeing the extra space when there are only a couple of rows. Simply reset or remove either of the height value to fix that.
EDIT
In the other case, if you want to keep the height set on the table, and only have the empty space to display at the bottom, you can add an empty row and set it to height:100%.
.details-table {
height: 270px;
width: 80%;
margin: 0 auto;
border-collapse: collapse;
}
.details-table td {
border: 1px solid #666;
padding: 5px;
}
<table class="details-table">
<tr>
<td>Item</td>
<td>Gross</td>
<td>Less</td>
<td>Net</td>
<td>Touch</td>
<td>Wastage</td>
<td>Fine</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<!-- insert empty row -->
<tr style="height:100%;">
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>

How to add horizontal lines in Table

I want two horizontal between both records and some extra bottom padding to add a symbol
Edit/update :
I am hard-coding what I want as below
table {
border: 1px solid black;
padding: 0em 2em;
}
tr {
border: 1px solid black;
padding: 0em 2em;
}
tr:nth-child(3) {
margin: 0px;
padding: 0px;
}
tr:nth-child(7) {
background-color: red
}
td:nth-child(21) {
border-bottom: 1px solid blue;
}
<table>
<tr>
<th colspan="2">Old_records</th>
<td>32</td>
</tr>
<tr>
<th>Records_fetched</th>
<td colspan="2">100</td>
</tr>
<tr>
<td colspan="3"> -----------------------------</td>
</tr>
<tr>
<th>Sum </th>
<td colspan="2">132</td>
</tr>
<tr>
<th>New_records</th>
<td></td>
<td>80</td>
</tr>
<tr>
<td colspan="3"> -----------------------------</td>
</tr>
<tr>
<th>Differnce </th>
<td colspan="2">52</td>
</tr>
</table>
Still I need symbols to be added and I an better way to add border instead of this row <tr><td colspan="3"> -----------------------------</td></tr>
Can someone suggest me how to do that it properly?
Add border in tr and apply border-collapse:collapse for table.
table {
border: 1px solid black;
padding:0em 2em;
border-collapse: collapse;
}
tr {
border-bottom: 1px solid black;
}
td {
padding: 2em;
}
<table>
<tr>
<th>Old_records</th>
<td> 32 </td>
</tr>
<tr>
<th>Records_fetched</th>
<td>100</td>
</tr>
<tr>
<th>NEw_records</th>
<td>80</td>
</tr>
</table>
Try the below code
<style>
table {
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
}
</style>
<table>
<tr>
<th>Old_records</th>
<td> 32 </td>
</tr>
<tr>
<th>Records_fetched</th>
<td>100</td>
</tr>
<tr>
<th>NEw_records</th>
<td>80</td>
</tr>
</table>
To insert an empty row, you can write:
<tr>
<td colspan="2"> </td>
</tr>
For extra padding, where you need - just add a class="extra-padding-bottom" attribute
And add appropriate CSS code:
.extra-bottom-padding {
padding-bottom: 100px;
}
For example <td class="extra-padding-bottom">
table {
border: 1px solid black;
padding: 0em 2em;
}
tr {
border: 1px solid black;
padding: 0em 2em;
}
tr:nth-child(3) {
margin: 0px;
padding: 0px;
}
tr:nth-child(even) > th,
tr:nth-child(even) > td {
padding-bottom: 0.75em;
border-bottom: 1px dashed #222;
}
tr:nth-child(odd) > th,
tr:nth-child(odd) > td {
padding-top: 0.75em;
}
<table>
<tr>
<th colspan="2">Old_records</th>
<td>32</td>
</tr>
<tr>
<th>Records_fetched</th>
<td colspan="2">100</td>
</tr>
<tr>
<th>Sum </th>
<td colspan="2">132</td>
</tr>
<tr>
<th>New_records</th>
<td></td>
<td>80</td>
</tr>
<tr>
<th>Differnce </th>
<td colspan="2">52</td>
</tr>
</table>
The solution worked for me is defining css properties at column level and defining colspan as the number of columns in the table
HTML -
<tr class="border_bottom">
<td colspan="6"></td>
</tr>
CSS -
tr.border_bottom td {
border-bottom: 1px solid #cccccc;
color: #707070;
}
table {
border-collapse: collapse;
}
<tr>
<td><hr> </td>
<td><hr> </td>
</tr>
I tried this, it worked

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>