HTML <tr> class Attribute

The class attribute on a <tr> tag assigns one or more classnames to the table row element. Classes are used to style elements. Classnames are defined in a stylesheet or in a local <style> element.

Example

#

A class attribute styling two <tr> elements.

First Name Last Name
Denice Hobermann
Paulo Cornell
<style>
  table.tb { width: 300px; border-collapse: collapse; }
  .tb th, .tb td { border: solid 1px #777; padding: 5px; }
  .tr-blue { background-color:aliceblue; }
</style>

<table class="tb">
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
  </tr>
  <tr class="tr-blue">
    <td>Denice</td>
    <td>Hobermann</td>
  </tr>
  <tr class="tr-blue">
    <td>Paulo</td>
    <td>Cornell</td>
  </tr>
</table>

Using class

The class attribute assigns one or more classnames to the <tr> tag.

Multiple classnames are separated by a space.

Classes (i.e. classnames) are used for styling the tr element.

JavaScript uses classes to access one or more elements by classname.

Tip:  class is a global attribute that can be applied to any HTML element.


Syntax

<tr class="classnames">

Values

#

Value Description
classnames One or more space-separated class names.

More Examples

A class attribute styling two <tr> elements.
Clicking the button toggles a classname that changes the data rows to dark mode.

First Name Last Name
Denice Hobermann
Paulo Cornell

<style>
  table.tbl { width: 300px; border-collapse: collapse; }
  .tbl th, .tbl td { border: solid 1px #777; padding: 5px; }
  .tr-alice { background-color:aliceblue; }
  .tr-dark { background-color:#222; color: white; }
</style>

<table class="tbl">
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
  </tr>
  <tr class="tr-alice">
    <td>Denice</td>
    <td>Hobermann</td>
  </tr>
  <tr class="tr-alice">
    <td>Paulo</td>
    <td>Cornell</td>
  </tr>
</table>

<br />
<button onclick="toggle();">Toggle class</button>

<script>
  let toggle = () => {
    let elements = document.getElementsByClassName("tr-alice");
    [].forEach.call(elements, element => element.classList.toggle("tr-dark"));
  }
</script>

Code explanation

Two CSS classes are defined in the <style> element.

The class attribute in <tr> assigns one classname.

Clicking the button calls JavaScript which locates all <tr> with the classname.

It then iterates over the elements and toggles another class changing the <tr> background and text color.


Browser support

Here is when class support started for each browser:

Chrome
1.0 Sep 2008
Firefox
1.0 Sep 2002
IE/Edge
1.0 Aug 1995
Opera
1.0 Jan 2006
Safari
1.0 Jan 2003

You may also like

 Back to <tr>
Guides