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 <td> style Attribute

A style attribute on a <td> tag assigns a unique style to the table cell.

Its value is CSS that defines the appearance of the td element.

Example

#

A style attribute on a <td> 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;  }
  .tb th { background: #3630a3; color:white; }
</style>

<table class="tb">
  <tr>
    <th>First name</th>
    <th>Last name</th>
  </tr>
  <tr>
    <td style="background: #f6f8ff;">Denice</td>
    <td style="background: #f6f8ff;">Hobermann</td>
  </tr>
  <tr>
    <td style="background: #f6f8ff;">Paulo</td>
    <td style="background: #f6f8ff;">Cornell</td>
  </tr>
</table>

Using style

The style attribute specifies the style, i.e. look and feel, of the <td> 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 <td> element only.


Syntax

<td style="CSS-styles">

Values

#

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

More Examples

A style attribute on a <td> element.
Clicking the button toggles the table cells 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; }
  .tbl th { background: #3630a3; color:white; }
</style>

<table class="tbl" id="mytable">
  <tr>
    <th>First name</th>
    <th>Last name</th>
  </tr>
  <tr>
    <td style="background: aliceblue;">Denice</td>
    <td style="background: aliceblue;">Hobermann</td>
  </tr>
  <tr>
    <td style="background: aliceblue;">Paulo</td>
    <td style="background: aliceblue;">Cornell</td>
  </tr>
</table>

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

<script>
  let toggle = () => {
    let elements = document.getElementById("mytable").getElementsByTagName("td");
    [].forEach.call(elements, element => {
        if (element.style.backgroundColor === "aliceblue") {
          element.style.backgroundColor = "#222";
          element.style.color = "white";
        } else {
          element.style.backgroundColor = "aliceblue";
          element.style.color = "#222";
        }
    });
  }
</script>

Code explanation

The style attribute assigns a background and text colors to the <td> element.

Clicking the button calls JavaScript which toggles the background and text color to another 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 <td>

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