Dofactory.com
Dofactory.com

HTML <details> class Attribute

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

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

Classes, i.e. classnames, are used for styling the del element.

Example

#

A class attribute styling a <details> element.

Van Gogh's Paintings

Sunflowers

Starry Night

Self Portrait

<style>
  .bg-blue { background-color:aliceblue;padding: 10px; }
</style>

<details class="bg-blue">
  <summary>Van Gogh's Paintings</summary>
  <p>Sunflowers</p>
  <p>Starry Night</p>
  <p>Self Portrait</p>
</details>

Using class

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

<details class="classnames" >

Values

#

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

More Examples

A class attribute styling a <details> element.
Clicking the button toggles a classname that changes the background color.

Van Gogh's Paintings

Sunflowers

Starry Night

Self Portrait


<style>
  .bg-blue { background-color:aliceblue;padding: 10px; }
  .bg-turq { background-color:paleturquoise; }
</style>

<details class="bg-blue" id="mydetails">
  <summary>Van Gogh's Paintings</summary>
  <p>Sunflowers</p>
  <p>Starry Night</p>
  <p>Self Portrait</p>
</details>

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

<script>
  let toggle = () => {
    let element = document.getElementById("mydetails");
    element.classList.toggle("bg-turq");
  }
</script>

Code explanation

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

The class attribute on the <details> element assigns one classname.

Repeatedly clicking the button toggles another class, changing the background color.


Browser support

Here is when class support started for each browser:

Chrome
12.0 Jun 2011
Firefox
49.0 Sep 2016
IE/Edge
Not supported
Opera
15.0 Jul 2015
Safari
6.0 Jul 2012

You may also like

 Back to <details>

Author: Jack Poorte
Published: Jun 20 2021
Last Reviewed: Sep 30 2023


What's your favorite/least favorite part of Dofactory?


Guides