I want to apply styles only to the table inside the DIV with a particular class:
Note: I'd rather use a css-selector for children elements.
Why does the #1 works and #2 doesn't?
1:
div.test th, div.test td, div.test caption {padding:40px 100px 40px 50px;}
2:
div.test th, td, caption {padding:40px 100px 40px 50px;}
HTML:
<html>
<head>
<style>
div.test > th, td, caption {padding:40px 100px 40px 50px;}
</style>
</head>
<body>
<div>
<table border="2">
<tr><td>some</td></tr>
<tr><td>data</td></tr>
<tr><td>here</td></tr>
</table>
</div>
<div class="test">
<table border="2">
<tr><td>some</td></tr>
<tr><td>data</td></tr>
<tr><td>here</td></tr>
</table>
</div>
</body>
</html>
What am I doing wrong?
This question is related to
css
css-selectors
The >
selector matches direct children only, not descendants.
You want
div.test th, td, caption {}
or more likely
div.test th, div.test td, div.test caption {}
Edit:
The first one says
div.test th, /* any <th> underneath a <div class="test"> */
td, /* or any <td> anywhere at all */
caption /* or any <caption> */
Whereas the second says
div.test th, /* any <th> underneath a <div class="test"> */
div.test td, /* or any <td> underneath a <div class="test"> */
div.test caption /* or any <caption> underneath a <div class="test"> */
In your original the div.test > th
says any <th> which is a **direct** child of <div class="test">
, which means it will match <div class="test"><th>this</th></div>
but won't match <div class="test"><table><th>this</th></table></div>
If you want to add style in all child and no specification for html tag then use it.
Parent tag div.parent
child tag inside the div.parent like <a>
, <input>
, <label>
etc.
code : div.parent * {color: #045123!important;}
You can also remove important, its not required
div.test td, div.test caption, div.test th
works for me.
The child selector > does not work in IE6.
.test * {padding: 40px 100px 40px 50px;}
This code can do the trick as well, using the SCSS syntax
.parent {
& > * {
margin-right: 15px;
&:last-child {
margin-right: 0;
}
}
}
Here is some code that I recently wrote. I think that it provides a basic explanation of combining class/ID names with pseudoclasses.
.content {_x000D_
width: 800px;_x000D_
border: 1px solid black;_x000D_
border-radius: 10px;_x000D_
box-shadow: 0 0 5px 2px grey;_x000D_
margin: 30px auto 20px auto;_x000D_
/*height:200px;*/_x000D_
_x000D_
}_x000D_
p.red {_x000D_
color: red;_x000D_
}_x000D_
p.blue {_x000D_
color: blue;_x000D_
}_x000D_
p#orange {_x000D_
color: orange;_x000D_
}_x000D_
p#green {_x000D_
color: green;_x000D_
}
_x000D_
<!DOCTYPE html>_x000D_
<html>_x000D_
_x000D_
<head>_x000D_
<title>Class practice</title>_x000D_
<link href="wrench_favicon.ico" rel="icon" type="image/x-icon" />_x000D_
</head>_x000D_
_x000D_
<body>_x000D_
<div class="content">_x000D_
<p id="orange">orange</p>_x000D_
<p id="green">green</p>_x000D_
<p class="red">red</p>_x000D_
<p class="blue">blue</p>_x000D_
</div>_x000D_
</body>_x000D_
_x000D_
</html>
_x000D_
As far as I know this:
div[class=yourclass] table { your style here; }
or in your case even this:
div.yourclass table { your style here; }
(but this will work for elements with yourclass
that might not be div
s) will affect only tables inside yourclass
. And, as Ken says, the > is not supported everywhere (and div[class=yourclass]
too, so use the point notation for classes).
Source: Stackoverflow.com