How to align table header in html tables - html

I tried to create html tables with header.
I added 2 rows but I couldn't figure out how to align to header.
My desired result is like this.
below is my previous work.
What is wrong point? how can I fix it? if someone has opinion please let me know.
Thanks
td {
padding:5px;
border:solid black 1px;}
table{
border-collapse:collapse;
border:solid black 1px;}
<table>
<tr>
<th>header</th>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>

I hope this will help you:
td {
padding:5px;
border:solid black 1px;}
table{
border-collapse:collapse;
border:solid black 1px;}
<table>
<tr>
<th rowspan="2">header</th>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>

You have to use rowspan="2" since you want to merge 2 rows.
td {
padding: 5px;
border: solid black 1px;
}
table {
border-collapse: collapse;
border: solid black 1px;
}
<table>
<tr>
<th rowspan="2">header</th>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>11</td>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
</tr>
</table>

Please find attached the solution:
<table>
<tr>
<td rowspan="2"> Header</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</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>

How to make the background of the first row of the table different color from the table?

I have a table and I want to make the first row different background color for instance blue. I use table tr:first-child but it doesn't work. Can you help me? Thanks.
<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>
You can use tr:first-of-type. check updated snippet below.
table tr:first-of-type td {
background: blue;
}
<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>
you can use this:
table tr:first-child {
background: blue;
}
or you can add class to your first tr
.bg-blue {
background:blue;
}
<table>
<tr class="bg-blue">
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
You can use this snippet.
table tr:first-child td {
background: blue;
}
<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>
Hope this helps :)
As you are saying the first-child is not working, I suggest you check it's parent property if it has any property which is overriding your first-child property. Basically, it should work that way. Also, I would like to know which browser actually you are seeing the issue.
Try this:
table tr:first-child {
background:blue;
color:white;
}
<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>
You can add a comment with your issue if still it is not resolved with the solution.

Border 1 px for table td

How can I make border 1px for table td. I have a table and I want to make the border 1px. Thank you!
<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>
You can use like this
table { border-collapse: collapse;}
for table.
And give
td {
border: 1px solid red;
}
You can use border-collapse for table with css.
Try This:
td {
border: 1px solid #000;
}
td {
border: 1px solid #000;
}
<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>
Or
table {
border-collapse: collapse;
}
td {
border: 1px solid #000;
}
table {
border-collapse: collapse;
}
td {
border: 1px solid #000;
}
<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>
You can define a CSS style for td.
This will affect all tables in HTML page.
<style>
td {
border:1px solid black
}
</style>
<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>
Try like this :
table {
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
}
<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>

Assign CSS class to full <tr> but not one of the <td> tags

I have a css class on table <tr> tags but i want to stop it from being added on one <td>
is this possible? can i add a seperate class to the specific <td> to stop the <tr>'s class being added?
You could use the :not selector (which you can use to filter all bar the selector within the brackets):
table td:not(.this) {
background: red;
color: blue;
}
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td class="this">2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
Alternatively, you could use a class selector if you wanted to style it differently (rather than not at all):
table tr {
background: red;
color: blue;
}
.this {
background: green;
color: white;
}
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td class="this">2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
tr.MyClassName{
// styling goes here
}
That should only style the tr but not the td.

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;
}