select nth column in a table - html

I wanted to change the width of the third td in the below table
#mytable table > tr > td:nth-child(3) {
width: 600px !important;
}
<table id="mytable">
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
Can you please help me out

Two errors:
Your selector: #mytable is already a table and has no table child,
so use only #mytable
The > in table > tr will not work since the browser adds tbody by default,
therefore tr is an immediate child of tbody
#mytable{
width:100%;
}
#mytable td:nth-child(3) {
width: 600px; background:#eee;
}
<table id="mytable">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>

#mytable table means that the CSS is trying to find an element with the ID of mytable and within that element looking for a <table>. You also do not need to be that specific; the following would achieve your goals:
#mytable tr td:nth-child(3) { width: 600px; }

This should be Visually more clear to you
Below is Snippet
.tblcommission{
border:1px solid black;
}
.tblcommission thead tr td:nth-child(2){
border:1px solid red;
background-color:red;
color:white;
}
.tblcommission tbody tr td:nth-child(2){
border:1px solid black;
background-color:blue;
color:white;
width:200px;
}
<table class="tblcommission">
<thead>
<tr>
<td>h1</td>
<td>h2</td>
<td>h3</td>
<td>h4</td>
</tr>
</thead>
<tbody>
<tr>
<td>111111</td>
<td>222222</td>
<td>333333</td>
<td>444444</td>
</tr>
<tr>
<td>111111</td>
<td>222222</td>
<td>333333</td>
<td>444444</td>
</tr>
<tr>
<td>111111</td>
<td>222222</td>
<td>333333</td>
<td>444444</td>
</tr>
</tbody>
</table>

Related

CSS HTML tfoot, tbody, thead complex styling

Can I use <thead> inside <tbody> and <tfoot> apart from main <thead> of <table>?
I would like to style thead inside main components differently, but I don't understand, why higher specifity some of them does not react and some styles works on every <tr> while I specified it only to <tbody>.
I would like to try do it without classes.
Here is simple example:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
body {
background-color: black;
text-align: center;
color: black;
}
table > thead {background-color: pink;} /*0002 - to main thead*/
table > tbody > thead > tr > th {background-color: skyblue; } /*0005 - to "Final students marks" and "Avarage marks in groups"*/
tbody > tr:nth-child(even) {background: #CCC;} /*0011*/
tbody > tr:nth-child(odd) {background: #FFF;} /* why it styling tfoot?? */
tfoot tr:nth-child(even) {background: sandybrown !important; /*why not work???*/ }
tfoot tr:nth-child(odd) {background: sandybrown !important;}
</style>
</head>
<body>
<table>
<thead>
<tr><th>Number</th><th>Name</th><th>Surname</th><th>Group</th><th>Mark</th></tr>
</thead>
<tbody>
<thead><tr><th colspan="5">Final students marks</th></tr></thead>
<tr><td>1</td><td>Adam</td><td>Abacki</td><td>A</td><td>4</td></tr>
<tr><td>2</td><td>Ewa</td><td>Babacka</td><td>B</td><td>5</td></tr>
<tr><td>3</td><td>Edward</td><td>Cabacki</td><td>A</td><td>3</td></tr>
<tr><td>4</td><td>Tomasz</td><td>Dadacki</td><td>A</td><td>4</td></tr>
<tr><td>5</td><td>Anna</td><td>Kowalska</td><td>B</td><td>3</td></tr>
<tr><td>6</td><td>Marek</td><td>Zawadzki</td><td>A</td><td>5</td></tr>
</tbody>
<tfoot>
<thead><tr><th colspan="5">Average marks in groups</th></tr></thead>
<tr><td>1</td><td colspan="2"></td><td>A</td><td>4</td></tr>
<tr><td>2</td><td colspan="2"></td><td>B</td><td>4</td></tr>
</tfoot>
</table>
</body>
</html>
This is the final effect, which I would like to have:
<thead>
Permitted content: Zero or more <tr> elements.
Permitted parents: A <table> element.
So your table structure should be:
table
thead
tr
th
tbody
tr
td
tfoot
tr
td
body {
text-align: center;
}
table {
width: 100%;
border: 1px solid black;
border-spacing: 0;
}
table>thead {
background-color: pink;
font-weight: bold;
}
table>tbody>tr:first-child>td,
table>tfoot>tr:first-child>td {
font-weight: bold;
background-color: skyblue;
}
tbody>tr:nth-child(odd) {
background: #CCC;
}
tfoot>tr {
background: sandybrown;
}
tbody>tr>td:first-child,
tfoot>tr>td:first-child {
background: yellow;
}
<body>
<table>
<thead>
<tr>
<th>Number</th>
<th>Name</th>
<th>Surname</th>
<th>Group</th>
<th>Mark</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5">Final students marks</td>
</tr>
<tr>
<td>1</td>
<td>Adam</td>
<td>Abacki</td>
<td>A</td>
<td>4</td>
</tr>
<tr>
<td>2</td>
<td>Ewa</td>
<td>Babacka</td>
<td>B</td>
<td>5</td>
</tr>
<tr>
<td>3</td>
<td>Edward</td>
<td>Cabacki</td>
<td>A</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>Tomasz</td>
<td>Dadacki</td>
<td>A</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>Anna</td>
<td>Kowalska</td>
<td>B</td>
<td>3</td>
</tr>
<tr>
<td>6</td>
<td>Marek</td>
<td>Zawadzki</td>
<td>A</td>
<td>5</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5">Average marks in groups</td>
</tr>
<tr>
<td>1</td>
<td colspan="2"></td>
<td>A</td>
<td>4</td>
</tr>
<tr>
<td>2</td>
<td colspan="2"></td>
<td>B</td>
<td>4</td>
</tr>
</tfoot>
</table>
thead Isn't valid inside of tbody (yes they're just tags but tables are from a different time with different browser rules). You CAN use multiple tbody tags to group things and css:first-child to color rows correctly.
body {
background-color: black;
text-align: center;
color: black;
}
thead {
background-color: pink;
}
tbody > tr:nth-child(even) {
background: #CCC;
}
tbody > tr:nth-child(odd) {
background: #FFF;
}
/* remove important or other colors won't work */
/* just place AFTER other definition in CSS */
tbody:nth-child(3) tr {
background: sandybrown;
}
/* place this after other definition to make everything blue */
tbody>tr:first-child {
background: skyblue;
}
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Number</th>
<th>Name</th>
<th>Surname</th>
<th>Group</th>
<th>Mark</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5">Final students marks</td>
</tr>
<tr>
<td>1</td>
<td>Adam</td>
<td>Abacki</td>
<td>A</td>
<td>4</td>
</tr>
<tr>
<td>2</td>
<td>Ewa</td>
<td>Babacka</td>
<td>B</td>
<td>5</td>
</tr>
<tr>
<td>3</td>
<td>Edward</td>
<td>Cabacki</td>
<td>A</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>Tomasz</td>
<td>Dadacki</td>
<td>A</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>Anna</td>
<td>Kowalska</td>
<td>B</td>
<td>3</td>
</tr>
<tr>
<td>6</td>
<td>Marek</td>
<td>Zawadzki</td>
<td>A</td>
<td>5</td>
</tr>
</tbody>
<tbody>
<tr>
<td colspan="5">Average marks in groups</td>
</tr>
<tr>
<td>1</td>
<td colspan="2"></td>
<td>A</td>
<td>4</td>
</tr>
<tr>
<td>2</td>
<td colspan="2"></td>
<td>B</td>
<td>4</td>
</tr>
</tbody>
</table>
</body>
</html>
Remove thead inside tfoot, tbody and use only tr and th and add pink color background
Note : thead and tfoot elements can be used once in a table.
tbody > tr > th, tfoot > tr > th {background-color: skyblue !important; } /*0005 - to "Final students marks" and "Avarage marks in groups"*/
tbody > tr > td:first-child, tfoot > tr > td:first-child {
background-color: yellow
}
code sample for reference
body {
background-color: black;
text-align: center;
color: black;
}
table > thead {background-color: pink;} /*0002 - to main thead*/
tbody > tr:nth-child(even) {background: #CCC;} /*0011*/
tbody > tr:nth-child(odd) {background: #FFF;} /* why it styling tfoot?? */
tfoot tr:nth-child(even) {background: sandybrown !important; /*why not work???*/ }
tfoot tr:nth-child(odd) {background: sandybrown !important;}
tbody > tr > th, tfoot > tr > th {background-color: skyblue !important; } /*0005 - to "Final students marks" and "Avarage marks in groups"*/
tbody > tr > td:first-child, tfoot > tr > td:first-child {
background-color: yellow
}
<head>
<title>Page Title</title>
</head>
<body>
<table>
<thead>
<tr><th>Number</th><th>Name</th><th>Surname</th><th>Group</th><th>Mark</th></tr>
</thead>
<tbody>
<tr><th colspan="5">Final students marks</th></tr>
<tr><td>1</td><td>Adam</td><td>Abacki</td><td>A</td><td>4</td></tr>
<tr><td>2</td><td>Ewa</td><td>Babacka</td><td>B</td><td>5</td></tr>
<tr><td>3</td><td>Edward</td><td>Cabacki</td><td>A</td><td>3</td></tr>
<tr><td>4</td><td>Tomasz</td><td>Dadacki</td><td>A</td><td>4</td></tr>
<tr><td>5</td><td>Anna</td><td>Kowalska</td><td>B</td><td>3</td></tr>
<tr><td>6</td><td>Marek</td><td>Zawadzki</td><td>A</td><td>5</td></tr>
</tbody>
<tfoot>
<tr><th colspan="5">Average marks in groups</th></tr>
<tr><td>1</td><td colspan="2"></td><td>A</td><td>4</td></tr>
<tr><td>2</td><td colspan="2"></td><td>B</td><td>4</td></tr>
</tfoot>
</table>
</body>
codepn - https://codepen.io/nagasai/pen/jQwWPm

Give on different cells colors

How can I have different colors on each cell. What I did is only to make the whole red, just to the first 3 cells only yellow blue and red. How can it be this? I should refer to specific <td>? I see this question, but it wasn't exactly what I was searching.
body {
background: #000;
}
#wrap {
margin: 0 auto;
/* margin 0 auto will center that box in your document */
width: 780px;
/*size of your box*/
background: #000;
text-align: center;
/* everything will be written in that box will be centered horizontaly*/
}
td:hover {
background-color: #ff0000;
color: #000000;
}
<div id="wrap">
<table width="780">
<tr>
<td align="center">
<table border=1>
<tbody>
<!-- Results table headers -->
<tr>
<th>Messages Per Month</th>
<th>1 Month Pricing</th>
<th>3 Month Pricing</th>
<th>12 Month Pricing</th>
</tr>
<tr>
<td>500</td>
<td>$14.95/Month</td>
<td>$12.95/Month</td>
<td>$9.95/Month</td>
</tr>
<tr>
<td>1,000</td>
<td>$24.95/Month</td>
<td>$20.95/Month</td>
<td>$17.95/Month</td>
</tr>
<tr>
<td>1,500</td>
<td>$37.95/Month</td>
<td>$31.95/Month</td>
<td>$26.95/Month</td>
</tr>
<tr>
<td>2,000</td>
<td>$49.95/Month</td>
<td>$41.95/Month</td>
<td>$35.95/Month</td>
</tr>
<tr>
<td>2,500</td>
<td>$62.95/Month</td>
<td>$52.95/Month</td>
<td>$44.95/Month</td>
</tr>
<tr>
<td>5,000</td>
<td>$119.95/Month</td>
<td>Not Available</td>
<td>Not Available</td>
</tr>
<tr>
<td>7,500</td>
<td>$179.95/Month</td>
<td>Not Available</td>
<td>Not Available</td>
</tr>
<tr>
<td>10,000</td>
<td>$219.95/Month</td>
<td>Not Available</td>
<td>Not Available</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</div>
Try using nth-child on the elements, here is a quick reference to all kinds of selections.
td:nth-child(odd) {
color: green;
}
td:nth-child(even) {
color: red;
}
td {
border: 1px solid gray;
}
<table>
<tr>
<td>2,500</td>
<td>$62.95/Month</td>
<td>$41.95/Month</td>
<td>$35.95/Month</td>
</tr>
<tr>
<td>1,500</td>
<td>$52.95/Month</td>
<td>$31.95/Month</td>
<td>$25.95/Month</td>
</tr>
</table>
You can use nth-child css psuedoselectors:
td:nth-child(1) {
color: yellow;
background-color: #AAA;
}
td:nth-child(2) {
color: red;
}
td:nth-child(3) {
color: blue;
}
<table>
<tr>
<td>Yellow</td>
<td>Red</td>
<td>Blue</td>
<td>Normal</td>
</tr>
</table>
First let's create a simplify version of your table.
table tr td{
border:2px solid black;
width:70px;height:30px;
text-align: center;
}
table{
border-collapse: collapse;
}
table:hover tr td:nth-child(1){
background: yellow;
}
table:hover tr td:nth-child(2){
background:blue;
}
table:hover tr td:nth-child(3){
background:red;
}
<table>
<tr>
<td>one</td>
<td>two</td>
<td>three</td>
<td>four</td>
</tr>
<tr>
<td>five</td>
<td>six</td>
<td>seven</td>
<td>eight</td>
</tr>
<tr>
<td>nine</td>
<td>ten</td>
<td>eleven</td>
<td>twelve</td>
</tr>
</table>
Let me give you a bit of an explanation.. the table:hover tr td:nth-child(1) part, first the table:hover part, when we hover to the whole table, we want to target all the tr, inside the table, then inside the tr, we want to only select the first td ":nth-child(1)" of every tr, so this will only select and change the background color of the one,five and nine td (which is the first column of the table) color to yellow if we hover the mouse to the whole body of the table.
PS: For me, I prepare to do this on JavaScript.

Table with fixed tbody height

I am having issue trying to make table 1 look like table 2 using css. I also noticed the increased height and watermark image does not reflect on print preview
Table 1
Table 2
Adding a row at the end of the tbody solve this.
<table>
<thead>
<tr>
<th>S/N</th>
<th>Description of goods</th>
<th>QTY</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Arch</td>
<td>7.92</td>
</tr>
<tr>
<td>2</td>
<td>White</td>
<td>3.96</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid #000;
}
td {
border-top: 0;
border-bottom: 0;
}
table {
height: 150px;
}
tr:last-child {
height: 100%;
}
Check my example https://jsfiddle.net/moisesnandres/4py2m8aq/

How do I select the “last child” of element with specific class name in CSS?

I have a HTML table and I want the last row, excluding the row with class .table-bottom, and remove its border-bottom:
<table cellpadding="0" cellspacing="0" style="width:752px;" class="table">
<tr class="table-top"><th style="width:40px;">ID</th> <th>Post title</th> <th>Date</th> <th>Owner</th> <th style="width:100px;">Actions</th></tr>
<tr class="table-middle"><td>1</td> <td>Test</td> <td>16.7.2013</td> <td>Admin</td> <td>Delete</td></tr>
<tr class="table-middle"><td>2</td> <td>Test 2</td> <td>3.8.2013</td> <td>Admin</td> <td>Delete</td></tr>
<tr class="table-bottom"><td colspan="5"></td></tr>
</table>
.table tr th {
color:#929191;
font-weight:bold;
font-size:14px;
text-align:left;
padding-left:19px;
border-right:1px solid #d7d7d7;
border-bottom:1px solid #d7d7d7;
}
.table tr th:last-child {
border-right:none;
}
.table tr td {
color:#929191;
font-weight:bold;
font-size:12px;
padding:19px;
border-right:1px solid #d7d7d7;
border-bottom:1px solid #d7d7d7;
}
.table tr td:last-child {
border-right:none;
}
.table .table-middle:last-of-type td {
border-bottom:none;
background:red;
}
.table tr.table-middle td:first-child {
text-align:center;
}
.table tr th:first-child {
text-align:center;
padding-left:0px;
}
.table tr.table-bottom td {
border-right:none;
border-bottom:none;
}
But the .table .table-middle:last-of-type td selector is not working.
Here is the Fiddle.
Any Ideas?
Short answer: you can't do it by CSS, unless you change your HTML structure.
More detail:
:last-of-type pseudo-class, represents the element which is the last sibling of its type in the list of children of its parent element.
Consider this selector: .table-middle:last-of-type.
while .table-middle points the element correctly, the element that has .table-middle class is NOT the last sibling of its type, because the term of type refers to the HTML element itself, not combination of element.class.
Here is the working solution:
HTML
<table cellpadding="0" cellspacing="0" style="width:752px;" class="table">
<thead>
<tr>
<th style="width:40px;">ID</th>
<th>Post title</th>
<th>Date</th>
<th>Owner</th>
<th style="width:100px;">Actions</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5"></td>
</tr>
</tfoot>
<tbody>
<tr>
<td>1</td>
<td>Test</td>
<td>16.7.2013</td>
<td>Admin</td>
<td>Delete</td>
</tr>
<tr>
<td>2</td>
<td>Test 2</td>
<td>3.8.2013</td>
<td>Admin</td>
<td>Delete</td>
</tr>
</tbody>
</table>
CSS
.table tbody tr:last-child td {
border-bottom:none;
background:red;
}
Here is the Fiddle
You could make it much simpler without using classes at all:
HTML:
<table>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
</table>
CSS:
table {
width: 500px;
}
table td { /* style for all the cells between the top and bottom row */
background: red;
}
table tr:first-child td { /* style for the cells in the top row */
background: green;
}
table tr:last-child td { /* style for the cells in the bottom row */
background: blue;
}
table tr:first-child td:first-child, /* first row, first cell */
table tr:first-child td:last-child, /* first row, last cell */
table tr:last-child td:first-child, /* last row, first cell */
table tr:last-child td:last-child { /* last row, last cell */
background: yellow;
}
Also check the working JSFiddle.

Set same width two tables

I have two tables that show data from database.
Now I set 1st table for headlines and 2nd table for the data.
I set like this
<table class="t_status">
<td>No</td>
<td>Name</td>
<td>Address</td>
</table>
In table #2
<table class="t_status">
<td>1</td>
<td>Michael</td>
<td>California</td>
<tr>
<td>2</td>
<td>Greg</td>
<td>LA</td>
Now facing the problem when data display, table 1 and table 2 set different width.
This is the CSS
table
{
empty-cells: show;
border-collapse: collapse;
width: 100%;
}
.t_status
{
border-collapse: collapse;
background: #fff;
border: 1px solid #d9d9d9;-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px;
width: 100%;
margin-top: -1px;
}
.t_status td, th
{
border-top: 1px solid #c0c0c0;
border-bottom: 1px solid #c0c0c0;
border-left: 1px solid #c0c0c0;
border-right: 1px solid #c0c0c0;
padding: 10px;
font-size: 40pt;
font-weight: bold;
}
.t_status td
{
color: #fff;
background-color: #000;
}
.t_status th
{
font-size: 40pt;
color: #fff;
}
Try to put them like this:
<table class="t_status">
<tr>
<td>No</td>
<td>Name</td>
<td>Address</td>
</tr>
</table>
and
<table class="t_status">
<tr>
<td>1</td>
<td>Michael</td>
<td>California</td>
</tr>
<tr>
<td>2</td>
<td>Greg</td>
<td>LA</td>
</tr>
</table>
if am correct you are using two tables for scrolling effect of head and data, so you will get table header for all the data.
to achieve this effect you can try using jquery table jtable
sample code
Your html syntax is incorrect. Use tr tags:-
<table class="t_status">
<tr>
<td>No</td>
<td>Name</td>
<td>Address</td>
</tr>
</table>
You should put all information into one table, thus you can assure that the rows have the same width.
<table class="t_status">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Michael</td>
<td>California</td>
</tr>
<tr>
<td>2</td>
<td>Greg</td>
<td>LA</td>
</tr>
</tbody>
</table>
<thead></thead> and <tbody></tbody> are not necessary.
It seems that you have forgot the <tr> tags. By the way, if you want to preserve your markup (correct or not, but two different tables), you can try with nth selectors and give a fixed width to each cell:
.t_status td:nth-child(1) {
width:2em;
}
.t_status td:nth-child(2) {
width:5em;
}
.t_status td:nth-child(3) {
width:5em;
}
Here's a working example.