HTML Th Class Attribute - Dofactory

HTML <th> class Attribute HTML <tfoot> HTML <thead>

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

Classnames are defined in a stylesheet or in a local <style> element.

Classes, i.e. classnames, are used to style elements.

Example

#

A class attribute styling a <th> element.

Customers
Denice Hobermann
Paulo Cornell
<style> table.tb { width: 250px; border-collapse: collapse; } .tb th, .tb td { border: solid 1px #777; padding: 5px; } .th-blue { background-color:lightblue; } </style> <table class="tb"> <tr> <th class="th-blue" colspan="2">Customers</th> </tr> <tr> <td>Denice</td> <td>Hobermann</td> </tr> <tr> <td>Paulo</td> <td>Cornell</td> </tr> </table> Try it live

Using class

#

Classes (i.e. classnames) are used for styling the th 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.

Syntax

#

<th class="classnames">

Values

#

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

More Examples

#

A class attribute styling a <th> element. Clicking the button toggles a classname that changes the header cell to dark mode.

Customers
Denice Hobermann
Paulo Cornell
Toggle class <style> table.tb { width: 250px; border-collapse: collapse; } .tb th, .tb td { border: solid 1px #777; padding: 5px; } .th-blue { background-color:lightblue; } .th-dark { background-color: #222; color: #fff; } </style> <table class="tb"> <tr> <th id="myth" class="th-blue" colspan="2">Customers</th> </tr> <tr> <td>Denice</td> <td>Hobermann</td> </tr> <tr> <td>Paulo</td> <td>Cornell</td> </tr> </table> <br /> <button onclick="toggle();">Toggle class</button> <script> let toggle = () => { let element = document.getElementById("myth"); element.classList.toggle("th-dark"); } </script> Try it live

Code explanation

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

The class attribute in <th> assigns one classname.

Repeatedly clicking the button toggles another class, changing the <th> background and text color of the table header cell.

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

#

  • Our HTML th Tag Reference
  • Our HTML class Attribute Reference
  • Our CSS Tables Tutorial

Back to <th>

Tag » What Is Th In Html