Dofactory.com
Dofactory.com
Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

HTML <tr> style Attribute

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.

Example

#

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>

Using style

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.


Syntax

<tr style="CSS-styles">

Values

#

Value Description
CSS-styles One or more CSS property/value pairs separated by semicolons (;).

More Examples

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>

Code explanation

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.


Browser support

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

You may also like

 Back to <tr>

Last updated on Sep 30, 2023

Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

Guides