yanman1234 February 2016

Apply same CSS to td of two different tables

Say I have two tables, each with their own class:

<table class="table1">
    <tbody>
        <tr>
            <td>Something</td>
        </tr>
    </tbody>
</table>

<table class="table2">
    <tbody>
        <tr>
            <td>Something else</td>
        </tr>
    </tbody>
</table>

Now what I'd like to do is apply the same css to the td's of each class. I know I can achieve this through the following css:

.table1 tbody td, .table2 tbody td{...}

However, I was wondering if there exists a syntax such that I can remove the commonalities and reduce this down to something like:

(.table1, .table2) tbody td{...}

I have already tried the syntax above with braces, brackets, and parenthesis and have had no success. I feel like this should be a rather simple thing to do but can't find a solution anywhere.

Answers


grim February 2016

Provided there are other tables on your page that don't need the same styling, add a class to your tables (i.e. .some-class):

<table class="table1 some-class">
    <tbody>
        <tr>
            <td>Something</td>
        </tr>
    </tbody>
</table>

<table class="table2 some-class">
    <tbody>
        <tr>
            <td>Something else</td>
        </tr>
    </tbody>
</table>

Then just apply the css to this class:

.some-class td {
    ...
}


fbid February 2016

You can use a preprocessor like Sass if you want to use nested classes, like this:

HTML

<table class="t1"> 
  ....
</table>

<table class="t2"> 
  ....
</table>

CSS (Sass)

.t1,.t2{
  tr > td {
    color: red;
  }
}

Example here: http://codepen.io/anon/pen/RrqZpp

Post Status

Asked in February 2016
Viewed 3,672 times
Voted 7
Answered 2 times

Search




Leave an answer