Prevent <td> from expanding when inserting content - html

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>

Related

How to make a table cell merge two rows and two columns?

Here is what I need to do:
Red: two columns and two rows
Purple: two columns
Blue: two rows
yellow and white: normal cell
Here is my table so far:
Here is my html code:
<body>
<div id="container">
<table id="board">
<tr>
<td colspan="2"></td>
<td></td>
<td colspan="2"></td>
</tr>
<tr>
<td colspan="2" rowspan="2"></td>
<td rowspan="2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="2"></td>
<td ></td>
<td colspan="2"></td>
</tr>
</table>
</div>
</body>
Here is my css:
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
display: flex;
background-color: #114B5F;
text-align: center;
flex-direction: column;
padding-top: 2%;
}
#container {
width: 800px;
height: 500px;
background: #E4FDE1;
margin: 10px auto;
}
#board
{
width: 100%;
height: 100%;
border: 1px solid black;
}
td
{
border: 1px solid black;
}
Somehow the cell that is supposed to occupy two rows and two cells (Red in 1st image) it appears that is only occupying two rows even though the code says otherwise. Is it something on css? How can I fix this?
Your HTML did it already. To see this, do what follows. Comment the first
<td colspan="2"></td>
in the last row of your cell and substitute it with
<td></td>
<td></td>
At least as long as you have no content within any cell, the default width of the first two columns (merged by the consistent colspan="2" attributes) and the third column alone is the same so you do not notice the difference. I add a snippet just to show:
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
display: flex;
background-color: #114B5F;
text-align: center;
flex-direction: column;
padding-top: 2%;
}
#container {
width: 800px;
height: 500px;
background: #E4FDE1;
margin: 10px auto;
}
#board {
width: 100%;
height: 100%;
border: 1px solid black;
}
td {
border: 1px solid black;
}
td.cblu{background-color:#0040ff}
td.clbl{background-color:#0080ff}
td.cred{background-color:#ff1a1a}
td.cwhi{background-color:#f8f8f8}
td.cyel{background-color:#ffff33}
<body>
<div id="container">
<table id="board">
<tr>
<td class="cblu" colspan="2"></td>
<td class="cwhi"></td>
<td class="cblu" colspan="2"></td>
</tr>
<tr>
<td class="cred" colspan="2" rowspan="2"></td>
<td class="clbl" rowspan="2"></td>
<td class="cyel"></td>
<td class="cyel"></td>
</tr>
<tr>
<td class="cyel"></td>
<td class="cyel"></td>
</tr>
<tr>
<td class="cblu"></td>
<td class="cblu"></td>
<!--<td class="cblu" colspan="2"></td>-->
<td class="cwhi"></td>
<td class="cblu" colspan="2"></td>
</tr>
</table>
</div>
</body>
Here I added the colors using some classes. I'd like to remark that no more edits to your DOM (html) is necessary to achieve what you want. But controlling responsively the width of the cells or columns of your table may not be trivial.

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>

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

How do i print a row with 3 equally sized cells under a row with 4 cells? HTML

The following does not achieve the result
<tr><td colspan="4">Title</td></tr>
<tr><td>1</td><td>1</td><td>1</td><td>1</td></tr>
<tr><td width="33.33%">1</td><td width="33.33%">2</td><td width="33.33%">3</td></tr>
<tr><td width="33.33%"> </td><td width="33.33%"></td><td width="33.33%"></td></tr>
The there is an extra space after the third cell to take into account the 4th column. How can I remove this so that the entire row is equally divided into three cells that fill it?
For equal widths you should create 2 table like this:
<table>
<tr><td colspan="4">Title</td></tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
</table>
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
Here is the fiddle: http://jsfiddle.net/Q57gW/2/
The best answer would be to not use table. If you REALLY need that table, one (very messy) soultion could be to have 12 columns in each row, and have colspan="3" in your first row, and colspan="4" in your other rows.
jsfiddle link for this solution
<table><tbody>
<tr>
<td style="border: 1px solid #333; width: 240px;" colspan="12">Title</td>
</tr>
<tr>
<td style="border: 1px solid #333; width: 60px;" colspan="3">1</td>
<td style="border: 1px solid #333; width: 60px;" colspan="3">1</td>
<td style="border: 1px solid #333; width: 60px;" colspan="3">1</td>
<td style="border: 1px solid #333; width: 60px;" colspan="3">1</td>
</tr>
<tr>
<td style="border: 1px solid #333; width: 80px;" colspan="4">1</td>
<td style="border: 1px solid #333; width: 80px;" colspan="4">2</td>
<td style="border: 1px solid #333; width: 80px;" colspan="4">3</td>
</tr>
</tbody></table>
But I would HEAVILY advice you to use divs or spans or something else than table cells. Here's a much nicer solution using paragraphs:
jsfiddle link
html:
<div style="width: 240px;">
<p class="onecell">Title</p>
<p class="fourcell">1</p><p class="fourcell">1</p><p class="fourcell">1</p><p class="fourcell">1</p>
<p class="threecell">1</p><p class="threecell">2</p><p class="threecell">3</p>
</div>
css:
p {
float: left;
border: 1px solid #333;
box-sizing: border-box;
padding: 0;
margin: 0;
}
p.onecell {
width: 100%;
}
p.fourcell {
width: 25%;
}
p.threecell {
width: 33%;
}