Alignment of tables in HTML - html

How do I place 3 different tables - L1, L2, R1 in the specified below position:
L1 on Top Left - 2 rows
L2 immediately below L1 on the Left - 2 rows.
R1 on the Top Right - This has 10 rows
The issue is L2 is placed way below L1 as it is inline, how can I make it appear right below L1? Should float left / right or any other setting can help me achieve this?
https://jsfiddle.net/cyppyntp/4/
<table class="one" id="L1">
<tr>
<th>Month-1</th>
<th>Savings-1</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
<table class="one" id="L2">
<tr>
<th>Month-2</th>
<th>Savings-2</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$200</td>
</tr>
<tr>
<td>March</td>
<td>$100</td>
</tr>
<tr>
<td>April</td>
<td>$600</td>
</tr>
<tr>
<td>May</td>
<td>$100</td>
</tr>
<tr>
<td>June</td>
<td>$100</td>
</tr>
<tr>
<td>July</td>
<td>$100</td>
</tr>
<tr>
<td>August</td>
<td>$100</td>
</tr>
<tr>
<td>Sep</td>
<td>$100</td>
</tr>
<tr>
<td>Oct</td>
<td>$100</td>
</tr>
<tr>
<td>Nov</td>
<td>$100</td>
</tr>
</table>
<table class="two" id="R1">
<tr>
<th>Month-3</th>
<th>Savings-3</th>
</tr>
<tr>
<td>December</td>
<td>$400</td>
</tr>
</table>
.inlineTable {
display: inline-block;
}
table.one {
width:45%;
display:inline-block;
vertical-align:top;
}
table.two {
text-align:left;
}

See this fiddle
I've changed the order of your tables slightly. Also, I have added two new divs. See the HTML below
<div class="left">
<table class="one">
<tr>
<th>Month-1</th>
<th>Savings-1</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
<table class="two">
<tr>
<th>Month-3</th>
<th>Savings-3</th>
</tr>
<tr>
<td>December</td>
<td>$400</td>
</tr>
</table>
</div>
<div class="right">
<table class="one">
<tr>
<th>Month-2</th>
<th>Savings-2</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$200</td>
</tr>
<tr>
<td>March</td>
<td>$100</td>
</tr>
<tr>
<td>April</td>
<td>$600</td>
</tr>
<tr>
<td>May</td>
<td>$100</td>
</tr>
<tr>
<td>June</td>
<td>$100</td>
</tr>
<tr>
<td>July</td>
<td>$100</td>
</tr>
<tr>
<td>August</td>
<td>$100</td>
</tr>
<tr>
<td>Sep</td>
<td>$100</td>
</tr>
<tr>
<td>Oct</td>
<td>$100</td>
</tr>
<tr>
<td>Nov</td>
<td>$100</td>
</tr>
</table>
</div>
And, also added the below given CSS
.left,.right{
float:left;
width:50%;
}

I'm not a big fan of floats, but they work here without any change to your markup other than class values. You could accomplish the same without even that by using a :first-child selector.
.inlineTable {
display: inline-block;
}
table {
width: 45%;
vertical-align: top;
background: #ddd;
margin-bottom: 10px;
}
table.left {
float: left;
}
table.right {
float: right;
}
<table class="left">
<tr>
<th>Month-1</th>
<th>Savings-1</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
<table class="right">
<tr>
<th>Month-2</th>
<th>Savings-2</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$200</td>
</tr>
<tr>
<td>March</td>
<td>$100</td>
</tr>
<tr>
<td>April</td>
<td>$600</td>
</tr>
<tr>
<td>May</td>
<td>$100</td>
</tr>
<tr>
<td>June</td>
<td>$100</td>
</tr>
<tr>
<td>July</td>
<td>$100</td>
</tr>
<tr>
<td>August</td>
<td>$100</td>
</tr>
<tr>
<td>Sep</td>
<td>$100</td>
</tr>
<tr>
<td>Oct</td>
<td>$100</td>
</tr>
<tr>
<td>Nov</td>
<td>$100</td>
</tr>
</table>
<table class="left">
<tr>
<th>Month-3</th>
<th>Savings-3</th>
</tr>
<tr>
<td>December</td>
<td>$400</td>
</tr>
</table>
Demo

Related

One Function works on different tables

I have a table generated in a backend. My task is to wrap text in each header column and add it to each body column cells by using .html() and .prepend(). It works as expected (you will see text in green).
Problem: If there are 2 independent tables in a page, it will be mess up. Green text in the first table looks as expected, but it will take header text from the first table and add to body column cells of second table as an image below:
The 2 tables should be like the image below
How to fix this issue, please give me a hand. Thanks
$(document).ready(function() {
$("table thead tr th").each(function(i) {
let bodyCode = $("<span>", {
text: $(this).text()
});
$(`table tbody tr td:nth-child(${i+1})`).prepend(bodyCode);
});
});
table {
width: 100%;
border: 1px solid gray
}
span {
color: green
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>
<table>
<thead>
<tr>
<th>Day</th>
<th>Earn</th>
</tr>
</thead>
<tbody>
<tr>
<td>Monday</td>
<td>$100</td>
</tr>
<tr>
<td>Tuesday</td>
<td>$80</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
</table>
</p>
<table>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
<td>$200</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
<td>$180</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
</table>
The main issue is that you need to loop through each table first.
So this code:
Gets each table element in the each.
Gets the th elements in that specific table using each
Gets the text you want from the th element and sets it to <span> tag
Then gets the current th element, finds its parent (table), and then finds each td element.
$(document).ready(function() {
// Loop through each table
$("table").each(function() {
// find all the tds in each table
let th = $(this).find('thead tr th');
// Loop through each th in the table
th.each(function(i) {
let bodyCode = $("<span>", {
text: $(this).text()
});
// find the td elements in this specific table.
$(this).parents('table').find(`tbody td:nth-child(${i+1})`).prepend(bodyCode);
});
});
});
table {
width: 100%;
border: 1px solid gray
}
span {
color: green
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Day</th>
<th>Earn</th>
</tr>
</thead>
<tbody>
<tr>
<td>Monday</td>
<td>$100</td>
</tr>
<tr>
<td>Tuesday</td>
<td>$80</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
</table>
<br>
<table>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
<td>$200</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
<td>$180</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
</table>
<p> added extra tables for proof of concept</p>
<table>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
<td>$200</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
<td>$180</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
</table>
<br>
<table>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
</table>

Html Rowspan Put on the top

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>

Table parallel rowspans

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>

How to separate nested table conspicuously?

How can i separate nested table in lower table as conspicuously as in upper table?
Upper table is not proper though.
I am trying to get Settlements Finance Time 3 Time 5 Bad Good in a single line in lower table.
Here is my code:
.my-table {
border: 1px solid #000;
}
.my-table tr:nth-child(even) {
background: #ddd;
}
.my-table tr:nth-child(odd) {
background: #fff;
}
<table class="my-table">
<tr>
<th>Service</th>
<th>Provider</th>
<th>Check</th>
<th>Marker</th>
<th>Captured Time</th>
<th>Final Time</th>
<th>Status</th>
<th>Comments</th>
</tr>
<tr>
<th>Sub Heading</th>
</tr>
<tr>
<td>Custody C</td>
<td>I</td>
<td>
<table>
<tr>
<td>Settlements</td>
<td>Finance</td>
<td>Time 3</td>
<td>Time 5</td>
<td>Bad</td>
<td>Good</td>
</tr>
</table>
<table>
<tr>
<td>Crossroad</td>
<td>
<table>
<tr>
<td>Complete</td>
</tr>
<tr>
<td>Partial</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>Time 4a</td>
</tr>
<tr>
<td>Time 4b</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>Time 6a</td>
</tr>
<tr>
<td>Time 6b</td>
</tr>
</table>
</td>
<td>Ok</td>
<td>danke</td>
</tr>
</table>
</td>
</tr>
</table>
<br>
<table class="my-table">
<tr>
<th>Service</th>
<th>Provider</th>
<th>Check</th>
<th>Marker</th>
<th>Captured Time</th>
<th>Final Time</th>
<th>Status</th>
<th>Comments</th>
</tr>
<tr>
<th>Sub Heading</th>
</tr>
<tr>
<td>Custody T</td>
<td>G</td>
<td>
<table>
<tr>
<td>Trades</td>
</tr>
<tr>
<td>Position</td>
</tr>
</table>
</td>
<td>Latest</td>
<td>TIME 1</td>
<td>TIME 2</td>
<td>Good</td>
<td>My Comments</td>
</tr>
<tr>
<td>Custody C</td>
<td>I</td>
<td>
<table>
<tr>
<td>Settlements</td>
</tr>
<tr>
<td>Crossroad</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>Finance</td>
</tr>
<tr>
<td>
<table>
<tr>
<td>Complete</td>
</tr>
<tr>
<td>Partial</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>Time 3</td>
</tr>
<tr>
<td>
<table>
<tr>
<td>Time 4a</td>
</tr>
<tr>
<td>Time 4b</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>
Time 5
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td>Time 6a</td>
</tr>
<tr>
<td>Time 6b</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>Bad</td>
</tr>
<tr>
<td>Ok</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>Good</td>
</tr>
<tr>
<td>danke</td>
</tr>
</table>
</td>
</tr>
<tr>
</tr>
</table>
To answer the question:
I am trying to get Settlements Finance Time 3 Time 5 Bad Good in a single line in lower table.
Vertically aligning your table cells will bring the content to the top of the cells, regardless of how many lines of content follows.
.my-table td {
vertical-align: top;
}
JSFiddle Example: https://jsfiddle.net/jennifergoncalves/y1Lr0mzw/
Please let me know if I missed what you were trying to accomplish.

add scroll bar to table body

I want to do this as easily as possible with out any additional libraries.
In my very long table I want to add a scrollbar to the <tbody> tag so that the head is always visible but it wont work. can you please help.
fiddle : http://jsfiddle.net/Hpx4L/
<table border="1">
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tfoot>
<tr>
<td>Sum</td>
<td>$180</td>
</tr>
</tfoot>
<tbody style="overflow-y:scroll; height:100px;"> <!-- wont work -->
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</tbody>
</table>
you can wrap the content of the <tbody> in a scrollable <div> :
html
....
<tbody>
<tr>
<td colspan="2">
<div class="scrollit">
<table>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
...
css
.scrollit {
overflow:scroll;
height:100px;
}
see my jsfiddle, forked from yours: http://jsfiddle.net/VTNax/2/
These solutions often have issues with the header columns aligning with the body columns, and may not work properly when resizing. I know you didn't want to use an additional library, but if you happen to be using jQuery, this one is really small. It supports fixed header, footer, column spanning (colspan), horizontal scrolling, resizing, and an optional number of rows to display before scrolling starts.
jQuery.scrollTableBody (GitHub)
As long as you have a table with proper <thead>, <tbody>, and (optional) <tfoot>, all you need to do is this:
$('table').scrollTableBody();
This is because you are adding your <tbody> tag before <td> in table you cannot print any data without <td>.
So for that you have to make a <div> say #header with position: fixed;
header
{
position: fixed;
}
make another <div> which will act as <tbody>
tbody
{
overflow:scroll;
}
Now your header is fixed and the body will scroll. And the header will remain there.
If you don't want to wrap a table under any div:
table{
table-layout: fixed;
}
tbody{
display: block;
overflow: auto;
}