The class attribute assigns one or more classnames to the <tr> tag.
Classnames are defined in a stylesheet or in a local <style> element.
Classes, i.e. classnames, are used to style elements.
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>
Classes (i.e. classnames) are used for styling the tr element.
Multiple classnames are separated by a space.
JavaScript uses classes to access elements by classname.
Tip: class is a global attribute that can be applied to any HTML element.
<tr class="classnames">
Value | Description |
---|---|
classnames | One or more space-separated class names. |
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>
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.
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 |
Back to <tr>