How to create a side by side HTML table grid? - html

I want to create a table with stuff in it like this:
What I have right now:
<table class="table">
<td>
<tr>
<div style="width: 100px;height:100px; background-color: blue;"></div>
</tr>
<tr>
<div style="width: 100px;height:100px; background-color: red;"></div>
</tr>
</td>
<td>
<div style="width: 100px;height:200px; background-color: yellow;"></div>
</td>
</table>

Use rowspan, and td cannot be direct child of a table.
table {
border-collapse: collapse;
}
td {
width: 100px;
text-align: center;
vertical-align: middle;
}
.blue {
background-color: royalblue;
height: 100px;
}
.yellow {
background-color: yellow;
height: 200px;
}
.red {
background-color: red;
height: 100px;
}
<table>
<tr>
<td class="blue">1</td>
<td class="yellow" rowspan="2">3</td>
</tr>
<tr>
<td class="red">2</td>
</tr>
</table>

Well, firstly, your table syntax is off. It should look like this.
<table>
<tr>
<td> </td>
</tr>
</table>
Next, what you want is the first row to have two columns, with the second column taking up the space of two rows. The next row will have one column. This can be done using rowspan.
Here's your refactored code.
<table class="table">
<tr>
<td rowspan="1">
<div style="width: 100px;height:100px; background-color: blue;"></div>
</td>
<td rowspan="2">
<div style="width: 100px;height:200px; background-color: yellow;"></div>
</td>
</tr>
<tr>
<td rowspan="1">
<div style="width: 100px;height:100px; background-color: red;"></div>
</td>
</tr>
</table>

Your code must like this: use rowspan for second td
<table cellpadding="0" cellspacing="0">
<tr>
<td style="width: 100px;height:100px; background-color: blue;"></td>
<td rowspan="2" style="width: 100px;height:200px; background-color: yellow;"></td>
</tr>
<tr>
<td style="width: 100px;height:100px; background-color: red;"></td>
</tr>
</table>
</tr>
</table>

Related

td width not apply when table wrapped with div with specific width and overflow is applied

With the below html, I am not able to add width of td element
here table is wrapped with div and it's width is 200px and overflow-x is given.
<div class="scroll" style="width: 200px; overflow-x: overlay;">
<table class="scrollable">
<tbody>
<tr>
<td width="100" data-index="0" style="width: 118px;">-0.29%</td>
<td data-index="1">-0.26%</td>
<td data-index="2">-0.20%</td><td data-index="3">-0.15%</td>
<td data-index="4">-0.09%</td><td data-index="5">0.19%</td>
<td data-index="6">0.54%</td><td data-index="7">0.95%</td>
<td data-index="8">1.33%</td><td data-index="9">1.67%</td>
<td data-index="10">1.97%</td><td data-index="11">2.22%</td>
<td data-index="12">2.44%</td><td data-index="13">2.62%</td>
<td data-index="14">2.78%</td>
</tr>
</tbody>
</table>
</div>
Try changing the display property of <td> to display:inline-block.
Also you should add this table { width: max-content; } to your CSS so your table can fit all the <td> now that they are bigger.
table {
width: max-content;
}
td {
width: 100px;
display: inline-block;
}
<div class="scroll" style="
width: 200px;
overflow-x: auto;
">
<table class="scrollable">
<tbody>
<tr>
<td data-index="0">-0.29%</td>
<td data-index="1">-0.26%</td>
<td data-index="2">-0.20%</td>
<td data-index="3">-0.15%</td>
<td data-index="4">-0.09%</td>
<td data-index="5">0.19%</td>
<td data-index="6">0.54%</td>
<td data-index="7">0.95%</td>
<td data-index="8">1.33%</td>
<td data-index="9">1.67%</td>
<td data-index="10">1.97%</td>
<td data-index="11">2.22%</td>
<td data-index="12">2.44%</td>
<td data-index="13">2.62%</td>
<td data-index="14">2.78%</td>
</tr>
</tbody>
</table>
</div>

How to make table consistent in all browsers

I am starting to learn HTML. In a table I created I use rowspan, but when I insert an image in a column that is expanded the image seems to not expand in the whole cell. So the rows of the table start to have different heights.
This happens in EDGE and IE but not in OPERA,Firefox etc as is shown in the images below. Column heights are not consistent there too.
Ideally, I would like to have the following heights (assuming fixed row height) for the cells identified by their content:
Headers 1 and 2: 1 column
Headers 3 and 4: 2 columns
Left image height: 4 columns
Right image height: 6 columns
body{
background-color:#FFFF99
}
img {
height: 70px;
display:block;
}
h1{
font-style: italic;
color:#3300CC;
}
table, th, td {
border: 1px solid black;
border-spacing:3px;
}
table {
width: 75%;
}
th {
padding:4px;
text-align:right;
}
td{
padding:4px;
}
.Tablestyle1{
background-color: #00CC66;
text-align: center
}
<h1>TITLE </h1>
<table>
<tr class="Tablestyle1" > <!--First Row-->
<td>A</td>
<td colspan="4">B</td>
</tr>
<tr>
<td class="Tablestyle1"rowspan="2">C</td>
<th>Header1</th>
<td> D</td>
<td rowspan="6"> <img src="https://www.w3schools.com/images/w3schools_green.jpg" alt="alt1" >
<td rowspan="6">
<ol type="I">
<li>List1</li>
<li>List2</li>
<li>List3</li>
</ol>
</tr>
<tr>
<th>Header2</th>
<td>F</td>
</tr>
<tr>
<td rowspan="4">
<img src="https://www.w3schools.com/images/w3schools_green.jpg" alt="alt2" >
</td>
<th rowspan="2">Header3</th>
<td rowspan="2">H</td>
</tr>
<tr></tr>
<tr>
<th rowspan="2">Header4</th>
<td rowspan="2">L</td>
</tr>
<tr></tr>
<tr>
<td colspan="5">
Link1
</td>
</tr>
</table>
Here is a stripped down example with inline css to explain how to style each element. The colspans and rowspans is also simplified.
This is a fixed height solution. If you want the height to be adjusted to the content, you might need some jQuery magic. Flexbox and tables won't work cross browser I think.
<table style="border-collapse: collapse; width: 75%; height: 216px;" border="1">
<tbody>
<tr style="height: 36px;">
<td style="width: 30%; height: 36px;">A</td>
<td style="width: 70%; height: 36px;" colspan="4">B</td>
</tr>
<tr style="height: 36px;">
<td style="width: 30%; height: 72px;" rowspan="2">C</td>
<td style="width: 15%; height: 36px;">1</td>
<td style="width: 10%; height: 36px;">D</td>
<td style="width: 20%; height: 144px;" rowspan="4">Image</td>
<td style="width: 25%; height: 144px;" rowspan="4">List</td>
</tr>
<tr style="height: 36px;">
<td style="width: 15%; height: 36px;">2</td>
<td style="width: 10%; height: 36px;">F</td>
</tr>
<tr style="height: 36px;">
<td style="width: 30%; height: 72px;" rowspan="2">Image</td>
<td style="width: 15%; height: 36px;">3</td>
<td style="width: 10%; height: 36px;">H</td>
</tr>
<tr style="height: 36px;">
<td style="width: 15%; height: 36px;">4</td>
<td style="width: 10%; height: 36px;">L</td>
</tr>
<tr style="height: 36px;">
<td style="width: 100%; height: 36px;" colspan="5">Link</td>
</tr>
</tbody>
</table>
If the content is too big the table could still be distorted – unless you use overflow:hidden https://stackoverflow.com/a/509825/762640
For the height, you can set height to all td's
Another option (also for the width) you can set class for every cell you want, and define the css rules for it.
You can see the example here.
https://jsfiddle.net/5upeywrd/2/
I did the rule for the header cell "A".

Why can't the browser render this table HTML properly?

I'm struggling with TABLE HTML.
I have no idea why this table tag doesn't work properly in browser
<table border="1">
<tbody>
<tr>
<td rowspan="2">1-1</td>
<td rowspan="3">2-1</td>
</tr>
<tr>
<td rowspan="2">1-2</td>
</tr>
<tr>
<td rowspan="3">2-2</td>
</tr>
<tr>
<td rowspan="2">1-3</td>
</tr>
</tbody>
</table>
The html above would be rendered like this
However the view I expected to see is like this
As I figured out, If I want to see what I want in browser, I should fix rowspans like this
<table border="1">
<tbody>
<tr>
<td rowspan="1">1-1</td>
<td rowspan="2">2-1</td>
</tr>
<tr>
<td rowspan="2">1-2</td>
</tr>
<tr>
<td rowspan="2">2-2</td>
</tr>
<tr>
<td rowspan="1">1-3</td>
</tr>
</tbody>
</table>
But I'm really wondering what's different and why The browser (Chrome) doesn't render the first one properly and does the second one.
According to W3C there is no way to specify float value like 1.5 for rowspan but some tweaks like below may help.
<table border="1">
<tbody>
<tr>
<td rowspan="2">1-1</td>
</tr>
<tr>
<td rowspan="2">1-2</td>
</tr>
<tr>
<td rowspan="2">2-1</td>
</tr>
<tr>
<td rowspan="3">2-2</td>
</tr>
<tr>
<td rowspan="2">3-1</td>
</tr>
</tbody>
</table>
Have you tried flexbox yet? It is little bit different approach to solve this.
#main {
width: 100px;
height: 150px;
border: 1px solid #c3c3c3;
align-items: stretch;
display: flex;
flex-flow: column wrap;
}
#main div {
width: 50px;
height: 50px;
line-height: 45px;
text-align: center;
}
#right {
width: 50px;
height: 150px;
}
#right div {
width: 50px;
height: 75px;
line-height: 70px;
text-align: center;
}
<div id="main">
<div style="background-color:lightgrey;" >1-1</div>
<div style="background-color:grey;">1-2</div>
<div style="background-color:lightgrey;">1-3</div>
<div id="right">
<div id="right" style="background-color:lightblue;">2-1</div>
<div id="right" style="background-color:lightgreen;">2-2</div>
</div>
</div>

How to do the horizontal alignment of HTML components

Have a look on target code.Here I am trying to split the main div into 2 equal halves and place the 2 tables at center of each of them.
Please help me to do the same
<div id="title">
Split Screen into 2 equaly halves
</div>
<div >
<table id="myTable">
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
<tr >
<td class="row_top"></td>
<td class="row"></td>
<td class="row_top"></td>
</tr>
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
</table>
<table id="myTable">
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
<tr >
<td class="row_top"></td>
<td class="row"></td>
<td class="row_top"></td>
</tr>
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
</table>
</div>
There is no such thing as splitting one element into two halves. Instead make 2 elements and position them properly.
Make divs 50% of the width of the parent element (body in this case). Make them float to the left.
Align the tables inside using
margin: 50px auto;
Solution: https://jsfiddle.net/hnmgnLat/2/
Place the two tables in different divs.set the width of the two divs to 45% each. Then set the float of the first div to left. I hope this works. The 45% width should give room for margin and padding in case it exist. Or better still set all padding and margin to 0px and set the width to 50%
try like this
<div class="parent">
<div class="child"></div>
<div class="child"></div>
</div>
add this style in css
.parent {
width: 100%;
margin: 0 auto;
}
.child {
min-width:50%;
display:inline-block;
}
you can use an ID only one TIME in your CODE and then you do an left and right float
or you can use flexbox
.row
{
border: 2px solid black;
width: 100px;
height: 100px;
}
.row_top
{
width: 100px;
height: 100px;
border-top:2px solid black;
border-bottom:2px solid black;
}
.row_bottom
{
width: 100px;
height: 100px;
border-left:2px solid black;
border-right:2px solid black;
}
.test{
display:flex;
flex-direction:row;
justify-content: space-around;
}
#title
{
text-align: center;
border-bottom: 1px solid black;
width: 250px;
margin: auto;
padding: 10px;
}
<div id="title">
Split Screen into 2 equaly halves
</div>
<div class="test">
<table id="myTableleft">
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
<tr >
<td class="row_top"></td>
<td class="row"></td>
<td class="row_top"></td>
</tr>
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
</table>
<table id="myTableright">
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
<tr >
<td class="row_top"></td>
<td class="row"></td>
<td class="row_top"></td>
</tr>
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
</table>
</div>

Table width is getting messed up

please help me with this:
the whole code is given below:
HTML
<div id="user_list">
<table id="grd" style="width: 100%;">
<thead>
<tr>
<td style="width: 15%;">Name</td>
<td style="width: 15%;">Age</td>
<td style="width: 35%;">Address</td>
<td style="width: 35%;">Office Address</td>
</tr>
</thead>
<tbody>
<tr>
<td>Agnib</td>
<td>25</td>
<td>jhjhhsdj</td>
<td>wyeruyweryuwe</td>
<tr>
</tbody>
</table>
</div>
Everything is fine till here. But, once I apply the below css, the width of the columns is getting messed up.
CSS
#grd thead, #grd tbody
{
display: block;
width: 100%;
}
Why the width is getting messed up and its becoming smaller in size. I have attached a sample image. Its happening exactly like the below:
#grd thead, #grd tbody
{
width: 100%;
}
<div id="user_list">
<table id="grd" style="width: 100%;">
<thead>
<tr>
<td style="width: 15%;">Name</td>
<td style="width: 15%;">Age</td>
<td style="width: 35%;">Address</td>
<td style="width: 35%;">Office Address</td>
</tr>
</thead>
<tbody>
<tr>
<td>Agnib</td>
<td>25</td>
<td>jhjhhsdj</td>
<td>wyeruyweryuwe</td>
<tr>
</tbody>
</table>
</div>
or
#grd thead, #grd tbody
{
width: 100%;
}
table
{
border:1px solid gray;
}
table tr td
{
border:1px solid gray;
}
DEMO FIDDLE
DEMO UPDATED
In css file just apply styles to complete table that is "#grd", no need to apply for individual elements like "#grd thead" and "#grd tbody".
Try this code once:
<table id="grd" style="width: 100%;" border=1>
<thead>
<tr><td></td>
<td style="width: 15%">Name</td>
<td style="width: 15%">Age</td>
<td style="width: 35%">Address</td>
<td style="width: 35%">Office Address</td>
</tr>
</thead>
<tbody>
<tr><td></td>
<td style="width: 15%">Agnib</td>
<td style="width: 15%">25</td>
<td style="width: 35%">jhjhhsdj</td>
<td style="width: 35%">wyeruyweryuwe</td>
<tr>
<tr><td></td>
<td style="width: 15%">Agnib</td>
<td style="width: 15%">25</td>
<td style="width: 35%">jhjhhsdj</td>
<td style="width: 35%">wyeruyweryuwe</td>
<tr>
</tbody>
</table>