HTML - <summary> Tag



The 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 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.

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

Following is the syntax of the <summary> tag −

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

Example

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> Lorem, ipsum dolor sit amet consectetur adipisicing elit. Iure velit harum dicta minus provident reiciendis obcaecati aut voluptatem repellat, dignissimos architecto ea ab non eligendi sed. Cum ipsum autem alias.
   </details>
</body>
</html>

When we run the above code, it will generate an output consisting of the text along with a dropdown menu. When the user, click on that menu, it displays its internal text.

Example

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>

On running the above code, the output window will pop up, consisting of the text along with a dropdown menu text displayed as soon as the page loads.

Example

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;
         font-style: italic;
         height: 30px;
         border: 1px solid black;
         border-radius: 10px;
         text-align: center;
         align-items: center;
         justify-content: center;
         background-color: green;
         cursor: pointer;
      }
   </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>

When we run the above code, it will generate an output consisting of the text along with a dropdown menu applied with the CSS diplayed on the webpage

Example

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>

When we run the above code, it will generate an output consisting of the text along with a two dropdown menu applied with the CSS diplayed on the webpage

html_tags_reference.htm
Advertisements