I just want to ask if how I can put the <td> on the top since I used a rowspan on the <td>. Please see sample below.
This is my code:
<table>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
<tr>
<td rowspan="2">
<div>January</div>
<div>test</div>
<div>test</div>
</td>
</tr>
<tr class="rowsl">
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td rowspan="2">January</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
I want to have it this way like picture:
You've got multiple solutions.
My main suggestion will be to remove all rowspans and the multiple trs.
See the code I changed into comment in the snippet below:
<table border="1">
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
<tr>
<td><!-- rowspan="2"-->
<div>January</div>
<div>test</div>
<div>test</div>
</td>
<!--/tr>
<tr class="rowsl"-->
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td><!-- rowspan="2"-->January</td>
<!--/tr>
<tr-->
<td>February</td>
<td>$80</td>
</tr>
</table>
From the snippet above, if you ever need to add lines in the td, you can add divs like you did for the month, and maybe stylize it a little, like that:
td div {
border-bottom: 1px solid black;
}
/* Or use the one below if you don't want the first column to be stylized
td:not(:first-child) div {
border-bottom: 1px solid black;
}
*/
<table border="1">
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
<tr>
<td>
<div>January</div>
<div>test</div>
<div>test</div>
</td>
<td>
<div>February</div>
<div>test</div>
<div>test</div>
</td>
<td>
<div>$80</div>
<div>test</div>
<div>test</div>
</td>
</tr>
<tr>
<td>January</td>
<td>February</td>
<td>$80</td>
</tr>
</table>
Or, you can do the following, keep the rowspan and add the empty tds that will make your table structure consistent:
<table border="1">
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
<!-- Do the following -->
<tr>
<td rowspan="2">
<div>January</div>
<div>test</div>
<div>test</div>
</td>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>-</td>
<td>-</td>
</tr>
<tr>
<td>January</td>
<td>February</td>
<td>$80</td>
</tr>
</table>
Why you can move all rowspan td in one tr
<table border="1">
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
<tr>
<td rowspan="2">
<div>January</div>
<div>test</div>
<div>test</div>
</td>
<td rowspan="2" style="vertical-align:top">February</td>
<td rowspan="2" style="vertical-align:top">$80</td>
</tr>
<tr class="rowsl">
</tr>
<tr>
<td rowspan="2">January</td>
<td>February</td>
<td>$80</td>
</tr>
</table>
Not sure my answer is what you are looking for:
<table border ="1">
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
<tr >
<td rowspan="2" >
<div>January</div><div>test</div><div>test</div>
</td>
<td>February</td>
<td>$80</td>
</tr>
<tr class="rowsl">
</tr>
<tr>
<td rowspan="2">January</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
The only way I see is putting it in the same row, but then you will have the same height:
<table border="1">
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
<tr>
<td rowspan="2">
<div>January</div>
<div>test</div>
<div>test</div>
</td>
<td rowspan="2" >February</td>
<td rowspan="2" >$80</td>
</tr>
<tr>
<td rowspan="2">January</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
You may also write as below code,
<table border=1>
<tr>
<th>Month</th>
<th>Savings</th>
<th rowspan="2">Savings for holiday!</th>
</tr>
<tr>
<td rowspan="2" >
<div>January</div>
<div>test</div>
<div>test</div>
</td>
</tr>
<tr class="rowsl">
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td rowspan="2">January</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
Related
My HTML page has table and 2 inner tables inside as td inside tr. The problem I have encountered is displacement in a case when values in two tables are different in terms of characters value. Here's my page:
<html>
<head>
</head>
<body>
<table>
<tr>
<td>1st input field</td>
</tr>
<tr>
<td>
<table style="width: 100%">
<tr>
<td>All:</td>
<td>500</td>
<td>$</td>
</tr>
<tr>
<td>part1:</td>
<td>100</td>
<td>$</td>
</tr>
<tr>
<td>part2:</td>
<td>140</td>
<td>$</td>
</tr>
<tr>
<td>part3:</td>
<td>260</td>
<td>$</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>2nd input field</td>
</tr>
<tr>
<td>
<table style="width: 100%">
<tr>
<td>All:</td>
<td>700</td>
<td>$</td>
</tr>
<tr>
<td>part1:</td>
<td>200</td>
<td>$</td>
</tr>
<tr>
<td>part2:</td>
<td>200</td>
<td>$</td>
</tr>
<tr>
<td>part3:</td>
<td>300</td>
<td>$</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
Now let's say that inside 1st inner table property all has 7 chars:
<html>
<head>
</head>
<body>
<table>
<tr>
<td>1st input field</td>
</tr>
<tr>
<td>
<table style="width: 100%">
<tr>
<td>All:</td>
<td>500.123</td>
<td>$</td>
</tr>
<tr>
<td>part1:</td>
<td>100</td>
<td>$</td>
</tr>
<tr>
<td>part2:</td>
<td>140</td>
<td>$</td>
</tr>
<tr>
<td>part3:</td>
<td>260</td>
<td>$</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>2nd input field</td>
</tr>
<tr>
<td>
<table style="width: 100%">
<tr>
<td>All:</td>
<td>700</td>
<td>$</td>
</tr>
<tr>
<td>part1:</td>
<td>200</td>
<td>$</td>
</tr>
<tr>
<td>part2:</td>
<td>200</td>
<td>$</td>
</tr>
<tr>
<td>part3:</td>
<td>300</td>
<td>$</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
Thankfully, every value from the first table "went" to the left, but in the 2nd table values remain still on its positions. How can I change it?
Well, I needed to redesign your codes, I removed the child tables and added colspan to the header. Although I'm not sure why you needed to use another table as children.
<table>
<tr>
<td colspan="3">1st input field</td>
</tr>
<tr>
<td>
<tr>
<td>All:</td>
<td>500.123</td>
<td>$</td>
</tr>
<tr>
<td>part1:</td>
<td>100</td>
<td>$</td>
</tr>
<tr>
<td>part2:</td>
<td>140</td>
<td>$</td>
</tr>
<tr>
<td>part3:</td>
<td>260</td>
<td>$</td>
</tr>
</td>
</tr>
<tr>
<td colspan="3">2nd input field</td>
</tr>
<tr>
<td>
<tr>
<td>All:</td>
<td>700</td>
<td>$</td>
</tr>
<tr>
<td>part1:</td>
<td>200</td>
<td>$</td>
</tr>
<tr>
<td>part2:</td>
<td>200</td>
<td>$</td>
</tr>
<tr>
<td>part3:</td>
<td>300</td>
<td>$</td>
</tr>
</td>
</tr>
</table>
The reason here is you have not set sizes for table cells. Cell with number 500.123 in it has more width, so the column becomes much wider, then in the second table. You can solve the problem by using simple CSS, just set some classes and then set them certain width.
td {
border: 1px solid black;
}
tr td:first-child {
width: 40px;
}
tr td:nth-child(2) {
width: 50px;
}
<html>
<head>
</head>
<body>
<table>
<tr>
<td>1st input field</td>
</tr>
<tr>
<td>
<table style="width: 100%">
<tr>
<td>All:</td>
<td>500.123</td>
<td>$</td>
</tr>
<tr>
<td>part1:</td>
<td>100</td>
<td>$</td>
</tr>
<tr>
<td>part2:</td>
<td>140</td>
<td>$</td>
</tr>
<tr>
<td>part3:</td>
<td>260</td>
<td>$</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>2nd input field</td>
</tr>
<tr>
<td>
<table style="width: 100%;">
<tr>
<td>All:</td>
<td>700</td>
<td>$</td>
</tr>
<tr>
<td>part1:</td>
<td>200</td>
<td>$</td>
</tr>
<tr>
<td>part2:</td>
<td>200</td>
<td>$</td>
</tr>
<tr>
<td>part3:</td>
<td>300</td>
<td>$</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
Update
More solved examples here
--
I am having a hard time with rowspans in a table.
I want to achieve the followings.
Explanation is always appreciated.
Also, any other example you think it would help understand, please share.
Here's the snippet:
<table border='1'>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
<tr>
<td rowspan="2">January</td>
<td>$100</td>
<td>$50</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
<br><br><br>
<table border='1'>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
<tr>
<td rowspan="2">January</td>
<td>$100</td>
<td>$50</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
try the following code it's easy to understand if you get to know about how the flow of table works
<table border='1'>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
<tr>
<td rowspan="2">January</td>
<td rowspan="2">$100</td>
<td>$50</td>
</tr>
<tr>
<td>$80</td>
</tr>
</table>
<br><br><br>
<table border='1'>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Savings for holiday!</th>
</tr>
<tr>
<td rowspan="2">January</td>
<td>$100</td>
<td rowspan="2">$50</td>
</tr>
<tr>
<td>February</td>
</tr>
</table>
I have requirement to distributing rows equally in table.
We assume there are 19 records in database. So we have to arrange data in following maner
ASC1 ASC8 ASC14
ASC2 ASC9 ASC15
ASC3 ASC10 ASC16
ASC4 ASC11 ASC17
ASC5 ASC12 ASC18
ASC6 ASC13 ASC19
ASC7
user will select in how many columns he want to display data. Assume he select as columns to display so output should be first column : 7 rows , second column : 6 rows and Third column: 6 rows. So basically its dynamic display.
I have completed all Logic but somehow i am getting Space in second and Third row. Here is my html output
<table style='width:480px' border='1'>
<tr>
<td style='width:160px'>
<table>
<tr>
<td>ASC1</td>
</tr>
</table>
<table>
<tr>
<td>ASC2</td>
</tr>
</table>
<table>
<tr>
<td>ASC3</td>
</tr>
</table>
<table>
<tr>
<td>ASC4</td>
</tr>
</table>
<table>
<tr>
<td>ASC5</td>
</tr>
</table>
<table>
<tr>
<td>ASC6</td>
</tr>
</table>
<table>
<tr>
<td>ASC7</td>
</tr>
</table>
</td>
<td style='width:160px'>
<table>
<tr>
<td>ASC8</td>
</tr>
</table>
<table>
<tr>
<td>ASC9</td>
</tr>
</table>
<table>
<tr>
<td>ASC10</td>
</tr>
</table>
<table>
<tr>
<td>ASC11</td>
</tr>
</table>
<table>
<tr>
<td>ASC12</td>
</tr>
</table>
<table>
<tr>
<td>ASC13</td>
</tr>
</table>
</td>
<td style='width:160px'>
<table>
<tr>
<td>ASC14</td>
</tr>
</table>
<table>
<tr>
<td>ASC15</td>
</tr>
</table>
<table>
<tr>
<td>ASC16</td>
</tr>
</table>
<table>
<tr>
<td>ASC17</td>
</tr>
</table>
<table>
<tr>
<td>ASC</td>
</tr>
</table>
<table>
<tr>
<td>ASC</td>
</tr>
</table>
</td>
</tr>
</table>
Text ASC8 and ASC14 should align text ASC1. Can you help me?
You can add a vertical align and set it to top:
table td, table td * {
vertical-align: top;
}
See it live:
<style>
table td, table td * {
vertical-align: top;
}
</style>
<table style='width:480px' border='1'>
<tr>
<td style='width:160px'>
<table>
<tr>
<td>ASC1</td>
</tr>
</table>
<table>
<tr>
<td>ASC2</td>
</tr>
</table>
<table>
<tr>
<td>ASC3</td>
</tr>
</table>
<table>
<tr>
<td>ASC4</td>
</tr>
</table>
<table>
<tr>
<td>ASC5</td>
</tr>
</table>
<table>
<tr>
<td>ASC6</td>
</tr>
</table>
<table>
<tr>
<td>ASC7</td>
</tr>
</table>
</td>
<td style='width:160px'>
<table>
<tr>
<td>ASC8</td>
</tr>
</table>
<table>
<tr>
<td>ASC9</td>
</tr>
</table>
<table>
<tr>
<td>ASC10</td>
</tr>
</table>
<table>
<tr>
<td>ASC11</td>
</tr>
</table>
<table>
<tr>
<td>ASC12</td>
</tr>
</table>
<table>
<tr>
<td>ASC13</td>
</tr>
</table>
</td>
<td style='width:160px'>
<table>
<tr>
<td>ASC14</td>
</tr>
</table>
<table>
<tr>
<td>ASC15</td>
</tr>
</table>
<table>
<tr>
<td>ASC16</td>
</tr>
</table>
<table>
<tr>
<td>ASC17</td>
</tr>
</table>
<table>
<tr>
<td>ASC</td>
</tr>
</table>
<table>
<tr>
<td>ASC</td>
</tr>
</table>
</td>
</tr>
</table>
i wanna create something like this in my table
i try with this code, but the result far from what i want
<table>
<tr>
<td rowspan="3">No</td>
<td rowspan="3">Item1</td>
</tr>
<tr>
<td>SubItem1a</td>
<td>Subitem1b</td>
<td>Subitem1c</td>
</tr>
<tr>
<td>AnotherSub1a</td>
<td>AnotherSub1b</td>
<td>AnotherSub1c</td>
</tr>
</table>
or
<table>
<tr>
<td rowspan="3">No</td>
<td rowspan="3">Item1</td>
<td>
<tr>SubItem1a</tr>
<tr>Subitem1b</tr>
<tr>Subitem1c</tr>
</td>
<td>
<tr>AnotherSub1a</tr>
<tr>AnotherSub1b</tr>
<tr>AnotherSub1c</tr>
</td>
</tr>
</table>
how to archive table like image?
Now used to this code rowspan Attribute
<table border="1" style="border-collapse: collapse; border-spacing: 0; width: 100%;">
<tr>
<td rowspan="3" align="center">No</td>
<td rowspan="3" align="center">Item1</td>
<td>SubItem1a</td>
<td>SubItem1a</td>
</tr>
<tr>
<td>Subitem1b</td>
<td>Subitem1c</td>
</tr>
<tr>
<td>AnotherSub1b</td>
<td>AnotherSub1c</td>
</tr>
</table>
This will help with your future reference.
I did some example hopefully you understand what is rowspan and colspan.
<h1> Without using Rowspan </h1>
<table border="1">
<tr>
<th>How</th>
<th>it</th>
<th>Work</th>
<th>Rowspan</th>
<th>First</th>
</tr>
<tr>
<td>10</td>
<td >20</td>
<td >30</td>
<td >40</td>
<td >50</td>
</tr>
<tr>
<td>60</td>
</tr>
<tr>
<td>70</td>
<td>80</td>
<td>90</td>
<td>100</td>
<td>110</td>
</tr>
</table>
<hr>
<h1> using Rowspan </h1>
<table border="1">
<tr>
<th>How</th>
<th>it</th>
<th>Work</th>
<th>Rowspan</th>
<th>First</th>
</tr>
<tr>
<td rowspan="2">10</td>
<td rowspan="2">20</td>
<td rowspan="2">30</td>
<td rowspan="2" >40</td>
<td >50</td>
</tr>
<tr>
<td>60</td>
</tr>
<tr>
<td>70</td>
<td>80</td>
<td>90</td>
<td>100</td>
<td>110</td>
</tr>
</table>
<hr>
<h1> using colspan </h1>
<table border="1">
<tr>
<th colspan="5">Hello World</th>
</tr>
<tr>
<th>How</th>
<th>it</th>
<th>Work</th>
<th>Rowspan</th>
<th>First</th>
</tr>
<tr>
<td rowspan="2">10</td>
<td rowspan="2">20</td>
<td rowspan="2">30</td>
<td rowspan="2" >40</td>
<td >50</td>
</tr>
<tr>
<td>60</td>
</tr>
<tr>
<td>70</td>
<td>80</td>
<td>90</td>
<td>100</td>
<td>110</td>
</tr>
</table>
DEMO
Hey guys I was creating this table table recently in HTML.
image:http://imgur.com/wPXCwrd
The table (shown in image) created perfectly but a weird blank row was created (marked red in image). When I tried to delete that code the entire cells below the QUESTIONS row gets shifted (image link:http://imgur.com/jBfmNGV).
How do I remove that blank cell after MEN cell?
CODE:
<!DOCTYPE html>
<html>
<body>
<table align="left" border="1" cellpadding="3" cellspacing="3">
<tbody>
<tr>
<td colspan="5">QUESTIONAIRE RESULTS</td>
</tr>
<tr>
<td rowspan="3">QUESTIONS</td>
<td colspan="2" rowspan="2">WOMEN</td>
<td colspan="2" rowspan="2">MEN</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>YES</td>
<td>NO</td>
<td>YES</td>
<td>NO</td>
</tr>
<tr>
<td>Question 1</td>
<td>42%</td>
<td>58%</td>
<td>61%</td>
<td>39%</td>
</tr>
<tr>
<td>Question 2</td>
<td>53%</td>
<td>47%</td>
<td>69%</td>
<td>31%</td>
</tr>
<tr>
<td>Question 3</td>
<td>26%</td>
<td>74%</td>
<td>51%</td>
<td>49%</td>
</tr>
<tr>
<td>Question 4</td>
<td>40%</td>
<td>60%</td>
<td>60%</td>
<td>40%</td>
</tr>
</tbody>
</table>
</body>
</html>
Just remove the :
<td> </td>
so your final code will be :
<!DOCTYPE html>
<html>
<body>
<table align="left" border="1" cellpadding="3" cellspacing="3">
<tbody>
<tr>
<td colspan="5">QUESTIONAIRE RESULTS</td>
</tr>
<tr>
<td rowspan="3">QUESTIONS</td>
<td colspan="2" rowspan="2">WOMEN</td>
<td colspan="2" rowspan="2">MEN</td>
</tr>
<tr>
</tr>
<tr>
<td>YES</td>
<td>NO</td>
<td>YES</td>
<td>NO</td>
</tr>
<tr>
<td>Question 1</td>
<td>42%</td>
<td>58%</td>
<td>61%</td>
<td>39%</td>
</tr>
<tr>
<td>Question 2</td>
<td>53%</td>
<td>47%</td>
<td>69%</td>
<td>31%</td>
</tr>
<tr>
<td>Question 3</td>
<td>26%</td>
<td>74%</td>
<td>51%</td>
<td>49%</td>
</tr>
<tr>
<td>Question 4</td>
<td>40%</td>
<td>60%</td>
<td>60%</td>
<td>40%</td>
</tr>
</tbody>
</table>
</body>
</html>
Preview: https://jsfiddle.net/3q6n9v7k/
A more correct answer would be the changing the rowspan and colspan.
Instead of
<tr>
<td rowspan="3">QUESTIONS</td> //
<td colspan="2" rowspan="2">WOMEN</td>
<td colspan="2" rowspan="2">MEN</td>
</tr>
Do this
<tr>
<td rowspan="2">QUESTIONS</td>
<td colspan="2" rowspan="1">WOMEN</td>
<td colspan="2" rowspan="1">MEN</td>
</tr>
Also remove :
<tr>
<td> </td>
</tr>
check out Fiddle