HTML popover Attribute

Modals have been an important part of websites for two decades. Stacking contents and using fetch to accomplish tasks are a great way to improve UX on both desktop and mobile. Unfortunately most developers don’t know that the HTML and JavaScript specs have implemented a native modal system via the popover attribute — let’s check […]

The post HTML popover Attribute appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

Modals have been an important part of websites for two decades. Stacking contents and using fetch to accomplish tasks are a great way to improve UX on both desktop and mobile. Unfortunately most developers don’t know that the HTML and JavaScript specs have implemented a native modal system via the popover attribute — let’s check it out!

The HTML

Creating a native HTML modal consists of using the popovertarget attribute as the trigger and the popover attribute, paired with an id, to identify the content element:

<!-- 
  "popovertarget" attribute will map to "id" of popover contents
-->
<button popovertarget="popover-contents">Open popover</button>
<div id="popover-contents" popover>This is the contents of the popover</div>

Upon clicking the button, the popover will open. The popover, however, will not have a traditional background layer color so we’ll need to implement that on our own with some CSS magic.

The CSS

Styling the contents of the popover content is pretty standard but we can use the browser stylesheet selector’s pseudo-selector to style the “background” of the modal:

/* contents of the popover */
[popover] {
  background: lightblue;
  padding: 20px;
}

/* the dialog's "modal" background */
[popover]:-internal-popover-in-top-layer::backdrop {
  background: rgba(0, 0, 0, .5);  
}

:-internal-popover-in-top-layer::backdrop represents the “background” of the modal. Traditionally that UI has been an element with opacity such to show the stacking relationship.

The post HTML popover Attribute appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2024-04-15T11:19:02+00:00) HTML popover Attribute. Retrieved from https://www.scien.cx/2024/04/15/html-popover-attribute/

MLA
" » HTML popover Attribute." David Walsh | Sciencx - Monday April 15, 2024, https://www.scien.cx/2024/04/15/html-popover-attribute/
HARVARD
David Walsh | Sciencx Monday April 15, 2024 » HTML popover Attribute., viewed ,<https://www.scien.cx/2024/04/15/html-popover-attribute/>
VANCOUVER
David Walsh | Sciencx - » HTML popover Attribute. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/04/15/html-popover-attribute/
CHICAGO
" » HTML popover Attribute." David Walsh | Sciencx - Accessed . https://www.scien.cx/2024/04/15/html-popover-attribute/
IEEE
" » HTML popover Attribute." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2024/04/15/html-popover-attribute/. [Accessed: ]
rf:citation
» HTML popover Attribute | David Walsh | Sciencx | https://www.scien.cx/2024/04/15/html-popover-attribute/ |

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.