If I am targeting a specific row, say the first one in a table, and want to change font color, I can do this
.mytable tr:first th {
color: red
}
But how do I add a hover property to the same row? This won't work, would it?
.mytable tr:first:hover th {
color: green
}
Can you put just the first row in a thead container?
.mytable thead:hover {
color: green
}
<table class="mytable">
<thead>
<tr><th>hello</th></tr>
</thead>
<tr><td>goodby</td></tr>
</table>
What about
.mytable tr:first th:hover {
color: green
}
?
Surely you use first-child and not just first? Then apply the hover to the first-child. You don't need to specify th as it will propogate through anyway.
Check this DEMO
.mytable tr:first-child { color: red; }
.mytable tr:first-child:hover { color: green; }
.mytable tr { color:blue; }
However if you are looking to specify any row, other than the first-child, you might have to look at some javascript, or maybe the use of nth-child but that would rule out < IE8 compatibility.
How about this:
.hoverstate:hover {
color: green;
}
with HTML like this:
<table>
<tr><td>1</td><td>2</td></tr>
<tr class="hoverstate"><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
</table>
?
Related
I have a table element that has also a table inside in one cell. (Jquery UI calendar is inside actually)
How can I style only the parent?
body table tr td:nth-child(2n) {
background-color: red;
}
does this: (fiddle here)
but I would like only the outer cells (number 2 and 5) to be selected.
Use the child (>) selector and add a tbody element in the selector (no HTML changes needed):
body > table > tbody > tr > td:nth-child(2n) {
background-color: red;
}
jsFiddle example
This works because it specifically only selects the outer table.
Tested successfully in Chrome, FF, and IE.
Add the following CSS:
table table tr td:nth-child(2n) {
background-color: transparent;
}
This selects the cells, but only if they have two table parents, and sets their background-color to transparent.
JSFiddle
This works, just reset the background for the inner table.
<table>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr>
<td>4</td>
<td>5</td>
<td>
<table class="inner">
<tr><td>6</td><td>7</td><td>8</td></tr>
<tr><td>9</td><td>A</td><td>B</td></tr>
</table>
</td>
</tr>
</table>
table {
border-spacing: 2px;}
td{
border-spacing: 2px;
border: 1px solid black;
}
body table tr td:nth-child(2n) {
background-color: red;
}
body .inner tr td:nth-child(2n) {
background-color: white;
}
Fiddle
I have two styles, one which is at element level 'td' and another which is at class level '.specialTable td'. And I've run into some problems as the class level style is inheriting all the 'td' style properties if I have not specified them again.
I have a CSS style
td
{
background-color:black;
}
and then I have
.specialTable tr:nth-child(even) {
background-color: white;
}
and
.specialTable td
{
background-color:none;
}
What happens here is that even though I've asked.specialTable td to have no background, it inherits the black background from element style 'td' and this causes my element style 'tr' to be blocked out, because cells are on top of rows.
I am trying to set alternating row style to my table. Please help me with how I can stop the original 'td' element style from getting in the way.
Here is the fiddle:
http://jsfiddle.net/PIyer/phADs/1/
you have a type in your css, but im not sure if that is the problem
specialTable tr:nth-child(even) {
background-color: white;
}
should be
.specialTable tr:nth-child(even) {
background-color: white;
}
aslso background-color:none is not valid css , maybee background-color:transparent
none is not a valid property for the background color. Try this:
.specialTable tr {
background-color: black;
}
.specialTable tr:nth-child(even) {
background-color: white;
}
Or you might use in your example just
.specialTable td
{
background-color: transparent;
}
This should let the white shine through.
You could simplify things, by using basic CSS overriding.
Let's say you have this:
<table class="specialTable">
<tr>
<td>This is an odd row</td>
</tr>
<tr>
<td>This is an even row</td>
</tr>
<tr>
<td>This is an odd row</td>
</tr>
<tr>
<td>This is an even row</td>
</tr>
</table>
And your default <td> style is this:
td {
background-color:black;
color: #FFF;
}
To make alternating (zebra) styling to .specialTable, you can simply do this:
.specialTable tr:nth-child(even) td {
background-color: blue;
}
This will override the original CSS defintion for <td> for all <td> tags within an even <tr> tag.
Check out a working example here: http://jsfiddle.net/rh5vV/
It's important to note that the nth-child sudo selector does not work in versions of IE8 and lower, so you may want to apply a class of .even to your even <tr> tags.
Try this out
.specialTable tr td {
background-color:transparent;
}
using background none is incorrect, use transparent instead
http://jsfiddle.net/RBY2v/1/
You can use background-color:transparent; or depending on background:none;:
.specialTable td {
background-color:transparent;
}
Good afternoon in my timezone.
I have a table with four tr and each tr has four td.
I want to apply different width's to each td.
For example I want the following:
<tr>
<td width="20%"/>
<td width="25%"/>
<td width="10%"/>
<td width="45%"/>
</tr>
Is there a way to have just one class that is applied to the tr and inside that class I give a width to each td? Or do I create four different classes each with different width's ? Something like
.firstTD{
width:20%;
}
.secondTD{
width:25%;
}
.thirdTD{
width:10%;
}
.fourTD{
width:45%;
}
This should be supported in internet explorer 6 too.
Thanks in advance.
Best regards
You can use the adjacent selector:
.mytable td{
width:20%;
}
.mytable td+td{
width:25%;
}
.mytable td+td+td{
width:10%;
}
.mytable td+td+td+td{
width:45%;
}
Like that you wouldn't need any classes, but I would give the table itself one, or all your tables would inherit those styles, obviously.
You don't need to use multiple classes. The adjacent selector has you covered.
td { width: 20%; }
td + td { width: 25%; }
td + td + td { width: 10%; }
td + td + td + td { width: 45%; }
If you want each td to be different widths you'd need to create different classes. You can set a width on the tr but it will divide up the cells into equal parts or enough to hold the data in them.
You can use define a class in the tr and use :nth-child(n) selector to give each td a separate width
<tr class="myrow">
<td />
<td />
<td />
<td />
</tr>
css can be provided as follows,
.myrow td:nth-child(1){
width:20%;
}
.myrow td:nth-child(2){
width:25%;
}
.myrow td:nth-child(3){
width:10%;
}
.myrow td:nth-child(4){
width:45%;
}
But this will work with the browsers that support CSS3. It will be supported from IE9 onwards.
you can use a single class applied to the table (or tbody) and use
.yourclass td { /* first td */ }
.yourclass td + td { /* 2nd td */ }
.yourclass td + td + td{ /* 3rd td */ }
.yourclass td + td + td + td { /* 4th td */ }
Consider that a rule written in this way will be also applied to all of the following rules, so use this only to set and override specific properties.
If you don't need to supprt older IE version you can also use nth-child
example
.yourclass tr td:nth-child(2) { /* 2nd td */ }
Note: the class is not strictly necessary but if you have multiple tables, this style will affect only elements you want style in this way.
The only way to do it is as you suggested, creating a CSS class for each TD. In any case, the TD's class attribute would be used only for the first row, as the subsequent ones will inherit the width value.
I'm trying to highlight (change background color) of the entire row when the mouse is hovering on a table row. I searched through the Net and it should be working, but it doesn't. I'm displaying it on Chrome.
<table class="list1">
<tr>
<td>1</td><td>a</td>
</tr>
<tr>
<td>2</td><td>b</td>
</tr>
<tr>
<td>3</td><td>c</td>
</tr>
</table>
my css:
.list1 tr:hover{
background-color:#fefefe;
}
The correct css should be:
.list1 tr:hover td{
background-color:#fefefe;
}
//--this css for the td keeps overriding the one i showed earlier
.list1 td{
background-color:#ccc000;
}
Thanks for the feedback guys!
Your best bet is to use
table.YourClass tr:hover td {
background-color: #FEFEFE;
}
Rows aren't fully support for background color but cells are, using the combination of :hover and the child element will yield the results you need.
You need to use <!DOCTYPE html> for :hover to work with anything other than the <a> tag. Try adding that to the top of your HTML.
try
.list1 tr:hover td{
background-color:#fefefe;
}
tr:hover doesn't work in old browsers.
You can use jQuery for this:
.tr-hover
{
background-color:#fefefe;
}
$('.list1 tr').hover(function()
{
$(this).addClass('tr-hover');
},function()
{
$(this).removeClass('tr-hover');
});
You can simply use background CSS property as follows:
tr:hover{
background: #F1F1F2;
}
Working example
Try it:
css code:
.list1 tr:hover td {
background-color:#fefefe;
}
Recently I had a similar problem, the problem was I was using background-color, use background: {anyColor}
example:
tr::hover td {background: red;}
This works like charm!
Works fine for me... The tr:hover should work. Probably it won't work because:
The background color you have set is very light. You don't happen to use this on a white background, do you?
Your <td> tags are not closed properly.
Please note that hovering a <tr> will not work in older browsers.
Use !important:
.list1 tr:hover{
background:#fefefe !important;
}
Like #wesley says, you have not closed your first <td>. You opened it two times.
<table class="list1">
<tr>
<td>1</td><td>a</td>
</tr>
<tr>
<td>2</td><td>b</td>
</tr>
<tr>
<td>3</td><td>c</td>
</tr>
</table>
CSS:
.list1 tr:hover{
background-color:#fefefe;
}
There is no JavaScript needed, just complete your HTML code
I had the same problem.
I found that if I use a DOCTYPE like:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
it didn't work. But if I use:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
it did work.
Also try thistr:hover td {color: aqua;}
`
Also, it matters in which order the tags in your CSS file are styled. Make sure that your tr:nth-child and tr:hover td are described before table's td and th. Like so:
#tblServers {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#tblServers tr:nth-child(even){background-color: #f2f2f2;}
#tblServers tr:hover td{background-color: #c1c4c8;}
#tblServers td, #tblServers th {
border: 1px solid #ddd;
padding: 8px;
}
#tblServers th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4a536e;
color: white;
}
Is there a way to apply a Class' style to only ONE level of td tags?
<style>.MyClass td {border: solid 1px red;}</style>
<table class="MyClass">
<tr>
<td>
THIS SHOULD HAVE RED BORDERS
</td>
<td>
THIS SHOULD HAVE RED BORDERS
<table><tr><td>THIS SHOULD NOT HAVE ANY</td></tr></table>
</td>
</tr>
</table>
Is there a way to apply a Class' style to only ONE level of td tags?
Yes*:
.MyClass>tbody>tr>td { border: solid 1px red; }
But! The ‘>’ direct-child selector does not work in IE6. If you need to support that browser (which you probably do, alas), all you can do is select the inner element separately and un-set the style:
.MyClass td { border: solid 1px red; }
.MyClass td td { border: none; }
*Note that the first example references a tbody element not found in your HTML. It should have been in your HTML, but browsers are generally ok with leaving it out... they just add it in behind the scenes.
how about using the CSS :first-child pseudo-class:
.MyClass td:first-child { border: solid 1px red; }
This style:
table tr td { border: 1px solid red; }
td table tr td { border: none; }
gives me:
this http://img12.imageshack.us/img12/4477/borders.png
However, using a class is probably the right approach here.
Just make a selector for tables inside a MyClass.
.MyClass td {border: solid 1px red;}
.MyClass table td {border: none}
(To generically apply to all inner tables, you could also do table table td.)
I wanted to set the width of the first column of the table, and I found this worked (in FF7) - the first column is 50px wide:
#MyTable>thead>tr>th:first-child { width:50px;}
where my markup was
<table id="MyTable">
<thead>
<tr>
<th scope="col">Col1</th>
<th scope="col">Col2</th>
</tr>
</thead>
<tbody>
...
</tbody>
</table>
I guess you could try
table tr td { color: red; }
table tr td table tr td { color: black; }
Or
body table tr td { color: red; }
where 'body' is a selector for your table's parent
But classes are most likely the right way to go here.
I think, It will work.
.Myclass tr td:first-child{ }
or
.Myclass td:first-child { }