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.
39 Shirts – Leaving Mozilla
In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell…
Being a Dev Dad
I get asked loads of questions every day but I’m always surprised that they’re rarely questions about code or even tech — many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes…
Duplicate DeSandro’s CSS Effect
I recently stumbled upon David DeSandro’s website when I saw a tweet stating that someone had stolen/hotlinked his website design and code, and he decided to do the only logical thing to retaliate: use some simple JavaScript goodness to inject unicorns into their page.
CSS Text Overlap
One of the important functions of CSS is to position elements.
Margin
,padding
,top
,left
,right
,bottom
,position
, andz-index
are just a few of the major players in CSS positioning. By using the above spacing…
[ad_2]