CSS for Table Properties in HTML - html

In a single page am having multiple tables but only for a set of tables want to apply css not for all. which for table bgcolor, th color, border color and text color.So I tried to create a separate class .mytable and applied which is not working but if I make that .mytable to table it comes for all the tables.Please help me to solve this.Thanks in Advance.
jsFiddle Link is Here
<style>
.mytable td th //instead of .mytable table it works
{
border:1px solid green;
}
th{
background-color:green;
color:white;
}
</style>
<table class="mytable">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
</table>
<table> /// no need of this css
</table>

Rewrite this .mytable td th as .mytable td, .mytable th
Here: http://jsfiddle.net/y8B2j/1/

Try this
.mytable td, .mytable th
{
border:1px solid green;
}
DEMO

I cant see any problem in above code, but you can try identifier instead of class by denoting it as '#mytable'.
Just give it a try.

th selects every <th>-element.
.mytable td th selects all <th>-elements inside <td>-elements inside an element, that has class mytable.
I think what you need is this:
/* td & th elements inside 'mytable' */
.mytable td, .mytable th
{
border:1px solid green;
}
/* th elements inside 'mytable' */
.mytable th
{
background-color:green;
color:white;
}

<style>
.mytable td th
{
border:1px solid green;
}
.mytable th
{
background-color:green;
color:white;
}
</style>
<table class="mytable">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
</table>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
</table>

It's because, you write wrong CSS Rule :
Your css rule : .mytable td th refers to inner th tag into table columns .mytable td.
So, If you want apply your css style for both th and td. You need to write this one:
.mytable td, .mytable th
{
border:1px solid green;
}
Try in fiddle

As i seen your code you using th as a child tag of td but th not child tag of td so use below code
Click here for fiddle
.mytable th
{
border:1px solid green;
}

Please add these lines on top of your Css file
tr{ background:#00CC66;}
th{ background:#ccCC66; border:1px dashed #0066FF;}
td{ background:#ffCC66;}
color:#fff;

Access like this:
.mytable td, .mytable th (It changes the property for all td and th nested inside the .mytable)
OR
If you have seperate class inside the mytable means
HTML
<div class="mytable">
<div class="data">
<ol>
...
</ol>
</div>
</div>
CSS
.mytable.data ol //Accessing the child ol

Related

How to change the border color of bootstrap table using inline css

Although inline css is not recommended but I just want to test it in one special case and hence want to override the default border color of a bootstrap table to white. But the following does not work. It still displays the default border color of a bootstrap table. How can it be achieved or what are other alternatives without using javascript or without messing around with default bootstrap css?
<table class="table table-bordered" style="border-color:white;">
...
...
</table>
Using only style="border-color:white;" sets the table border to white but they are being overwritten by the th and td styles.
You have to set it for every th and td too. If you are using only only Inline css then that will be time consuming. The easier way would be to include the styles in css and use direct-child sign (>) to give the style preference. Like this.
table.table-bordered > thead > tr > th{
border:1px solid blue;
}
Here is a working snippet :
table.table-bordered{
border:1px solid blue;
margin-top:20px;
}
table.table-bordered > thead > tr > th{
border:1px solid blue;
}
table.table-bordered > tbody > tr > td{
border:1px solid blue;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container">
<table class="table table-bordered">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
</tbody>
</table>
</div>
Easiest way is to add your own CSS file, with custom changes, after bootstrap default CSS. Not sure about version you use, but this is how applying of border looks in 3.3.7, and you have to overwrite that rule:
.table-bordered > tbody > tr > td, .table-bordered > tbody > tr > th, .table-bordered > tfoot > tr > td, .table-bordered > tfoot > tr > th, .table-bordered > thead > tr > td, .table-bordered > thead > tr > th {
border: 1px solid red; //your desired color
}
Demo: http://www.bootply.com/T5xU5ismja
It's probably the elements inside that have the border, in which case the inline style needs to be applied to the element that has the border, like the td
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<table class="table table-bordered">
<tr>
<td style="border-color:white;">asdf</td>
</tr>
</table>
If you want to only target the table header use the following code:-
http://www.bootply.com/oAYBUqQet3
.table-bordered > tbody > tr > th {
border: 1px solid blue;
}
table.table-bordered{
border:1px solid blue;
margin-top:20px;
}
table.table-bordered > thead > tr > th{
border:1px solid blue;
}
table.table-bordered > tbody > tr > td{
border:1px solid blue;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container">
<table class="table table-bordered">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
</tbody>
</table>
</div>
Try this:
table.table-bordered{
border:1px solid blue!important;
margin-top:20px;
}
table.table-bordered > thead > tr > th{
border:1px solid blue!important;
}
table.table-bordered > tbody > tr > td{
border:1px solid blue!important;
}

CSS table border with multiple tables

I have two tables in one page. I want to border around one of them and no border on the other one. So I am using the id="". But it is still putting borders around both tables and I am not sure why. If I change table1 for no border, it will take away the border on both tables. Please give me any helpful link or advice. Thanks.
table
{
border-collapse: collapse;
}
#table1 td, tr
{
text-align:center;
border: 1px solid black;
}
#table1 th
{
background: orange;
}
#table2
{
margin:auto;
}
<table id = "table1">
<th> Header </th>
<tr>
<td>row 1</td>
</tr>
</table>
<table id = "table2">
<th> Header </th>
<tr>
<td>row 1</td>
</tr>
</table>
You have to change your selector at #table1 td, tr.
#table1 td will apply to all td in #table1, but tr will apply for all tr.
So set it to #table1 td, #table1 tr and it works.
#table1 td, #table1 tr
{
text-align:center;
border: 1px solid black;
}
Example

Css select all but first tr doesn't work

Here's the fiddle:
https://jsfiddle.net/80mek2sL/1/
I want to select all but the first tr and apply:
border-top: 1px grey solid;
Then I want to select all first td's but not the first td of the first tr (= ignore first tr) and apply
border-right: 1px grey dotted;
(I totally dont care about compatibility with prehistorical Web browsers, I just want it to work on nowadays Web browsers)
What I dont get (that's why I'm lost actually) is that immediate selector table > tr doesn't select tr (otherwise I would have solved my problem)
Your selector is working. The problem is that tr's don't have a border. You need to apply it the td within...
#cheatsheet tr:not(:first-child) td {
border-top:1px grey solid;
background-color: #EF0;
}
Updated Fiddle
#cheatsheet td {
margin:2px;
padding:2px
}
#cheatsheet tr td:first-child {
padding-left:10%;
width:30%;
}
#cheatsheet thead {
background-color: #EFE;
}
#cheatsheet h3 {
text-align: center;
}
table#cheatsheet {
border:1px black solid;
margin:2px; padding:2px;
border-right:1px grey solid;
width:100%;
}
#cheatsheet tr:not(:first-child) td {
border-top:1px grey solid;
background-color: #EF0;
}
<h1>Vim</h1>
<table id="cheatsheet">
<thead><tr>
<td colspan="2"><h3>aa</h3></td>
</tr></thead>
<tr>
<td><code class="prettyprint lang-sh">:split</code></td>
<td style="width:auto">bb</td>
</tr>
<tr>
<td><code class="prettyprint lang-sh">:vsplit</code></td>
<td style="width:auto">split vertical</td>
</tr>
</table>
On another note, the reason table > tr doesn't work is because tr's are not an immediate descendant of table in the rendered HTML. If you use your browsers element inspector you will see that thead and tbody elements are automatically inserted for you
EDIT
After the comment below all you need to do is this...
#cheatsheet tbody td {
border-top:1px grey solid;
background-color: #EF0;
}
ie. target the td within tbody only,
Updated Fiddle
check fiddle :https://jsfiddle.net/80mek2sL/6/
nth-child(n+2) selector helps to select any number of child. in following example I am selecting row from ahead of second child.
#cheatsheet tr:nth-child(n+2) td {
border-top:1px grey solid;
background-color: #EF0;
}
You can also play aorund (n + *) and check the result to better understand the nth-child selector
note: you can not put border property to <tr> so you will need to
assign it to <td>
HTML
<table id="cheatsheet">
<thead>
<tr>
<td colspan="2">
<h3>aa</h3>
</td>
</tr>
</thead>
<tr>
<td><code class="prettyprint lang-sh">:split</code>
</td>
<td style="width:auto">bb</td>
</tr>
<tr>
<td><code class="prettyprint lang-sh">:vsplit</code>
</td>
<td style="width:auto">split vertical</td>
</tr>
<tr>
<td><code class="prettyprint lang-sh">:vsplit</code>
</td>
<td style="width:auto">split vertical</td>
</tr>
<tr>
<td><code class="prettyprint lang-sh">:vsplit</code>
</td>
<td style="width:auto">split vertical</td>
</tr>
</table>
CSS
#cheatsheet td {
margin:2px;
padding:2px
}
#cheatsheet tr td:first-child {
padding-left:10%;
width:30%;
}
#cheatsheet thead {
background-color: #EFE;
}
#cheatsheet h3 {
text-align: center;
}
table#cheatsheet {
border:1px black solid;
margin:2px;
padding:2px;
border-right:1px grey solid;
width:100%;
}
#cheatsheet tr:nth-child(n+2) td {
border-top:1px grey solid;
background-color: #EF0;
}

CSS Table Border

HI take a look at this simple code:
http://jsfiddle.net/8GsZa/
What I want is something like this:
By adding the .tborder class to tr or td wont work.
Use css border-collapse property , that may fix your problem
.tborder
{
border-collapse:collapse;
}
.tborder,.tborder td,.tborder th
{
border:1px solid #427BD6;
}
FIDDLE DEMO
Demo Fiddle Here
mention .tborder tr td in your css
.tborder tr td
{
border:1px solid #427BD6;
}
<table border="0" class="tborder" cellspacing="0" cellpadding="0">
<tr>
<td align="center">Title1</td>
<td align="center">Title2</td>
</tr>
<tr>
<td>test1</td>
<td>test2</td>
</tr>
</table>
.tborder tr td{
border:1px solid #427BD6;
}

How to highlight table row on hover using CSS only?

Is it possible to highlight table row on hover (something like this) using CSS only (without JavaScript)?
Yes, a row is possible but not a column.
tr:hover {
background-color: lightyellow;
}
Yes it's possible but you have to worry about browser compatibility here is an example
<style type="text/css">
.tbl {width: 640px;}
.tbl tr {background-color: Blue; height: 24px}
.tbl tr:hover {background-color: Red;}
</style>
<table class="tbl">
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
</table>
works with most browsers
table tr:hover td {
background: #efefef;
cursor: pointer;
}
If you don't care about Internet Explorer, the :hover CSS pseudo-class works with any element.
If you do care about IE, you can find a workaround here.
<style type="text/css">
tr.tr-hover-class:hover {
background: grey !important;
}
</style>
<table>
<tr class='tr-hover-class'></tr>
</table>
In this case you would add it to a table row using a stylesheet rule as in this CSS code example below:
tr:hover {
background-color: #ffff99;
}
Full Example:
.hoverTable{
width:100%;
border-collapse:collapse;
}
.hoverTable td{
padding:7px; border:#4e95f4 1px solid;
}
/* Define the default color for all the table rows */
.hoverTable tr{
background: #b8d1f3;
}
/* Define the hover highlight color for the table row */
.hoverTable tr:hover {
background-color: #ffff99;
}
<table class="hoverTable">
<tr>
<td>Text 1A</td><td>Text 1B</td><td>Text 1C</td>
</tr>
<tr>
<td>Text 2A</td><td>Text 2B</td><td>Text 2C</td>
</tr>
<tr>
<td>Text 3A</td><td>Text 3B</td><td>Text 3C</td>
</tr>
</table>