How to change details label when open/closed without JavaScript

I wish the details element would support different labels (provided by the summary tag) for its open and closed states.

Consider a label of “Closed, click to open” when the details element is closed and “Open, click to close” when open:

Sadly only …


This content originally appeared on DEV Community and was authored by John P. Rouillard

I wish the details element would support different labels (provided by the summary tag) for its open and closed states.

Consider a label of "Closed, click to open" when the details element is closed and "Open, click to close" when open:

Open details element with label changed to indicate state.

Sadly only one summary tag is supported by the details element. However you can use an empty summary with a ::before pseudo-element to implement this.

Save the following to a file:

<html>
  <body>
  <style>
    #widget1 div { padding-inline-start: 1em; max-width: 20em;}
    #widget1 summary::before { content: "Closed, click to open";}
    #widget1[open] summary::before { content: "Open, click to close";}
    #widget1[open] summary { height: 2em;}
  </style>

  <p> Text before details.</p>
  <details id="widget1">
    <summary></summary>
    <div>
    A widget can be in one of two states. The default closed
    state displays only the triangle and the label inside summary
    (or a user agent-defined default string if no summary).
    </div>
  </details>
  <p>Text after details.</p>
  <body>
</html>

to experiment.
The second and third style lines are where the magic happens.

Note that it might not be accessible. I didn't try to see if adding a live region aria label to the summary would cause the label change to be announced. I suspect it will not, but refining this is left as an exercise for the reader. Add your experimental findings to the comments below.


This content originally appeared on DEV Community and was authored by John P. Rouillard


Print Share Comment Cite Upload Translate Updates
APA

John P. Rouillard | Sciencx (2021-07-12T21:26:26+00:00) How to change details label when open/closed without JavaScript. Retrieved from https://www.scien.cx/2021/07/12/how-to-change-details-label-when-open-closed-without-javascript/

MLA
" » How to change details label when open/closed without JavaScript." John P. Rouillard | Sciencx - Monday July 12, 2021, https://www.scien.cx/2021/07/12/how-to-change-details-label-when-open-closed-without-javascript/
HARVARD
John P. Rouillard | Sciencx Monday July 12, 2021 » How to change details label when open/closed without JavaScript., viewed ,<https://www.scien.cx/2021/07/12/how-to-change-details-label-when-open-closed-without-javascript/>
VANCOUVER
John P. Rouillard | Sciencx - » How to change details label when open/closed without JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/12/how-to-change-details-label-when-open-closed-without-javascript/
CHICAGO
" » How to change details label when open/closed without JavaScript." John P. Rouillard | Sciencx - Accessed . https://www.scien.cx/2021/07/12/how-to-change-details-label-when-open-closed-without-javascript/
IEEE
" » How to change details label when open/closed without JavaScript." John P. Rouillard | Sciencx [Online]. Available: https://www.scien.cx/2021/07/12/how-to-change-details-label-when-open-closed-without-javascript/. [Accessed: ]
rf:citation
» How to change details label when open/closed without JavaScript | John P. Rouillard | Sciencx | https://www.scien.cx/2021/07/12/how-to-change-details-label-when-open-closed-without-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.