A style attribute on a <tr> tag assigns a unique style to the table row.
Its value is CSS that defines the appearance of the tr element.
A style attribute on a <tr> element.
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; }
</style>
<table class="tb">
<tr style="background-color:aliceblue;">
<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>
The style attribute specifies the style, i.e. look and feel, of the <tr> element.
A style contains any number of CSS property/value pairs, separated by semicolons (;).
The style attribute overrides any other style that was defined in a <style> tag or an external CSS file.
This inline styling affects the current <tr> element only.
<tr style="CSS-styles">
Value | Description |
---|---|
CSS-styles | One or more CSS property/value pairs separated by semicolons (;). |
The <tr> tags below has a style attribute with a background color.
Clicking the button toggles the background color of the data rows.
First Name | Last Name |
---|---|
Denice | Hobermann |
Timothy | O'Neill |
Jane | Hollander |
<style>
table.tbl { width:300px; border-collapse: collapse; }
.tbl th, .tbl td { padding:3px; border: 1px solid #777; }
</style>
<table id="mytb" class="tbl">
<tr style="background-color:navy;color:white;">
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr style="background-color:aliceblue;">
<td>Denice</td>
<td>Hobermann</td>
</tr>
<tr style="background-color:aliceblue;">
<td>Timothy</td>
<td>O'Neill</td>
</tr>
<tr style="background-color:aliceblue;">
<td>Jane</td>
<td>Hollander</td>
</tr>
</table>
<br />
<button type="button" onclick="toggle();">Toggle style</button>
<script>
let toggle = () => {
let elements = document.getElementById("mytb").getElementsByTagName("tr");
[].forEach.call(elements, element => {
if (element.style.backgroundColor === "aliceblue") {
element.style.backgroundColor = "white";
} else if (element.style.backgroundColor === "white"){
element.style.backgroundColor = "aliceblue";
}
});
}
</script>
The style attribute assigns a background to the <tr> elements.
Clicking the button calls JavaScript which locates all the <tr> elements.
It then iterates over the elements and changes the background color.
Here is when style 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>