I have these css attributes.
table, td, th
{
border: 1px solid;
border-collapse: collapse;
color: green;
}
But they should only be applied if table has a special class or id?
<table class="mycl">
All other tables should not get any of this.
How can I address that in the css?
I tried for class
.mycl table, td, th
{
border: 1px solid;
border-collapse: collapse;
color: green;
}
but it did not work out. I am not sure how to combine the class with the multiple tags.
EDIT
Bases on the the current answers I get to this that does exactly what I want – only format the table with class mycl (and no other) and do it correctly (skip the mycl in the css and see).
table.mycl th, table.mycl td, table.mycl
{
border: 1px solid;
border-collapse: collapse;
color:blue;
}
Is there any optimization to that?
3
Answers
This:
Means "a
<table>
which is a child/descendant of an element with themycl
class". To reference a<table>
which has themycl
class, you’d combine the two:I think that if your table has the class "mycl" you can use something like this:
Here you specify that all the elements with class .mycl and have a td tag inside and th inside, should have those styles. But if you write:
Your saying that every table, every td tag and every th tag should have those styles.
You are using incorrect syntax per CSS specificity rules.
Specificity meaning the relevance of the property value you want to apply to an element.
Using
.mycl table
means: apply the following styles to any.mycl
class and to anytable
Using
table.mycl
means: apply the following styles to the table who contains the class namemycl
If you want to apply a style to the
table
with classmycl
then you want to use: