Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class on Table?

Is it not possible to style a <table> and its <tr> <td> using css classes?

For example:

<table class="calendar_table">
<tr>
        <td>
            <h2>Datum</h2>
        </td>
        <td>
            <h2>Event</h2>
        </td>
        <td>
            <h2>Type</h2>
        </td>
    </tr>
</table>

Then using something like this CSS:

.calendar_table {
    width:880px;
}
.calendar_table tr {
    margin:0;
    padding:4px;
}
like image 672
jamietelin Avatar asked Apr 18 '11 14:04

jamietelin


People also ask

Can we use class in table in HTML?

CSS classes can be used on all HTML elements.

What is class attribute in HTML table?

The class attribute specifies one or more classnames for an element. The class attribute is mostly used to point to a class in a style sheet. However, it can also be used by a JavaScript (via the HTML DOM) to make changes to HTML elements with a specified class.


2 Answers

The answers above are either old, or not answered properly, as they are ignoring the styling of the table itself and not just td or th etc. elements.

If we would have a table like this:

<table class="custom_class">
  <thead>
    <tr>
      <th>header 1</th>
      <th>header 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>row value 1</td>
      <td>row value 2</td>
      </tr>
  </tbody>
</table>

Then in .css we should put:

table.custom_class  {
  border: 1px solid black;

  td, th {
    color: blue;
  }
}
like image 138
Aleks Avatar answered Sep 28 '22 22:09

Aleks


It is possible, it should work properly!

Here is an example

Have fun, you can do whatever you want! I don't recommend using <table>though, unless it is used to present structured data that is meant to be in a table. If it is to draw a layout, use <div> and CSS!

like image 36
David Avatar answered Sep 28 '22 23:09

David