I have 2 tables with same class elements like:
/* and css to make number increases in Numbers List col */
.myTbl tbody {
counter-reset: rowNumber;
}
.myTbl tbody tr {
counter-increment: rowNumber;
}
.myTbl tbody tr td:nth-child(1)::before {
content: counter(rowNumber);
min-width: 1em;
margin-right: 0.5em;
}
<div class="tableRow">
<table class="myTbl">
<thead>
<tr>
<td>Numbers List</td>
<td>Content</td>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>A</td>
</tr>
<tr>
<td></td>
<td>B</td>
</tr>
<tr>
<td></td>
<td>C</td>
</tr>
</tbody>
</div>
<div class="tableRow">
<table class="myTbl">
<thead>
<tr>
<td>C</th>
<td>D</th>
</tr>
</thead>
<tbody>
<tr>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</div>
I want the css only active in specific table like table 1 , but I'm having duplicate classes. How to do it?
And I cant affect the html table like change class or add id,... cause It's exported from other, only js or jquery to do it.
I tried adding ::nth-child(1) not working, is there a way same like eq() in js?
:first-child or nth-child(1) are actually work, you need to select from the parent
body .myTbl:first-child tbody {
counter-reset: rowNumber;
background: yellow
}
<table class="myTbl">
<thead>
<tr>
<td>Numbers List</td>
<td>Content</td>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td>B</td>
</tr>
</tbody>
<table class="myTbl">
<thead>
<tr>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<td>3</td>
<td>4</td>
</tr>
</tbody>
First, you had unclosed tags.
Second, Using pseudo selectors is the key. But there is a difference between :first-child & :first-of-type.
/* and css to make number increases in Numbers List col */
.myTbl tbody {
counter-reset: rowNumber;
}
.myTbl tbody tr {
counter-increment: rowNumber;
}
/* OR :first-child */
.myTbl:first-of-type tbody tr:first-of-type td::before {
content: '•';
min-width: 1em;
margin-right: 0.5em;
}
<table class="myTbl">
<thead>
<tr>
<td>Numbers List</td>
<td>Content</td>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>A</td>
</tr>
<tr>
<td></td>
<td>B</td>
</tr>
<tr>
<td></td>
<td>C</td>
</tr>
</tbody>
</table>
<table class="myTbl">
<thead>
<tr>
<td>C</td>
<td>D</td>
</tr>
</thead>
<tbody>
<tr>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
Related
So I want to get a table just like the picture down below:
I used first-child and last-child selectors but they seem like they select all the first and last elements IDK why, here's what I got:
here's what I've tried:
<style>
table {
border: 1px solid #c4dcf3;
border-collapse: collapse;
}
table :first-child, table :last-child {
background-color: green;
}
table tbody:nth-child(odd) {
background-color: red;
}
</style>
<table>
<thead>
<tr>
<th>Country</th>
<th>OrderID</th>
<th>Order Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>USA</td>
<td>1000</td>
<td>$1,300</td>
</tr>
<tr>
<td>USA</td>
<td>1001</td>
<td>$700</td>
</tr>
<tr>
<td>CA</td>
<td>1002</td>
<td>$2,000</td>
</tr>
<tr>
<td>CA</td>
<td>1003</td>
<td>$1,000</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td></td>
<td>$5,000</td>
</tr>
</tfoot>
</table>
:first-child and :last-child have to be placed on the child-elements. Since you threw it on tables, it looked for the first and last table in a set of table, which probably don't exist.
You can also target thead and tfoot for your desired styling, which might be more appropriate.
Same principle applies to the alternating highlighting. table > tbody > tr:nth-child(odd)
I added a working snippet.
<style>
table {
border: 1px solid #c4dcf3;
border-collapse: collapse;
}
thead,
tfoot {
background-color: green;
}
table > tbody > tr:nth-child(odd) {
background-color: red;
}
</style>
<table>
<thead>
<tr>
<th>Country</th>
<th>OrderID</th>
<th>Order Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>USA</td>
<td>1000</td>
<td>$1,300</td>
</tr>
<tr>
<td>USA</td>
<td>1001</td>
<td>$700</td>
</tr>
<tr>
<td>CA</td>
<td>1002</td>
<td>$2,000</td>
</tr>
<tr>
<td>CA</td>
<td>1003</td>
<td>$1,000</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td></td>
<td>$5,000</td>
</tr>
</tfoot>
</table>
Don't target 1st / last child when you can simply target thead and tfoot
Also your code doesn't work because it should be:
table>*:first-child ,
table>*:last-child {
background-color: green;
}
without space
You need to apply color on tr and make changes according to parent as you are using thead, tbody, tfoot. Your css not apply properly because you are using table tag there. table has 3 child thead, tbody, tfoot and each have their separate tr tags as first child and last child.
<style>
table {
border: 1px solid #c4dcf3;
border-collapse: collapse;
}
thead tr, tfoot tr{
background-color: green;
}
tbody tr:nth-child(odd) {
background-color: red;
}
</style>
<table>
<thead>
<tr>
<th>Country</th>
<th>OrderID</th>
<th>Order Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>USA</td>
<td>1000</td>
<td>$1,300</td>
</tr>
<tr>
<td>USA</td>
<td>1001</td>
<td>$700</td>
</tr>
<tr>
<td>CA</td>
<td>1002</td>
<td>$2,000</td>
</tr>
<tr>
<td>CA</td>
<td>1003</td>
<td>$1,000</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td></td>
<td>$5,000</td>
</tr>
</tfoot>
</table>
table {
border: 1px solid #c4dcf3;
border-collapse: collapse;
}
thead,
tfoot {
background-color: #4e81e7;
}
tr:nth-child(even) {background-color: #f2f2f2;}
<table>
<thead>
<tr>
<th>Country</th>
<th>OrderID</th>
<th>Order Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>USA</td>
<td>1000</td>
<td>$1,300</td>
</tr>
<tr>
<td>USA</td>
<td>1001</td>
<td>$700</td>
</tr>
<tr>
<td>CA</td>
<td>1002</td>
<td>$2,000</td>
</tr>
<tr>
<td>CA</td>
<td>1003</td>
<td>$1,000</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td></td>
<td>$5,000</td>
</tr>
</tfoot>
</table>
I am working on a table that is optimized with Bootstrap and I encounter the following problem I want column 1 both header and body to have font size 12px then column number 2 both header and body to have font size 21px.
In other words, I want to customize the font as I want for each column.
Code:
<table class="table">
<thead>
<tr>
<th style="font-size: 12px;">1</th>
<th style="font-size: 21px;">2</th>
<th style="font-size: 12px;">3</th>
<th style="font-size: 21px;">4</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
You need to use nth-child/nth-of-type and if you want a zebra style (column-based) you can use odd and even
/*one approach */
.table th:first-child,
.table td:first-child {
font-size: 21px
}
.table th:nth-child(2),
.table td:nth-child(2) {
font-size: 12px
}
/*second approach */
/* odd */
.table th:nth-child(2n+1),
.table td:nth-child(2n+1) {
background: red
}
/* even */
.table th:nth-child(2n),
.table td:nth-child(2n) {
background: lightblue
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" rel="stylesheet" />
<table class="table">
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
you can also use odd and even keywords with nth-child:
th:nth-child(odd),
td:nth-child(odd){
font-size: 12px;
}
th:nth-child(even),
td:nth-child(even){
font-size: 21px;
}
<table class="table">
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
Easily with CSS nth-child you can target what ever child you want, see below:
td:nth-child(1),
td:nth-child(3),
th:nth-child(1),
th:nth-child(3){
font-size: 12px
}
td:nth-child(2),
td:nth-child(4),
th:nth-child(2),
th:nth-child(4){
font-size: 21px
}
<table class="table">
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
Check MDN Web Docs for more about nth-child
I managed to create 'double row' for a table in Bootstrap:
<table class="table table-striped table-bordered">
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
<tr>
<td>A</td>
<td>A</td>
<td>A</td>
</tr>
<tr>
<td rowspan="2">B</td>
<td>B1</td>
<td>B1</td>
</tr>
<tr>
<td>B2</td>
<td>B2</td>
</tr>
</table>
Ideally I want it to look like this:
Playing around with colspan didn't really help, only broke things. I tried to set colspan of every row's first column to 2 and the one of B to 1, and have additional <td> for B1 and B2, didn't work.
Finally I've got a solution for you :
SNIPPET
.container {
background-color: #ccc;
}
thead{
background:#C9E4F4;
}
table,
th,
td {
text-align: center !important;
border: 1.5px solid #929292 !important;
}
#text1 {
background-color: #cac;
}
#text2 {
background-color: #cca;
}
#navigation {
background-color: #acc;
}
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.0/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered">
<tr>
<thead>
<th colspan="2">1</th>
<th>2</th>
<th>3</th>
</thead>
</tr>
<tr>
<td colspan="2">A</td>
<td>A</td>
<td>A</td>
</tr>
<tr>
<td rowspan="2">B</td>
<td>B1</td>
<td>B1</td>
<td>B1</td>
</tr>
<tr>
<td>B2</td>
<td>B2</td>
<td>B2</td>
</tr>
</table>
How can I achieve this kind of table:
Inside the Schedule column, there are sub columns (Jan, Feb and so on).
I tried <th></th> inside a <th></th>
But it is not working.
See my fiddle: http://jsfiddle.net/TAJzY/1/
The solution is colspan="" and rowspan="":
Use colspan="12" on the "Schedule/Milestone of Activities" cell.
And remove the 12 empty trailing cells in the same <tr> row.
Use rowspan="2" on the "Estimated Budget" cell.
And remove the single empty initial <th> cell from the <tr> below.
Don't forget to use explicit <thead>, <tbody>, and optional <tfoot> sections.
While you can use HTML tables without explicit sections, styling HTML tables with CSS is a lot easier and effective this way, and you can use techniques like thead { position: sticky; } for Excel-style "frozen" rows which are otherwise very difficult - or just tedious - to implement otherwise.
Step 1:
First, make a table, without any splitting/merging of cells, so you have something like this (click the "Run code snippet" button below to see the table):
table { border: 1px outset #bbb; }
table > * > tr > * { border: 1px inset #bbb; }
thead { background-color: #7ACABD; text-align: center; }
tbody { background-color: #e0fffa; }
tfoot { background-color: #39c4ae' }
<table>
<thead>
<tr>
<th>Estimated Budget</th>
<th>Schedule/Milestone of Activities</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<th></th>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>May</th>
<th>Jun</th>
<th>Jul</th>
<th>Aug</th>
<th>Sep</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
</tr>
</thead>
<tbody>
<tr>
<td>$123</td>
<td>j</td>
<td>f</td>
<td>m</td>
<td>a</td>
<td>m</td>
<td>j</td>
<td>j</td>
<td>a</td>
<td>s</td>
<td>o</td>
<td>n</td>
<td>d</td>
</tr>
<tr>
<td>$456</td>
<td>j</td>
<td>f</td>
<td>m</td>
<td>a</td>
<td>m</td>
<td>j</td>
<td>j</td>
<td>a</td>
<td>s</td>
<td>o</td>
<td>n</td>
<td>d</td>
</tr>
</tbody>
</table>
Step 2:
Then make the "Schedule/Milestone of Activities" cell span all 12 remaining columns (of the 13 total) with colspan="12" - which also means removing the empty trailing <th></th> elements in the same <tr> as those are now represented by the <th colspan="12"> cell:
table { border: 1px outset #bbb; }
table > * > tr > * { border: 1px inset #bbb; }
thead { background-color: #7ACABD; }
tbody { background-color: #e0fffa; }
tfoot { background-color: #39c4ae' }
<table>
<thead>
<tr>
<th>Estimated Budget</th>
<th colspan="12">Schedule/Milestone of Activities</th>
</tr>
<tr>
<th></th>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>May</th>
<th>Jun</th>
<th>Jul</th>
<th>Aug</th>
<th>Sep</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
</tr>
</thead>
<tbody>
<tr>
<td>$123</td>
<td>j</td>
<td>f</td>
<td>m</td>
<td>a</td>
<td>m</td>
<td>j</td>
<td>j</td>
<td>a</td>
<td>s</td>
<td>o</td>
<td>n</td>
<td>d</td>
</tr>
<tr>
<td>$456</td>
<td>j</td>
<td>f</td>
<td>m</td>
<td>a</td>
<td>m</td>
<td>j</td>
<td>j</td>
<td>a</td>
<td>s</td>
<td>o</td>
<td>n</td>
<td>d</td>
</tr>
</tbody>
</table>
Step 3:
To make the "Estimated Budget" cell span those 2 rows in <thead> add rowspan="2" and also remove the empty initial <th></th> in the second <tr> (as that empty <th> cell's "slot" is now taken by the <th rowspan="2"> from the previous row).
Like so:
table { border: 1px outset #bbb; }
table > * > tr > * { border: 1px inset #bbb; }
thead { background-color: #7ACABD; }
tbody { background-color: #e0fffa; }
tfoot { background-color: #39c4ae; }
/* Right-align budget numbers in the first column: */
table > tbody > tr > td:first-child { text-align: right; }
<table>
<thead>
<tr>
<th rowspan="2">Estimated Budget</th>
<th colspan="12">Schedule/Milestone of Activities</th>
</tr>
<tr>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>May</th>
<th>Jun</th>
<th>Jul</th>
<th>Aug</th>
<th>Sep</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
</tr>
</thead>
<tbody>
<tr>
<td>$123</td>
<td>j</td>
<td>f</td>
<td>m</td>
<td>a</td>
<td>m</td>
<td>j</td>
<td>j</td>
<td>a</td>
<td>s</td>
<td>o</td>
<td>n</td>
<td>d</td>
</tr>
<tr>
<td>$456</td>
<td>j</td>
<td>f</td>
<td>m</td>
<td>a</td>
<td>m</td>
<td>j</td>
<td>j</td>
<td>a</td>
<td>s</td>
<td>o</td>
<td>n</td>
<td>d</td>
</tr>
</tbody>
</table>
You use the colspan attribute on the Schedule th. It would make it to span over many columns.
Using rowspan for the Budget th will have the same effect on rows.
Try this (note use of colspan):
<table border="1">
<tr>
<td>budget</td>
<td colspan="3">header</td>
</tr>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
</tr>
</table>
You can use my http://blocknote.net editor if you need more complex table layout. Table editor there is pretty convenient.
<table>
<tr>
<td>foo</td>
<td colspan="3">Header</td>
</tr>
<tr>
<td></td>
<td>Content</td>
...
</tr>
<tr>
<td>foo1</td>
<td>foo2</td>
...
</tr>
</table>
How can I make the table header appear on the left side of the table as a column instead on the top as a row? I have this markup:
<table>
<thead>
<tr>
<th>a</th>
<th>b</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
</tbody>
</table>
Just use <th> as the first element in the row. Then add the scope attribute, which has no visual impact, but you could use it e.g. in CSS.
<table>
<tbody>
<tr>
<th scope="row">A</th>
<td>b</td>
</tr>
<tr>
<th scope="row">C</th>
<td>d</td>
</tr>
</tbody>
</table>
See also http://www.w3.org/TR/WCAG20-TECHS/H63
How's this?
Example
CSS
thead {
float: left;
}
thead th {
display: block;
}
tbody {
float: right;
}
jsFiddle.
Update
Well, the 1, 2 should also be as column, obviously.
jsFiddle.
It also looks like IE baulks at this. You may have to trade semantic-ness for cross browser compatibility.
You can see the result here. You mean like this?
<table border="1">
<thead>
<tr>
<th></th>
<th colspan="2">Letters</th>
</tr>
<tr>
<th></th>
<th>a</th>
<th>b</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="3">Numbers</td>
<td>1</td>
<td>4</td>
</tr>
<tr>
<td>2</td>
<td>5</td>
</tr>
<tr>
<td>3</td>
<td>6</td>
</tr>
</tbody>
</table>
You usually use rowspan and colspan for cells spanning multiple columns/rows.
I needed something a little different, but the answers by #alex and #marion got me started in the right direction. The problem was that when you needed many items in the table, the "columns" started stacking funny on smaller screens.
Thanks to Serge for his answer here that led me in this solution. This solution allows for scrolling horizontally and doesn't stack funny regardless of the size of the screen/window. I tested it in Chrome, Firefox, Opera, Edge, and IE11. Here's the fiddle with the correct alignment for the new "rows" and "columns": https://jsfiddle.net/berrym/6r3zvaef/21/
And just in case it disappears from JSFiddle:
<style>
table{
display:block;
white-space:nowrap;
width:100%;
}
td, th {
border-bottom: 1px solid red;
border-collapse: collapse;
}
thead {
float: left;
background: yellow;
width: 10%;
}
thead tr {
width:100%;
float:left;
}
thead th {
display: block;
}
tbody {
float: left;
width: 90%;
}
tbody tr {
display: inline-block;
}
tbody td {
float:left;
width:100%;
}
</style>
<table>
<thead>
<tr>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
</tbody>
</table>
This worked perfectly for me : (inspired from the first answer)
Example here
html :
<table>
<thead>
<tr>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<td>a1</td>
<td>b1</td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
</tr>
</tbody>
</table>
css :
table, td, th {
border: 1px solid red;
}
thead {
float: left;
}
thead th {
display: block;
background: yellow;
}
tbody {
float: left;
}
tbody tr {
display: block;
float: left;
}
tbody td {
display: block;
}
If you use bootstrap, you can achieve this easily with the table-reflow style: http://v4-alpha.getbootstrap.com/content/tables/#reflow