change style of table's specific columns - css

As you can see, I have two tables each inside two divisions . Here is jsFiddle example ! What I want to do is to change the background color of firstDiv's table first column and third column , secondDiv's table second column and fourth column , By CSS only :)
<div id="firstDiv" style="float:left;margin-right:12px;">
<table width="200" border="1" cellpadding="8">
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
<div id="secondDiv">
<table width="200" border="1" cellpadding="8">
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</div>

You are looking at typical usage of nth-child with even/odd selector.
Following should do the trick.
#firstDiv td:nth-child(odd)
{
background-color:#cecece;
}
#secondDiv td:nth-child(even)
{
background-color:#cecece;
}
Fiddle
Another variation:-
#firstDiv td:nth-child(2n+1)
{
background-color:#cecece;
}
#secondDiv td:nth-child(2n+2)
{
background-color:#cecece;
}
If you want to specifically select 1st and 3 alone then you can use
#firstDiv td:nth-child(1),
#firstDiv td:nth-child(3) {
background-color:#cecece;
}
#secondDiv td:nth-child(2),
#secondDiv td:nth-child(4) {
background-color:#cecece;
}
See for support

Use :nth-child pseudo-selector. See the fiddle

Edited:
You can achive it by using nth-child also but all browser does not support nth-child
But the following approach is supported in all browsers..
CSS:
#firstDiv td,
#firstDiv td + td + td{
background-color:#cecece;
}
#firstDiv td + td,
#firstDiv td + td + td + td{
background-color:#fff;
}
#secondDiv td + td,
#secondDiv td + td + td + td{
background-color:#cecece;
}
#secondDiv td,
#secondDiv td + td + td{
background-color:#fff;
}
SEE DEMO

You will need to apply your CSS class to a relevant cell (e.g. 1st TD) of every row in the table. This way an impression will be given that entire column is of a different color. When setup like this if you change background color in CSS class definition - the change will affect all cells in the column at once.

Related

Handlebars - custom CSS on table not working

I am working on the CSS applied on the table in Handlebars express.
Here's my hbs template to display my table.
<div class="table-1">
<table class="table" cellspacing="0">
<tr>
<th class="left">Item No</th>
<th class="left">Description</th>
<th class="left">Qty</th>
<th class="left"> Unit </th>
<th class="left">Prix</th>
<th class="left">Discount</th>
<th class="left">VAT</th>
<th class="left">Total</th>
</tr>
<tr>
<td> 0000 </td>
<td> Description </td>
<td> 0 </td>
<td> unit </td>
<td> 0.00 </td>
<td> 0 % </td>
<td> 0% </td>
<td> 0.00 </td>
</tr>
<tr>
<td> 0000 </td>
<td> Description </td>
<td> 0 </td>
<td> unit </td>
<td> 0.00 </td>
<td> 0 % </td>
<td> 2.5% </td>
<td> 0.00 </td>
</tr>
<tr style="width: 100%;">
<td colspan="4"></td>
<td colspan="3"> Total gross</td>
<td> 0.00 </td>
</tr>
<tr>
<td colspan="4"></td>
<td colspan="3"> Discount </td>
<td> 0.00 </td>
</tr>
<tr>
<td colspan="4"></td>
<td colspan="3"> Total Amt </td>
<td> 0.00 </td>
</tr>
</table>
</div>
Here's my css,
<!DOCTYPE html>
<html>
<head>
<style>
.table-1 .left {
text-align: left;
}
.table-1 .table td,
.table-1 .table th {
border: none;
font-weight: 300;
}
.table-1 tr:nth-child(odd) {
background-color: #EEEEEE;
}
.table-1 th,
.table-1 tr:last-child {
background-color: #16e0bb;
color: #FFFFFF;
}
.table-1 tr:last-child>td {
font-weight: 700 !important;
}
</style>
<meta charset="utf-8">
</head>
<body>
{{{body}}}
</body>
</html>
Issue: the CSS is not applied correctly when rendering the PDF template using Handlebars express. Even inline css is not working especially when using HEX color codes.
It seems, that background-color css does not work at all. What other way is there to apply background-color css on the table? To render the pdf, I'm using Handlebars express, puppeteer and NodeJS.
Expected Output:
Actual Output:
Was able to fix the issue above.
Solution: should include 'printBackground: true' in PDF options before generating the PDF from HTML with Puppeteer and NodeJS.
Thank you

Vertical align for tbody

I have a table separated by thead and tbody. The text I have in tbody is vertically centered. I want to have it displayed at the top of the tbody.
<thead>
<tr>
<td>.... </td>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5">
<h1>Hi</h1>
<p> Hello!
</p>
</td>
</tr>
</tbody>
I tried doing something like:
tbody { vertical-align:top;}
But that doesnt work
The problem is not with valign as it works correctly, the problem is the element h1 has a margin by default, you should set it to 0:
h1 {
margin: 0;
}
<table border="1">
<thead>
<tr>
<td>.... </td>
<td>.... </td>
</tr>
</thead>
<tbody valign="top">
<tr>
<td>
<h1>Hi</h1>
<p> Hello!</p>
</td>
<td>
A
</td>
</tr>
</tbody>
</table>

How to property adjust table?

I have this simple code.
<div class="rating" >
<table>
<tr>
<th> Award </th>
<th> Winner </th>
<th> Runner-up </th>
</tr>
<tr>
<td> Starburst Award </td>
<td> #winner </td>
<td> #runner-up#</td>
</tr>
<tr>
<td> Rising Star Award </td>
<td> #winner </td>
<td> #runner-up#</td>
</tr>
<tr>
<td> Shooting Star Award </td>
<td> #winner </td>
<td> #runner-up#</td>
</tr>
<tr>
<td> Shining Star Award </td>
<td> #winner......................... </td>
<td> #runner-up#.....................................</td>
</tr>
</table>
</div>
table.rating, th,tr,td
{
width:100%;
border-collapse:collapse;
border:1px solid black; height:50px;
border:1px solid black;
}
Using the css I want to make better. How can I have it that it looks more like a table and not be separated like that, and have the be in the middle?
I have created a http://fiddle.jshell.net/9axbx/ , all I really want to do make the table look better.
The following code sets it up more like a table:
<div class="rating" >
<table border="1">
<tr>
<th> Award </th>
<th> Winner </th>
<th> Runner-up </th>
</tr>
<tr>
<td> Starburst Award </td>
<td> #winner </td>
<td> #runner-up#</td>
</tr>
<tr>
<td> Rising Star Award </td>
<td> #winner </td>
<td> #runner-up#</td>
</tr>
<tr>
<td> Shooting Star Award </td>
<td> #winner </td>
<td> #runner-up#</td>
</tr>
<tr>
<td> Shining Star Award </td>
<td> #winner</td>
<td> #runner-up#</td>
</tr>
</table>
</div>
<style type="text/css">
table.rating, th,tr,td
{
width: 30%;
border-collapse:collapse;
border:1px solid black;
height:50px;
border:1px solid black;
text-align: center;
}
</style>
Here's what it looks like:
I'm not quite sure what you're looking for, but that should be a good start. You really should have the <style></style> tags inside the <head></head> of your html document, but the above still works, for demonstration purposes.
EDIT: Just a note on the changes I made--
I first removed the periods after the text in the last couple of cells. Not quite sure what you put them there for. Second, I added text-align:center to your CSS. If you don't want the text centered, you can change it to, say, text-align:left to align it to the left. Third, You do not need a border, and if you don't want one just remove the border options from the CSS. Finally, I changed the width of your cells to 30%, so they all should be the same width. This makes things much neater and cleaner. 100% makes a cell the full width of the table, which I assume you don't want. If you want a cell to take up more than one column you should use the colspan attribute.

First row of table disappears once the column headers are fixed (sticky header)

I need your help,
I can't seem to figure out as to why the first row in the table (ABC-123-123456) disappears or appears to be hidden once the top columns are fixed. As my work uses IE7 still I need to be able to have a browser compliant sticky column headers.
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#data_container {
margin-top: 5px;
width: 100%;
height: 200px;
overflow-x: scroll;
overflow-y: scroll;
border: 1px solid #ccc;
position: relative;
color: rgb(60,60,60);
font-size: 9pt;
}
#data {
border-collapse:collapse;
border-spacing: 0;
border-right: 1px solid #ccc;
table-layout: fixed;
}
#data th {
border-bottom:1px solid #ccc;
border-left: 1px solid #ccc;
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#ffffff", endColorstr="#dcdcdc");
font-weight: normal;
}
#data tr {
text-align: center;
}
#data td {
border-bottom:1px solid #ccc;
border-left: 1px solid #ccc;
text-align: left;
padding-left: 6px;
}
#data tr:hover td { background: #f2f2f2; }
#data th, #data td {
height: 20px;
width: 130px;
}
#data tr td:first-child, #data tr th:first-child {
border-left: 0;
}
#data thead tr {
top:expression(this.offsetParent.scrollTop);
position: absolute;
}
#data tr:first-child td {
border-top: 0;
}
</style>
</head>
<body>
<div id="data_container">
<table id="data">
<!-- Table Header -->
<thead>
<tr>
<th data-sort="string">File Number</th>
<th>Test Column</th>
<th>Request Type</th>
<th>Resize This</th>
<th>Firstname</th>
<th>Lastname</th>
<th data-sort="string">Progress</th>
<th>Vital Task</th>
</tr>
</thead>
<!-- Table Header -->
<!-- Table Body -->
<tbody>
<tr>
<td>ABC-123-123456</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>100%</td>
<td>Yes</td>
</tr><!-- Table Row -->
<tr>
<td>ABC-123-942471</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>100%</td>
<td>Yes</td>
</tr><!-- Darker Table Row -->
<tr>
<td>ABC-123-408126</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>20%</td>
<td>No</td>
</tr>
<tr>
<td>ABC-123-396225</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>80%</td>
<td>No</td>
</tr>
<tr>
<td>ABC-123-385417</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>ABC-123-374250</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>ABC-123-408970</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>ABC-123-404552</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>ABC-123-403102</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>100%</td>
<td>Yes</td>
</tr>
<tr>
<td>ABC-123-404555</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>23%</td>
<td>yes</td>
</tr>
<tr>
<td>ABC-123-406789</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>80%</td>
<td>No</td>
</tr>
<tr>
<td>Hyperlink Example</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>80%</td>
<td>Another</td>
</tr>
</tbody>
<!-- Table Body -->
</table>
</body>
</html>
add this
#data tbody{
position: absolute;
top: 24px;
}
Remove position:absolute from #data thead tr.
jsfiddle

How to prevent to pass css attributes to the inner elements?

I'm creating 3 tables.
<table id="first">
<tr>
<td>
1. CAPTION
</td>
</tr>
<tr>
<td>
<table id="second">
<tr>
<td>
2. CAPTION
</td>
</tr>
<tr>
<td>
<img src="" width="100" height="100" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table id="third">
<tr>
<td>
3. CAPTION
</td>
</tr>
<tr>
<td>
<img src="" width="100" height="100" />
</td>
</tr>
</table>
</td>
</tr>
</table>
I want to add 10px padding (padding-top:10px) for the main table td elements.
#first tr td
{
padding-top: 10px;
padding-left: 0pt;
}
But this padding is adding to inner tables td elements.
How can i prevent to pass padding-top setting to the inner tables?
Use #first > tr > td.
It means "a td that is a direct child of a tr that is a direct child of an element with ID 'first'".
There are 2 solutions.
One (the hard way),
Specify padding values to the inner child elements so that it over-rides the parent style specifications.
Two (the above method),
Use '#first > tr > td'
but, it leads to cross browser comparability issues.
ie-6 does have a large share of the browser market.

Resources