Table styling with CSS - html

I made a simple HTML table for this question.
I want to have a different style for the text and a different one for the numbers in the <td> and in the <tfoot>.
Can I style text differently from numbers in <td> and the <tfoot>?
and
What is the best way in the web practice to style a more complex table?
Update
Like this?
<td> 12<b>x</b>6 </td>
Here is my fiddle.
HTML
<table>
<caption>Woah</caption>
<thead>
<tr>
<th></th>
<th>Animalistic</th>
<th>People</th>
</tr>
</thead>
<tfoot>
<td>Run</td>
<td>1 x 92</td>
<td>1 x 92</td>
</tfoot>
<tbody>
<tr>
<td>John</td>
<td>9889 x 92</td>
<td>9889 x 92</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Bill</td>
<td>9889 x 92</td>
<td>9889 x 92</td>
</tr>
</tbody>
</table>
CSS
table {
width:300px;
background:#f5f7f3;
}
caption {
background:#000;
color:#fff;
}
th {
border:1px solid #000;
height:40px;
background:#b2b2b2;
}
td {
border:1px solid #b2b2b2;
}

add a < span > tag to the text with the style you want.
<td><span style="some style" or class = "some class"> some text </span></td>

You can't set different color for numeric value and text if they are in same tag. You have to give them separate tag. Like:
<td><span>1</span> text <span>92</span></td>
Then add css:
td span{
color:red;
}
Note: numeric values are in span tag and text value don't have any
tag. so with this css we can set color for numeric value and text
value different

There are numerous ways to target table elements. Here is an excellent list of available CSS selectors.
Example of the below.
Basic HTML structure (only one <tbody>):
<table>
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>9999 x 9999</td>
</tr>
</tbody>
<tfoot>
<tr>
<td></td>
</tr>
</tfoot>
</table>
To target the numbers separately, use a <span>:
<td>9999 <span>x</span> 9999</td>
OR
<td><span>9999</span> x <span>9999</span></td>
Target in the CSS with:
tbody td span {
/* styles */
}
Target every second row in the tbody with nth-child:
tbody tr:nth-child(even) {
background: #F00;
}
Target every td in the tbody:
tbody td {
text-align: center;
}
Target the first td of each row:
tbody td:first-child {
text-align: right;
}
Target the tfoot itself
tfoot {
background: #CCC;
}

Related

How to override a specific table border CSS style while using Bootstrap table formatting

I'm using Bootstrap with tables, and trying to make some minor overrides to the default CSS with limited success.
In the table below, I'm able to add a dark border at the bottom of the table head (thead), and to the bottom of the table rows in the footer (tr in tfoot), but I cannot add a border to the bottom of the last table row (tr:last-child), or alternately the bottom of the table body (tbody), or I suppose the top of the table footer (tfoot).
I've had limited success with this:
.table-sm.event-table tbody > tr:last-child {
border-bottom: 2px solid #999;
}
However this doesn't render in all browsers, and only 'works' by making the single pixel light grey line a 2 pixel dark line, which I don't want, I just want a single pixel dark border between the last row of the body and the first row of the footer (between Row Two and Total Expense).
I know this has to do with the specificity of the CSS rules, and Bootstrap's rule taking precedent over my own, but even though I was able to make the other rules work, I cannot for the life of me figure out how to specify this one.
.event-table {
width: 100%;
}
.table thead > tr > th {
border-bottom: 1px solid #333;
}
.table tfoot > tr > td {
border-bottom: 1px solid #333;
}
<table class="table table-bordered table-sm event-table">
<thead>
<tr>
<th>Unit</th>
<th>Total</th>
</tr>
</thead>
<tfoot>
<tr>
<td>Total Expense $</td>
<td class="text-right">$200</td>
</tr>
<tr>
<td>Total Revenue $</td>
<td class="text-right">$300</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>Row One</td>
<td>$100</td>
</tr>
<tr>
<td>Row Two</td>
<td>$100</td>
</tr>
</tbody>
</table>
Specificity is the name of the game and if you deal with Bootstrap, you'll quickly learn that it get's very complicated and even nigh impossible. While using #ids and !important may be an immediate remedy to your situation, it will bite you in the #rse if they are used even if only moderately. Try using only a few #id if you must and avoid !important at all costs.
A safer solution is to double up on a class:
As a nonsense special case for (2), duplicate simple selectors to increase specificity when you have nothing more to specify.
MDN - The !important exception
The following demo has each table section (i.e. <thead>, <tbody>, and <tfoot>) with it's last row border-bottom a different color. Note that the bootstrap.css file is loaded as well, so it does work to the best of my knowledge and evidence at hand.
Demo
.event-table {
width: 100%;
}
.table thead>tr.rowA1.rowA1>th {
border-bottom: 1px solid red;
}
.table tbody>tr.rowB2.rowB2>td {
border-bottom: 1px solid lime;
}
.table tfoot>tr.rowC2.rowC2>td {
border-bottom: 1px solid blue;
}
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'>
<table class="table table-bordered table-sm event-table">
<thead>
<tr class='rowA1'>
<th>Unit</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr class='rowB1'>
<td>Row One</td>
<td>$100</td>
</tr>
<tr class='rowB2'>
<td>Row Two</td>
<td>$100</td>
</tr>
</tbody>
<tfoot>
<tr class='rowC1'>
<td>Total Expense $</td>
<td>$200</td>
</tr>
<tr class='rowC2'>
<td>Total Revenue $</td>
<td>$300</td>
</tr>
</tfoot>
</table>
Give your tags an id instead of a class. This way, when you go to style the certain element with the id in your css, it will be at a higher priority than the Bootstrap style, which would erase the need for !important in most cases
So say you add an id to your table tag in the html like so
<table class="table table-bordered table-sm event-table" id="main-table">
You should be able to do this with success
#main-table {
width: 100%;
}
#main-table thead > tr > th, #main-table tfoot > tr > td {
border-bottom: 1px solid #333;
}
Sometimes you will have to use !important to override Bootstrap styles like so
border-bottom: 2px solid #999 !important;

How to remove spaces between cells in a html table

I try to remove white space between Table1Header and Table2Header. I tried border:0px, padding:0px and border-spacing:0px; styles. Firefox and Opera tell me that my border-spacing style is overrided by the user agent style (which is 2px). How can I force browser to use my styleshits?
http://jsfiddle.net/cdjDR/2/
<table class="tableGroup">
<tbody>
<tr>
<td>
<table>
<tbody>
<tr class="tableHeader">
<td><span class="tableHeader"><label>Table1Header</label></span>
</td>
</tr>
<tr class=" tableData">
<td>
<div class="ui-datatable">
<div>
<table>
<thead>
<tr>
<th>
<div><span><span class="ui-header-text">Table1Col1</span></span>
</div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td><span>2</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</td>
<td>
<table>
<tbody>
<tr class="tableHeader">
<td><span class="tableHeader"><label>Table2Header</label></span>
</td>
</tr>
<tr class="tableData">
<td>
<div class="ui-datatable">
<div>
<table>
<thead>
<tr>
<th>
<div><span><span class="ui-header-text" >Table2Col1</span></span>
</div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td><span>12345</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
span.tableHeader > label {
display: inline-block;
float:left;
line-height:30px;
padding-left:10px;
color: #202020;
font-size: 13px;
}
tr.tableHeader {
background-color: #EEEEEE;
}
table.tableGroup, table.tableGroup > tr > td > table {
border-spacing: 0px;
}
table.tableGroup div.ui-datatable th > div > span >span.ui-header-text {
color: #808080;
font-size: 11px;
}
table.tableGroup td, table.tableGroup th {
padding: 0px;
border: 0px;
}
You can simply use border-collapse: collapse; or even border-spacing: 0; is fine
table { /* Will apply to all tables */
border-spacing: 0;
/* OR border-collapse: collapse; */
}
Demo
You can easily override the useragent stylesheet with a simple element selector.
If you want to normalize the styles, you should use CSS Reset
Coming to your selector which is seems dirty to me, as yo are targeting the table with class .tableGroup and the table nested under that
table.tableGroup, table.tableGroup > tr > td > table {
border-spacing: 0px;
}
So you better use
table.tableGroup,
table.tableGroup table {
border-spacing: 0;
}
you need to add (border="0" cellpadding="0" cellspacing="0") Table tributes in every table tag
example
<table border="0" cellpadding="0" cellspacing="0">
*example with your classes *
<table border="0" cellpadding="0" cellspacing="0" class="tableGroup">
Try this
table {
border-spacing:0px;
}
works by using css
The browsers are not telling you that your border-spacing style is overridden by the user agent style sheet. Instead, they may indicate that inheritance does not take place for it. This is simply caused by the fact that some style sheet sets the property on the element.
The reason why your rule is not applied to the inner table element is that it does not match any of your selectors. The selector
table.tableGroup > tr > td > table
does not match it, because a tr element is never a child of table even if might appear to be. By HTML syntax, there is an intervening tbody element, even if its start and end tag are missing. The following selector would match:
table.tableGroup > tbody > tr > td > table
Of course, a mere table selector would do the job as well, provided that you want all table elements to be styled by the rule.

HTML Table Alternating Row THBody Usage

I have several html tables in my content area of my page. The style is weird because it doesn't start the alternating row color fresh at the start of each table, it carries it on through out the list of tables.
<table>
<tr>
Blue
</tr>
<tr>
White
</tr>
<tr>
Blue
</tr>
</table>
<table>
<tr>
White
</tr>
<tr>
Blue
</tr>
<tr>
White
</tr>
</table>
The colour in the rows is a representation of what the css would set as the row background. But I want css to start the alternating again for the next table. So it would be:
<table>
<tr>
Blue
</tr>
<tr>
White
</tr>
<tr>
Blue
</tr>
</table>
<table>
<tr>
Blue
</tr>
<tr>
White
</tr>
<tr>
Blue
</tr>
</table>
Does THBODY have anything to do with it?
Thanks,
CSS Code
table { border-collapse:collapse; text-align:center; }
table th, td { border:1px solid #759EC7; padding:3px 7px 2px; }
th { color: #fff;
background-color: #5c87b2; text-align:center; }
tr:nth-child(odd) { background-color: #CEE1F5; }
tr:nth-child(even) { background-color: #fff; }
Update
It may be a bug that has crept in, I've look on the suggested fiddles and it works perfectly so it is just some buggy code somewhere.
You can easily achieve it using combinations of :nth-child() by passing even and odd values. For eg. see this fiddle.
where, the CSS is
body {
background-color: black;
color: red;
}
table tr:nth-child(odd) {
background-color: blue;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
The only problem you have is missing the tag in the table.
It works perfectly if you add it. It shouldnt have anything to do with the tbody tag.
<table>
<tr>
<td>Blue</td>
</tr>
<tr>
<td>White</td>
</tr>
<tr>
<td>Blue</td>
</tr>
</table>
<table>
<tr>
<td>Blue</td>
</tr>
<tr>
<td>White</td>
</tr>
<tr>
<td>Blue</td>
</tr>
</table>
here is the fiddle: http://jsfiddle.net/rBwBm/
I think you're doing it using javascript, right ? Probably getting a collection of tr through jquery with $('tr') ? Try using CSS nth-child(odd) and nth-child(even) instead, most modern browsers won't have any problem with that.
The issue I was having was with two <TH> rows, which through off the alternating row colouring. So for example:
<tr>
<th colpsan="2">Name</th>
</tr>
<tr>
<th>First</th>
<th>Last</th>
</tr>
This would have the Blue start on the Name row and then start alternating. So the first line of the table body would be Blue
<tr>
<th>Name</th>
</tr>
This would have the Blue start on the Name row like before and then start alternating, However, the first line of the table body would be White
In these situations it would show a changing style which is not what I wanted to achieve. So all I did to fix this is:
<thead>
<tr>
<th colpsan="2">Name</th>
</tr>
<tr>
<th>First</th>
<th>Last</th>
</tr>
</thead>
<tbody>
<!-- Table Content in Here -->
</tbody>
And I then changed the style sheet to be:
tbody tr:nth-child(odd) {}
tbody tr:nth-child(even) {}
So basically I used the TBody and THead tags to make a more specific css style which is brilliant. More control, flexibility. So in my new example, you can have as many rows in the THead as you like, the content should always start on White, and to answer my question:
Does THead have anything to do with it?
Yes, it has EVERYTHING to do with it.

Html column group and title

What would be the html markup to achieve the same result as the first table in http://reference.sitepoint.com/css/tableformatting#tableformatting__tbl_table-objects-display-values
I am looking for how they specified the column group and how to set the title (Women, Men). Also, how to target the specific column group in css.
thanks,
bsr.
Good question. I sat down and reflected on the last time I addressed table formatting issues, then navigated to following links:
http://www.w3.org/TR/html4/struct/tables.html#h-11.2.4
http://www.w3.org/TR/CSS2/tables.html
After some meditation and drinking water, wrote some code for you to refer:
body {
background: #e4e4e4;
font-family: sans-serif;
}
th {
background: #d5d6d6;
}
td {
background: #fff;
}
table {
border-collapse: separate;
border-spacing: 1em 0.5em;
background-color: #ddd;
}
th, td {
border: 1px solid #000;
padding: 4px;
}
tfoot {
font-weight: bold;
}
<table>
<thead>
<tr><th rowspan="2">Question</th><th colspan="2">Women</th><th colspan="2">Men</th></tr>
<tr><th>Yes</th><th>No</th><th>Yes</th><th>No</th></tr>
</thead>
<tbody>
<tr><th>Question1</th><td>42%</td><td>58%</td><td>61%</td><td>39%</td></tr>
<tr><th>Question2</th><td>53%</td><td>47%</td><td>69%</td><td>31%</td></tr>
<tr><th>Question3</th><td>26%</td><td>74%</td><td>51%</td><td>49%</td></tr>
</tbody>
<tfoot>
<tr><th>Average</th><td>40%</td><td>60%</td><td>60%</td><td>40%</td></tr>
</tfoot>
</table>
Mostly when I try to see such layouts, I attempt to count how many rows and how many columns will be necessary in the final html. This helps to construct the html properly.
CSS then simply becomes a selection of those elements with either classes or elements. For your question I chose elements.
HTH!
#gsvolt
I just write the same table as you mentioned in a link. I hope it'll help you out. Thanks
thead th,
tbody tr td:first-child {
background-color: #ccc;
}
<table border="1" style="border-collapse: collapse;">
<thead>
<tr>
<th rowspan="2">Question</th>
<th colspan="2">Women</th>
<th colspan="2">Men</th>
</tr>
<tr>
<th>Yes</th>
<th>No</th>
<th>Yes</th>
<th>No</th>
</tr>
</thead>
<tbody>
<tr>
<td>Question 1</td>
<td>42%</td>
<td>58%</td>
<td>61%</td>
<td>39%</td>
</tr>
<tr>
<td>Question 2</td>
<td>53%</td>
<td>47%</td>
<td>69%</td>
<td>31%</td>
</tr>
<tr>
<td>Question 3</td>
<td>26%</td>
<td>74%</td>
<td>51%</td>
<td>49%</td>
</tr>
<tr>
<td>Average</td>
<td>40%</td>
<td>60%</td>
<td>60%</td>
<td>40%</td>
</tr>
</tbody>
</table>
To make a <th> span a set of rows, give it a rowspan attribute. For collumns, a colspan attribute.
To then target that <th> element, use the normal CSS selector methods, such as .class, #id, tag, etc.
you can use <colgroup> and <col class="men">
see: http://www.smashingmagazine.com/2008/08/13/top-10-css-table-designs/
5. Vertical Zebra Style

change ahref color in th but not in tr

I have a table where the table header contains an a href as well as each tr. I want to change the color of the th ahref without affecting the color of the ahref in the tr. How can I go about this?
<table>
<tr>
<th>
Index
</th>
</tr>
<tr>
Index
</tr>
</table>
table.Documents th
{
color: Black;
}
does not work
table th a {
color: black; //wont apply to td a
}
Also, your html is badly formed. It should be:
<tr>
<td>
Index
</td>
</tr>
And if you want to use the class "Documents":
<table class="Documents">