HTML - <summary> Tag



HTML <summary> tag is used to specify the summary, caption, or legend for the details element disclosure box.

It is used within the details element. When a user clicks on the <summary>, it toggles the states of the parent element <details> open and closed. The <summary> element content can be any heading content, plain text, or HTML that can be used within a paragraph.

The default toggle state is closed(either you use the close attribute or not). You can also change the style to display: block to remove the disclosure triangle.

Syntax

<summary>.....</summary>

Attribute

HTML summary tag supports Global and Event attributes of HTML.

Examples of HTML summary Tag

Bellow examples will illustrate the usage of summary tag. Where, when and how to use summary tag.

Implementing summary Element

The following example is showing the uses of the HTML <summary> tag within a ‘details’ element.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML summary tag</title>
</head>
<body>
   <!--create a summary tag-->
   <p>HTML 'summary' element example..</p>
   <details>
      <summary>Read more</summary>
       It is used within the details element. 
       When a user clicked on the summary,
       it toggles the states of the parent
       element details open and closed.
       The summary element content can be any 
       heading content, plain text, or HTML 
       that can be used within a paragraph.
   </details>
</body>
</html>

Summary in List Form

Following is another example of the HTML <summary> tag. Here, we are using the <summary> tag inside a <details></details> element to specify the summary for it. Then, we are creating HTML list, when the user clicks on the summary list will be displayed.

But, we use the 'open' attribute in the 'details' element, so by default, it will open.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML summary tag</title>
</head>
<body>
   <!--create a summary tag-->
   <p>HTML 'summary' element example..</p>
   <details open>
      <summary>Overview</summary>
      <ul>
      <li>Total money : 5000</li>
      <li>Cash on hand : 3570 </li>
      <li>Spended money : (5000 - 3570) = 1430</li>
      </ul>
   </details>
</body>
</html>

Styling summary Element

In this example, we are using the HTML <summary> tag to specify the ‘summary’ for the ‘details’ element disclosure box. We are using CSS to style the created ‘summary’ element.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML summary tag</title>
   <style>
      summary {
         width: 100px;
         color: white;
         height: 30px;
         border: 2px solid black;
         border-radius: 5px;
         text-align: center;
         justify-content: center;
         background-color: green;
         cursor: pointer;
         padding-top: 10px 
      }
   </style>
</head>
<body>
   <!--create a summary tag-->
   <p>HTML 'summary' element example..</p>
   <details>
      <summary>Click me!</summary>
      <p>You clicked on click me!</p>
</body>
</html>

Nested summary Element

Let's look at the another scenario , where we are going to takemultiple or nested <summary> tag.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML summary tag</title>
   <style>
      summary:nth-child(1) {
         color: red;
      }
   </style>
</head>
<body>
   <!--create a summary tag-->
   <p>HTML 'summary' element example..</p>
   <details open>
      <summary>Click me!</summary>
      <p>Because of 'open' attribute its opened(by default)</p>
      <details>
      <summary>click me again!</summary>
      <p>You clicked on 'click me again!'</p>
      </details>
</body>
</html>

Supported Browsers

Tag Chrome Edge Firefox Safari Opera
embed Yes 12.0 Yes 79.0 Yes 49.0 Yes 6.0 Yes 15.0
html_tags_reference.htm
Advertisements