Align table using rowspan and colspan - html

I want to create a table using rowspan and colspan and it is almost done. only the issue is in last tr. I don't know why but it is not picking the rowspan in last tr.
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
<table style="width:50%">
<tr>
<td height="75px">1</td>
<td colspan="4" height="75px">2</td>
</tr>
<tr>
<td rowspan="3" colspan="2">a</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td></td>
<td></td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
As you can see in result I want to merge the last blank td with a.
This is the result I want:

You should put the first set of cells with 3, 4, 5 after the td with the row+colspan.
And omit the empty cells from the last row.
Result:
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
th,
td {
padding: 5px;
text-align: left;
}
<table style="width:50%">
<tr>
<td width="20%" height="75px">1</td>
<td colspan="4" height="75px">2</td>
</tr>
<tr>
<td rowspan="3" colspan="2">a</td>
<td width="20%">3</td>
<td width="20%">4</td>
<td width="20%">5</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
Note that the second column never has content of its own, it is always part of a colspan set. That would normally make the second column collapse on most if not all browsers, and that is why the cell width is being set.

This is simply an error in counting. rowspan="3" means it spans three rows.
First row
<tr>
<td rowspan="3" colspan="2">a</td>
</tr>
Second row
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
Third row
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
Fourth row … so it doesn't span it.
<tr>
<td></td>
<td></td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
(You should omit the empty cells from the final row too).

try this:
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
<table style="width:50%">
<tr>
<td height="75px">1</td>
<td colspan="4" height="75px">2</td>
</tr>
<tr>
<td rowspan="3" colspan="2">a</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<tr style="visibility:hidden">
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</tr>
</table>

In order to keep the overlapping of the cells as you stated, you need to use an "invisible" row for reference. I'm not aware of any other way to achieve this, as colspan and rowspan need actual cells as a reference.
Here's an updated version of your example that works that way. Had to change the CSS a bit as well to prevent double borders around the hidden cells.
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
.reference,
.reference td {
padding: 0px 5px;
height: 0px;
line-height: 0px;
border: 0 none;
}
<table style="width:50%">
<tr>
<td height="75px">1</td>
<td colspan="4" height="75px">2</td>
</tr>
<tr>
<td rowspan="4" colspan="2">a</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr class="reference">
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>

Related

How to set separate html tables

I have html tables and I would like to add summary columns next to it.
I could create simple table, but I couldn't figure out how to set separate tables next to it.
My desired result is described below. If someone has idea, please let me know.
td {
border: solid 1px black;
padding:5px;
}
table {
border-collapse: collapse;
}
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
Here you go with a solution
td {
border: solid 1px black;
padding:5px;
}
table {
border-collapse: collapse;
}
.noborder {
border: none;
padding: 5px 8px;
}
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td class="noborder"></td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
<td class="noborder"></td>
<td>2</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
<td class="noborder"></td>
<td>2</td>
</tr>
</table>
Rather than creating another table I will suggest to use the no-border cell.
Hope this will help you
According to your requirement, you actually do not need another table, but in future if you want to use table side by side then you can use below solution.
td {
border: solid 1px black;
padding:5px;
}
table {
border-collapse: collapse;
float:left;
}
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
<table style="margin-left:20px">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>

Maintain same border-size when empty cell is removed in HTML

I got what I want as per the screenshot. However, the borders got attached to one another and got thicker. How do I maintain the border size?
This is actually what I'm planning to make it look like:
DEMO: https://jsfiddle.net/xnqh9d70/
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>Day</td>
<td>Sun.</td>
<td>Mon.</td>
<td>Tues.</td>
<td>Wed.</td>
<td>Thu.</td>
<td>Fri.</td>
<td>Sat.</td>
<td></td>
</tr>
<tr>
<td>Fare(s)</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td></td>
</tr>
<tr>
<td>Date(s)</td>
<td>9</td>
<td>8</td>
<td>7</td>
<td>6</td>
<td>5</td>
<td>4</td>
<td>3</td>
<td>2</td>
</tr>
</tbody>
</table>
CSS:
table {
border-collapse: separate;
empty-cells: hide;
border: 0;
border-color: #000000;
}
This is probably not the nicest solution, but what you can do is eliminate one of the two borders completely, only having borders on elements where it is needed then.
table {
border-collapse: separate;
empty-cells: hide;
border: 0;
border-color: #000000;
border-left: 1px solid black
}
tr {
border-bottom: none;
}
td {
border-left: none;
}
tr:last-of-type {
border-bottom: 1px solid black;
}
td:first-of-type {
border-right: 1px solid black;
}
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>Day</td>
<td>Sun.</td>
<td>Mon.</td>
<td>Tues.</td>
<td>Wed.</td>
<td>Thu.</td>
<td>Fri.</td>
<td>Sat.</td>
<td></td>
</tr>
<tr>
<td>Fare(s)</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td></td>
</tr>
<tr>
<td>Date(s)</td>
<td>9</td>
<td>8</td>
<td>7</td>
<td>6</td>
<td>5</td>
<td>4</td>
<td>3</td>
<td>2</td>
</tr>
</tbody>
</table>
The general way for creating borders between cells is by styling the td elements, and setting border-collapse: collapse; on the table.
I also added a class for your blank cells to remove the border on them.
table {
empty-cells: hide;
border-collapse: collapse;
border-color: #000000;
}
td {
border: 1px solid black;
}
.empty-cell {
border: none;
}
<table>
<tbody>
<tr>
<td>Day</td>
<td>Sun.</td>
<td>Mon.</td>
<td>Tues.</td>
<td>Wed.</td>
<td>Thu.</td>
<td>Fri.</td>
<td>Sat.</td>
<td class="empty-cell"></td>
</tr>
<tr>
<td>Fare(s)</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td class="empty-cell"></td>
</tr>
<tr>
<td>Date(s)</td>
<td>9</td>
<td>8</td>
<td>7</td>
<td>6</td>
<td>5</td>
<td>4</td>
<td>3</td>
<td>2</td>
</tr>
</tbody>
</table>
empty-cells: hide; is use to hide border
The empty-cells property sets whether or not to display borders on
empty cells in a table. Reference Here
you can apply css to td:empty for hide empty td
and for border-collapse: separate; case you need to add manually border-right to last td
table {
border-collapse: collapse;
border-color: #000000;
empty-cells: hide;
}
td {
border: 1px solid black;
}
td:empty {
border: 0px;
}
<table>
<tbody>
<tr>
<td>Day</td>
<td>Sun.</td>
<td>Mon.</td>
<td>Tues.</td>
<td>Wed.</td>
<td>Thu.</td>
<td>Fri.</td>
<td>Sat.</td>
<td></td>
</tr>
<tr>
<td>Fare(s)</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td></td>
</tr>
<tr>
<td>Date(s)</td>
<td>9</td>
<td>8</td>
<td>7</td>
<td>6</td>
<td>5</td>
<td>4</td>
<td>3</td>
<td>2</td>
</tr>
</tbody>
</table>

HTML create table

Im having trouble with getting table to look like I need.
I need table like this
But what i keep gettng is that ether the text of sundmused ja hadaabi are overlapping or thevahetused cell gets too wide so the alignment of thext gets weird. Can someone help me to create a table like this.
If you have too small table, you can use text-overflow: ellipsis; to add ... when there is not enough space for text.
table {
border: 1px solid black;
table-layout: fixed;
width: 30%;
border-collapse: collapse;
text-align: center;
margin-bottom: 10px;
}
table:nth-child(2) {
width: 100%;
}
table:nth-child(3) {
width: 50%;
}
table td {
overflow: hidden;
text-overflow: ellipsis;
border: 1px solid black;
}
.important {
background-color: yellow;
}
<table>
<tr>
<td colspan="3">vahetused</td>
<td>sundmused</td>
<td>hadaabid</td>
</tr>
<tr>
<td class="important" colspan="3">01.01.2017.00:00-02.01.2017.00:00</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>Kokku</td>
<td>1</td>
<td></td>
<td>1</td>
<td>1</td>
</tr>
</table>
<table>
<tr>
<td colspan="3">vahetused</td>
<td>sundmused</td>
<td>hadaabid</td>
</tr>
<tr>
<td class="important" colspan="3">01.01.2017.00:00-02.01.2017.00:00</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>Kokku</td>
<td>1</td>
<td></td>
<td>1</td>
<td>1</td>
</tr>
</table>
<table>
<tr>
<td colspan="3">vahetused</td>
<td>sundmused</td>
<td>hadaabid</td>
</tr>
<tr>
<td class="important" colspan="3">01.01.2017.00:00-02.01.2017.00:00</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>Kokku</td>
<td>1</td>
<td></td>
<td>1</td>
<td>1</td>
</tr>
</table>
This looks suspiciously like an assignment! But anyway:
I suspect your problem is that the first two cells 'span' three columns in the bottom row, and they don't do this automatically. Adding colspan="3" to the cells gives the result as below, which is guess is what you need.
td {
text-align: center;
}
<table border="1">
<tr>
<td colspan="3">vahetused</td>
<td>sundmused</td>
<td>hadaabid</td>
</tr>
<tr>
<td colspan="3">01.01.2017.00:00 - 02.01.2017 00:00</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>Kokku</td>
<td>1</td>
<td></td>
<td>1</td>
<td>1</td>
</tr>
</table>
Add your style to this code (padding, margin and color)
<table border style='text-align: center;border-collapse: collapse'>
<tr>
<td COLSPAN=3>vahetused</td>
<td>sundmused</td>
<td>hadaabid</td>
</tr>
<tr>
<td COLSPAN=3>01.01.2017.00:00 - 02.01.2017.00:00</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>Kokku</td>
<td>1</td>
<td> </td>
<td>1</td>
<td>1</td>
</tr>
</table>

Can I color table columns using CSS without coloring individual cells?

Is there a way to color spans of columns all the way down. See, starting example below:
<table border="1">
<tr>
<th>Motor</th>
<th colspan="3">Engine</th>
<th>Car</th>
<th colspan="2">Body</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<td>7</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>
And I am looking for a better way (less code, non-individual coloring) to color, for example, "Engine" and "Body" spans, including all the cells underneath them in #DDD
<style>
.color {
background-color: #DDD
}
</style>
<table border="1">
<tr>
<th>Motor</th>
<th colspan="3" class="color">Engine</th>
<th>Car</th>
<th colspan="2" class="color">Body</th>
</tr>
<tr>
<td>1</td>
<td class="color">2</td>
<td class="color">3</td>
<td class="color">4</td>
<td>5</td>
<td class="color">6</td>
<td class="color">7</td>
</tr>
<tr>
<td>7</td>
<td class="color">1</td>
<td class="color">2</td>
<td class="color">3</td>
<td>4</td>
<td class="color">5</td>
<td class="color">6</td>
</tr>
</table>
Yes, you can... using the <col> element:
.grey {
background-color: rgba(128,128,128,.25);
}
.red {
background-color: rgba(255,0,0,.25);
}
.blue {
background-color: rgba(0,0,255,.25);
}
<table>
<colgroup>
<col class="grey" />
<col class="red" span="3" />
<col class="blue" />
</colgroup>
<thead>
<tr>
<th>#</th>
<th colspan="3">color 1</th>
<th>color 2</th>
</tr>
</thead>
<tbody>
<tr>
<th>1</th>
<td>red</td>
<td>red</td>
<td>red</td>
<td>blue</td>
</tr>
<tr>
<th>2</th>
<td>red</td>
<td>red</td>
<td>red</td>
<td>blue</td>
</tr>
</tbody>
</table>
Note: You can use the span attribute to make the col definition apply to more than one column.
See also: <colgroup>
You can use the nth-child selector for that:
tr td:nth-child(2),
tr td:nth-child(3) {
background: #ccc;
}
<table>
<tr>
<th colspan="2">headline 1</th>
<th>headline 2</th>
</tr>
<tr>
<td>column 1</td>
<td>column 2</td>
<td>column 3</td>
</tr>
<tr>
<td>column 1</td>
<td>column 2</td>
<td>column 3</td>
</tr>
<tr>
<td>column 1</td>
<td>column 2</td>
<td>column 3</td>
</tr>
</table>
It is generally simplest to style cells (by column if desired), but columns can be styled, in different ways. One simple way is to wrap columns in a colgroup element and set styles on it. Example:
<style>
.x {
background-color: #DDD
}
</style>
<table border="1">
<col>
<colgroup class=x>
<col>
<col>
<col>
</colgroup>
<col>
<colgroup class=x>
<col>
<col>
</colgroup>
<tr>
<th>Motor</th>
<th colspan="3" class="color">Engine</th>
<th>Car</th>
<th colspan="2" class="color">Body</th>
</tr>
<tr>
<td>1</td>
<td class="color">2</td>
<td class="color">3</td>
<td class="color">4</td>
<td>5</td>
<td class="color">6</td>
<td class="color">7</td>
</tr>
<tr>
<td>7</td>
<td class="color">1</td>
<td class="color">2</td>
<td class="color">3</td>
<td>4</td>
<td class="color">5</td>
<td class="color">6</td>
</tr>
</table>
I would use the nth-child css pseudo-class for this:
tr td:nth-child(2), tr th:nth-child(2), tr td:nth-child(3), tr td:nth-child(4), tr th:nth-child(4), tr td:nth-child(6), tr td:nth-child(7){
background-color: #DDD;
}
tr td:nth-child(2),
tr th:nth-child(2),
tr td:nth-child(3),
tr td:nth-child(4),
tr th:nth-child(4),
tr td:nth-child(6),
tr td:nth-child(7) {
background-color: #DDD;
}
<table border="1">
<tr>
<th>Motor</th>
<th colspan="3">Engine</th>
<th>Car</th>
<th colspan="2">Body</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<td>7</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>
You can use CSS3:
http://jsfiddle.net/snuggles08/bm98g8v8/
<style>
.table td:nth-of-type(1) {
background: red;
}
.table td:nth-of-type(5) {
background: blue;
}
.table td:nth-of-type(3) {
background: green;
}
.table td:nth-of-type(7) {
background: lime;
}
.table td:nth-of-type(2) {
background: skyblue;
}
.table td:nth-of-type(4) {
background: darkred;
}
.table td:nth-of-type(6) {
background: navy;
}
</style>
Styled table:
<table border="1" class="table">
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<td>7</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</tbody>
</table>
<hr>Unstyled table:
<table border="1" class="table2">
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<td>7</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</tbody>
</table>
The following implement's the nth-child selector and should work...
<style type="text/css">
th:nth-child(2),
th:nth-child(4)
{
background-color: rgba(255, 0, 0, 1.0);
}
td:nth-child(2),
td:nth-child(3),
td:nth-child(4),
td:nth-child(6),
td:nth-child(7)
{
background-color: rgba(255, 0, 0, 0.5);
}
</style>
My version using nth-child expressions:
Using the CSS concept of cascade rules to first coloring the cells and then to uncolor the ones i want to be transparent. The first selector selects all the cells after the first one, and the second one selects the fifth cell to be transparent.
<style type="text/css">
/* colored */
td:nth-child(n+2) { background-color: #ddd }
/* uncolored */
td:nth-child(5) { background-color: transparent }
</style>
<table border="1">
<tr>
<th>Motor</th>
<th colspan="3">Engine</th>
<th>Car</th>
<th colspan="2">Body</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<td>7</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>
Check this interesting reference:
http://learn.shayhowe.com/advanced-html-css/complex-selectors/
This is an old question with a lot of great answers. Just wanted to add the -n and nth-last-child selectors that haven't yet been mentioned. They're helpful when applying CSS to multiple columns but may not know the number of columns prior to styling, or have multiple tables with varying widths.
/* Select the first two */
table tr td:nth-child(-n + 2) {
background-color: lightblue;
}
/* Select all but the first two */
table tr td:not(:nth-child(-n + 2)) {
background-color:lightgreen;
}
/* Select last two only */
table tr td:nth-last-child(-n + 2) {
background-color:mistyrose;
}
/* Select all but the last two */
table tr td:not(:nth-last-child(-n + 2)) {
background-color:yellow;
}
jsFiddle: https://jsfiddle.net/3rpf5oht/2/

How do I add definition to the cell borders in an HTML table?

This table:
<table border="1">
<tbody><tr>
<td></td>
<td>Grokkability</td>
<td>PIA Factor*</td>
<td>FancyPantsiness</td>
</tr>
<tr>
<td>XML</td>
<td>10</td>
<td>8</td>
<td>2</td>
</tr>
<tr>
<td>Code</td>
<td>10</td>
<td>5</td>
<td>4</td>
</tr>
<tr>
<td>Auto-Wiring</td>
<td>2</td>
<td>2</td>
<td>10</td>
</tr>
</tbody></table>
...looks as I want it to on jsfiddle (http://jsfiddle.net/clayshannon/9AX8H/), but on Code Project it has lost its cell formatting (http://www.codeproject.com/Tips/711127/Swapping-Out-Concrete-Implementations-of-Interface)
What must I do to force the cell boundaries to be visible?
You probably want to use css..
<table class="border">
<tbody><tr>
<th></th>
<th>Grokkability</th>
<th>PIA Factor*</th>
<th>FancyPantsiness</th>
</tr>
<tr>
<th>XML</th>
<td>10</td>
<td>8</td>
<td>2</td>
</tr>
<tr>
<th>Code</th>
<td>10</td>
<td>5</td>
<td>4</td>
</tr>
<tr>
<th>Auto-Wiring</th>
<td>2</td>
<td>2</td>
<td>10</td>
</tr>
</tbody></table>
Your css
.border {
border: solid 1pt;
border-collapse:collapse;
}
.border th{
border: 1px solid #000000;
padding: 4px;
background-color: #34B767;
}
.border td{
border: 1px solid;
padding: 4px;
}