Vertical alignment and distribution of rows in table - html

The second column spans 2 rows. I want the first column NOT to be divided by 50% for each row. Row2 should start right under the content of Row1.
<table border="1" style="width:850px">
<tr>
<td style="width: 50%; vertical-align: top;">1.Row Cell 1</td>
<td rowspan="2" style="height:800px">1-2 Row Cell 2</td>
</tr>
<tr style="vertical-align:top">
<td style="vertical-align:top">2.Row Cell 1</td>
</tr>
</table>
OK, seems that this is related to Internet Explorer 11, but there must be a way to accomplish this!?

So there is your solution in the snippet below :
table , td, th {
border: 1px solid #595959;
border-collapse: collapse;
}
td, th {
padding: 3px;
width: 250px;
height: 150px;
}
th {
background: #f0e6cc;
}
.even {
background: #fbf8f0;
}
.odd {
background: #fefcf9;
}
<table>
<tbody>
<tr>
<td style="height:1em">This is a test thank you for your attention</td>
<td rowspan="2"></td>
</tr>
<tr>
<td></td>
</tr>
</tbody>
</table>
Hope it help.

Adding a height: 1em; seemed to work - like this for the first cell:
<td style="width: 50%; vertical-align: top; height: 1em;">

Related

How can I build the table with rowspan

I have to make a table, but I don't know how to build it, I've try a lot, but I can't.
I want to make a table like this -->
But I can only do this -->
I want the f cell to merge the two cells down, in the left middle of the b cell and the g cell.
How can I solve ? THANKS !!!
Here is my code:
table, td {
border:2px solid black;
border-collapse: collapse;
}
td{
height: 10px;
width: 200px;
vertical-align: top;
line-height: 25px;
}
td.a{
text-align: center;
vertical-align: top;
}
td.b{
text-align: left;
vertical-align: bottom;
}
td.c{
text-align: left;
vertical-align: middle
}
<table align="center">
<tr>
<td>a</td>
<td class="a" rowspan="3">b</td>
<td>c</td>
</tr>
<tr>
<td>d</td>
<td class="c" rowspan="3">e</td>
</tr>
<tr>
<td class="b" rowspan="2">f</td>
</tr>
<tr>
<td class="a" rowspan="2">g</td>
</tr>
<tr>
<td>h</td>
<td>i</td>
</tr>
</table>
Better to use grid but if you really wan to use table, you will need to fix a height to your tr, otherwise you wont see any difference:
tr{
height: 25px;
}
And if you really want to that the height fit with your image, you can double height on the fourth row:
tr:nth-child(4){
height: 50px;
}
DEMO
table, td {
border:2px solid black;
border-collapse: collapse;
}
tr{
height: 25px;
}
tr:nth-child(4){
height: 50px;
}
td{
height: 10px;
width: 200px;
vertical-align: top;
line-height: 25px;
}
td.a{
text-align: center;
vertical-align: top;
}
td.b{
text-align: left;
vertical-align: bottom;
}
td.c{
text-align: left;
vertical-align: middle
}
<html>
<head>
<title>Table</title>
<style> </style>
</head>
<body>
<table align="center">
<tbody>
<tr>
<td>a</td>
<td class="a" rowspan="3">b</td>
<td>c</td>
</tr>
<tr>
<td>d</td>
<td class="c" rowspan="3">e</td>
</tr>
<tr>
<td class="b" rowspan="2">f</td>
</tr>
<tr>
<td class="a" rowspan="2">g</td>
</tr>
<tr>
<td>h</td>
<td>i</td>
</tr>
<tbody>
</table>
</body>
</html>
I recommend you to use CSS-Grid to specify the rows and columns and then use grid-row property on the the item you want to span to other row it will make your task easier. Make sure you have enough rows available in your table to span one row item to other line.

Automatically make the td in table equal depend on number of td

I have several tables. Some have 4 td in a row, some have 3 td in a row. Is there any smart way to make those td equal depend on the number of td in a row, since I don't want to write a specific css for each case.
For example, if a row have 4 td, each td's width should be 25% and 33.33% if a row have 3 td.
I'm using scss.
Edit: I'm also looking for a way that also meet this condition too: in a table, there is two rows, the first row has 2 td, the second row has 3 td and this table still meet my requirement.
For example, in this case, I want the td in the first row (which has 1 td) will have 100% width, not 25%
table {
table-layout: fixed;
width: 100%;
text-align: center;
border-collapse: collapse;
}
td, th {
border: 1px solid #dddddd;
padding: 8px;
}
<table>
<tr>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</table>
Thank you.
Use Below CSS
table {
table-layout: fixed;
width: 100%;
text-align: center;
border-collapse: collapse;
}
td, th {
border: 1px solid #dddddd;
padding: 8px;
}
Check Example here:- https://codepen.io/rvtech/pen/yLyewjG
You just need to use a fixed table layout and set the width of the table. I have included a few examples below.
.myTable, .subTable {
width: 200px;
table-layout: fixed;
}
td {
border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<h1>1. table in a table(use a sub table that uses the same css)</h1>
<table class="myTable">
<tr>
<td>apple pie</td>
<td>orange tart</td>
<td>blueberry crumble</td>
</tr>
<tr>
<table class="subTable">
<tr>
<td>apple pie</td>
<td>orange chocolate</td>
</tr>
</table>
</tr>
<tr>
<table class="subTable">
<tr>
<td>meat pie</td>
<td>apple crumble</td>
<td>cranberry jam</td>
</tr>
</table>
</tr>
</table>
<hr>
<h1>2. use colspans that you will have to set via hand or javascript</h1>
<table class="myTable">
<tr>
<td>pumpkin pie</td>
<td>banana tart</td>
</tr>
<tr>
<td colspan="2">
blueberry pie
</td>
</tr>
</table>
<hr>

Validator gives table errors

I have to make a table like
and I have this code so far:
table, td, tr {
border: 1px solid black;
}
td {
width: 50px;
height: 50px;
}
td[rowspan="2"] {
height: 100px;
}
td[colspan="2"] {
width: 100px;
}
<div>
<table>
<tr>
<td>a</td>
<td colspan="2">b</td>
</tr>
<tr>
<td rowspan="2">c</td>
<td colspan="2" rowspan="2">
<table>
<tr>
<td colspan="2" rowspan="2">d</td>
</tr>
<tr></tr>
</table>
</td>
</tr>
<tr></tr>
</table>
</div>
The validator is giving me
and I don't know how to fix them.
I need no errors from the validator, as
it has to be acceptable by specification.
rowspans and colspans are only used if a cell should span 2 other cells horizontally or vertically, which isn't the case in your example. They are not there to define width or height.
So delete those and use classes instead to define the properties you want:
Apart from that, delete those empty tr elements you have in there - they make no sense without tds in them. ALso the nested table with only one cell in it is rather strange (you could just fill that cell with content), but maybe there's a reason for that which you didn't tell us.
table, td, tr {
border: 1px solid black;
}
td {
width: 50px;
height: 50px;
}
td.b {
height: 100px;
}
td.a {
width: 100px;
}
<div>
<table>
<tr>
<td>a</td>
<td class="a">b</td>
</tr>
<tr>
<td class="b">c</td>
<td class="a b" >
<table>
<tr>
<td class="a b">d</td>
</tr>
</table>
</td>
</tr>
</table>
</div>

HTML table with mixed rowspan

I'm trying to use HTML to construct a table with three rows (1-3) and three columns (A-C) forming nine "virtual cells" (A1, B1, C1, A2, B2, C2, A3, B3, C3) and apply row spanning so that:
cell A1 span all three rows (covering A2 and A3)
cell C1 span two rows (covering C2)
cell B2 span two rows (covering B3)
This is what I want to see:
This is the HTML I thought would give me that:
<html>
<head>
<style>
table { border-collapse: collapse; }
td { border: 1px solid black; padding: 1em; vertical-align: top; }
</style>
</head>
<body>
<table>
<tr><td rowspan="3">A1</td><td>B1</td><td rowspan="2">C1</td></tr>
<tr><td rowspan="2">B2</td></tr>
<tr><td>C3</td></tr>
</table>
</body>
</html>
But that gives me:
What is the correct way to get what I want? Or is it not possible?
This is for use in technical documentation. It is not a layout issue, the content is semantically a table.
In order to prevent the rows collapsing without the need for additional markup, you can attach a phantom cell to each row with tr::after set to display: table-cell with your cell padding on top and bottom and a unicode blank space:
tr::after {
content: '\00a0';
display: table-cell;
padding: 1em 0;
}
Gives you the correct result:
It's worth noting that the phantom cell will create a slight gap to the right like this:
Full snippet
table {
border-collapse: collapse;
}
td {
border: 1px solid black;
padding: 1em;
vertical-align: top;
}
tr:after {
content: '\00a0';
display: table-cell;
padding: 1em 0;
}
<table>
<tr>
<td rowspan="3">A1</td>
<td>B1</td>
<td rowspan="2">C1</td>
</tr>
<tr>
<td rowspan="2">B2</td>
</tr>
<tr>
<td>C3</td>
</tr>
</table>
Here's a solution without having to know the table height up front, using hidden table cells, like in Align table using rowspan and colspan (as I said, it's basically a duplicate, just another layout):
<html>
<head>
<style>
table { border-collapse: collapse; }
td { border: 1px solid black; padding: 1em; vertical-align: top; }
td.hidden { visibility: hidden; padding: 1em 0; border: 0 none; }
</style>
</head>
<body>
<table>
<tr><td rowspan="3">A1</td><td>B1</td><td rowspan="2">C1</td><td class="hidden">‌</td></tr>
<tr><td rowspan="2">B2</td><td class="hidden">‌</td></tr>
<tr><td>C3</td><td class="hidden">‌</td></tr>
</table>
</body>
</html>
Why not just setting a height to the tr cause it is a table the height will adjust anyways if there is more content inside the row.
something like so:
table {
border-collapse: collapse;
}
tr {
height: 30px;
}
td {
border: 1px solid black;
padding: 1em;
vertical-align: top;
}
<table>
<tr>
<td rowspan="3">A1</td>
<td>B1</td>
<td rowspan="2">C1</td>
</tr>
<tr>
<td rowspan="2">B2</td>
</tr>
<tr>
<td>C3</td>
</tr>
</table>
Otherwise,
<!DOCTYPE html>
<html>
<head>
<style>
table { border-collapse: collapse; }
td{border: 1px solid black; padding: 1em; vertical-align: top; }
</style>
</head>
<body>
<table>
<tr>
<td rowspan="3">A1</td>
<td>B1</td>
<td rowspan="2">C1</td>
</tr>
<tr>
<td rowspan="2">B2</td>
</tr>
<tr>
<td rowspan="2">C3</td>
</tr>
</table>
</body>
</html>
You could hack it like this:
<html>
<head>
<style>
table { border-collapse: collapse; }
td { border: 1px solid black; padding: 1em; vertical-align: top; }
</style>
</head>
<body>
<table>
<tr>
<td style="width:0px;padding:0;border:0"></td>
<td rowspan="3">A1</td>
<td>B1</td>
<td rowspan="2">C1</td>
</tr>
<tr>
<td style="width:0px;padding:0;border:0;height:50px"></td>
<td rowspan="2">B2</td>
</tr>
<tr>
<td style="width:0px;padding:0;border:0"></td>
<td>C3</td>
</tr>
</table>
</body>
</html>
... but I would recommend to use another structure instead of tables, since it doesn't have a lot in common with table, besides the columns.
It's depend the height of your table.
http://codepen.io/anon/pen/jBOgpx
<table>
<tr>
<td rowspan="3">A1</td>
<td>B1</td>
<td rowspan="2">C1</td>
</tr>
<tr>
<td rowspan="2" style="height:65px">B2</td>
</tr>
<tr>
<td>C3</td>
</tr>
</table>

Rowspan cell not the same size

I have a simple table with rowspan . I need to that cell number1, cell number2 and cell number6 to be the same size .
How can I achieve that please
<table>
<tr>
<td>number1</td>
<td>number3</td>
</tr>
<tr>
<td rowspan="2">number2</td>
<td>number4</td>
</tr>
<tr>
<td>number5</td>
</tr>
<tr>
<td>number6</td>
<td>number7</td>
</tr>
</table>
Like this you mean?
This solution uses the existing markup, no need for any "empty" rows.
table {
border-collapse: collapse;
}
td {
border: 1px solid #ddd;
padding: 10px;
}
tr:nth-child(1) td:first-child,
tr:nth-child(2) td:first-child,
tr:nth-child(4) td:first-child {
height: 60px;
}
<table>
<tr>
<td>number1</td>
<td>number3</td>
</tr>
<tr>
<td rowspan="2">number2</td>
<td>number4</td>
</tr>
<tr>
<td>number5</td>
</tr>
<tr>
<td>number6</td>
<td>number7</td>
</tr>
</table>
The trick is to add a single cell row under each row that has a rowspan. So basically, you need to accommodate room for any span in a table, otherwise you'll get extra cells protruding from the table. Easiest way to remember is to:
Make one less cell for every unit of the (rowspan -1) on each proceeding row.
Make one less cell for every unit of the (colspan -1) on each proceeding column.
table {
width: 105px;
table-layout: fixed;
}
td {
width: 50px;
text-align: center;
}
tr {
height: 50px;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Col1, 2, & 6</title>
<style>
table {
1px solid grey
}
td {
border: 1px solid black
}
</style>
</head>
<body>
<table>
<tr>
<td rowspan="2">1</td>
<td rowspan="2">2</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2">3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
</tr>
<tr>
<td rowspan="2">6</td>
<td rowspan="2">7</td>
</tr>
<tr></tr>
</table>
</body>
</html>