Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      The Ultimate Guide to Node.js Development Pricing for Enterprises

      July 29, 2025

      Stack Overflow: Developers’ trust in AI outputs is worsening year over year

      July 29, 2025

      Web Components: Working With Shadow DOM

      July 28, 2025

      Google’s new Opal tool allows users to create mini AI apps with no coding required

      July 28, 2025

      5 preinstalled apps you should delete from your Samsung phone immediately

      July 30, 2025

      Ubuntu Linux lagging? Try my 10 go-to tricks to speed it up

      July 30, 2025

      How I survived a week with this $130 smartwatch instead of my Garmin and Galaxy Ultra

      July 30, 2025

      YouTube is using AI to verify your age now – and if it’s wrong, that’s on you to fix

      July 30, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Time-Controlled Data Processing with Laravel LazyCollection Methods

      July 30, 2025
      Recent

      Time-Controlled Data Processing with Laravel LazyCollection Methods

      July 30, 2025

      Create Apple Wallet Passes in Laravel

      July 30, 2025

      The Laravel Idea Plugin is Now FREE for PhpStorm Users

      July 30, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      New data shows Xbox is utterly dominating PlayStation’s storefront — accounting for 60% of the Q2 top 10 game sales spots

      July 30, 2025
      Recent

      New data shows Xbox is utterly dominating PlayStation’s storefront — accounting for 60% of the Q2 top 10 game sales spots

      July 30, 2025

      Opera throws Microsoft to Brazil’s watchdogs for promoting Edge as your default browser — “Microsoft thwarts‬‭ browser‬‭ competition‬‭‬‭ at‬‭ every‬‭ turn”

      July 30, 2025

      Activision once again draws the ire of players for new Diablo Immortal marketing that appears to have been made with generative AI

      July 30, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»Creating an Auto-Closing Notification With an HTML Popover

    Creating an Auto-Closing Notification With an HTML Popover

    June 9, 2025

    The HTML popover attribute transforms elements into top-layer elements that can be opened and closed with a button or JavaScript. Most popovers can be light-dismissed, closing when the user clicks or taps outside the popup. Currently, HTML popover lacks built-in auto-close functionality, but it’s easy to add. Auto closing popups are useful for user interfaces like banner notifications — the new-message alerts in phones, for instance.

    A picture demo, is worth a thousand words, right? Click on the “Add to my bookmarks” button in the following example. It triggers a notification that dismisses itself after a set amount of time.

    CodePen Embed Fallback

    Let’s start with the popover

    The HTML popover attribute is remarkably trivial to use. Slap it on a div, specify the type of popover you need, and you’re done.

    <div popover="manual" id="pop">Bookmarked!</div>

    A manual popover simply means it cannot be light-dismissed by clicking outside the element. As a result, we have to hide, show, or toggle the popover’s visibility ourselves explicitly with either buttons or JavaScript. Let’s use a semantic HTML button.

    <button popovertarget="pop" popovertargetaction="show">
      Add to my bookmarks
    </button>
    <div popover="manual" id="pop">Bookmarked!</div>

    The popovertarget and popovertargetaction attributes are the final two ingredients, where popovertarget links the button to the popover element and popovertargetaction ensures that the popover is show-n when the button is clicked.

    Hiding the popover with a CSS transition

    OK, so the challenge is that we have a popover that is shown when a certain button is clicked, but it cannot be dismissed. The button is only wired up to show the popover, but it does not hide or toggle the popover (since we are not explicitly declaring it). We want the popover to show when the button is clicked, then dismiss itself after a certain amount of time.

    The HTML popover can’t be closed with CSS, but it can be hidden from the page. Adding animation to that creates a visual effect. In our example, we will hide the popover by eliminating its CSS height property. You’ll learn in a moment why we’re using height, and that there are other ways you can go about it.

    We can indeed select the popover attribute using an attribute selector:

    [popover] {
      height: 0;
      transition: height cubic-bezier(0.6, -0.28, 0.735, 0.045) .3s .6s;
    
      @starting-style { 
        height: 1lh;
      }
    }

    When the popover is triggered by the button, its height value is the one declared in the @starting-style ruleset (1lh). After the transition-delay (which is .6s in the example), the height goes from 1lh to 0 in .3s, effectively hiding the popover.

    Once again, this is only hiding the popover, not closing it properly. That’s the next challenge and we’ll need JavaScript for that level of interaction.

    Closing the popover with JavaScript

    We can start by setting a variable that selects the popover:

    const POPOVER = document.querySelector('[popover]');

    Next, we can establish a ResizeObserver that monitors the popover’s size:

    const POPOVER = document.querySelector('[popover]');
    const OBSERVER = 
      new ResizeObserver((entries) => {
        if(entries[0].contentBoxSize[0].blockSize == 0) 
          OBSERVER.unobserve((POPOVER.hidePopover(), POPOVER));
      });

    And we can fire that off starting when the button to show the popover is clicked:

    const POPOVER = document.querySelector('[popover]');
    const OBSERVER = 
      new ResizeObserver((entries) => {
        if(entries[0].contentBoxSize[0].blockSize == 0) 
          OBSERVER.unobserve((POPOVER.hidePopover(), POPOVER));
      });
    document.querySelector('button').onclick = () => OBSERVER.observe(POPOVER);

    The observer will know when the popover’s CSS height reaches zero at the end of the transition, and, at that point, the popover is closed with hidePopover(). From there, the observer is stopped with unobserve().

    In our example, height and ResizeObserver are used to auto-close the notification. You can try any other CSS property and JavaScript observer combination that might work with your preference. Learning about ResizeObserver and MutationObserver can help you find some options.

    Setting an HTML fallback

    When JavaScript is disabled in the browser, if the popover type is set to any of the light-dismissible types, it acts as a fallback. Keep the popover visible by overriding the style rules that hide it. The user can dismiss it by clicking or tapping anywhere outside the element.

    If the popover needs to be light-dismissible only when JavaScript is disabled, then include that popover inside a <noscript> element before the manual popover. It’s the same process as before, where you override CSS styles as needed.

    <noscript>
      <div popover="auto" id="pop">Bookmarked!</div>
    </noscript>
    
    <div popover="manual" id="pop">Bookmarked!</div>
    
    <!-- goes where <head> element's descendants go -->
    <noscript>
      <style>
        [popover] {
          transition: none;
          height: 1lh;
        }
      </style>
    </noscript>

    When to use this method?

    Another way to implement all of this would be to use setTimeout() to create a delay before closing the popover in JavaScript when the button is clicked, then adding a class to the popover element to trigger the transition effect. That way, no observer is needed.

    With the method covered in this post, the delay can be set and triggered in CSS itself, thanks to @starting-style and transition-delay — no extra class required! If you prefer to implement the delay through CSS itself, then this method works best. The JavaScript will catch up to the change CSS makes at the time CSS defines, not the other way around.


    Creating an Auto-Closing Notification With an HTML Popover originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleRaspberry Pi 5 Desktop Mini PC: Installing Software
    Next Article GitHub for Beginners: Code review and refactoring with GitHub Copilot

    Related Posts

    News & Updates

    5 preinstalled apps you should delete from your Samsung phone immediately

    July 30, 2025
    News & Updates

    Ubuntu Linux lagging? Try my 10 go-to tricks to speed it up

    July 30, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    CVE-2025-53392 – pfSense File Traversal Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    How to Create Decoy File System in Linux Using FUSE

    Learning Resources

    GitHub Copilot Spaces: Bring the right context to every suggestion

    News & Updates

    CVE-2024-54028 – Catdoc OLE Document DIFAT Parser Integer Underflow Heap-Based Memory Corruption

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    CVE-2025-3503 – “WP Maps Stored Cross-Site Scripting Vulnerability”

    May 1, 2025

    CVE ID : CVE-2025-3503

    Published : May 1, 2025, 6:15 a.m. | 1 hour, 56 minutes ago

    Description : The WP Maps WordPress plugin before 4.7.2 does not sanitise and escape some of its Map settings, which could allow high privilege users such as admin to perform Stored Cross-Site Scripting attacks even when the unfiltered_html capability is disallowed (for example in multisite setup).

    Severity: 0.0 | NA

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-4807 – SourceCodester Online Student Clearance System Directory Traversal Information Disclosure

    May 16, 2025

    CVE-2025-47115 – Adobe Experience Manager Stored Cross-Site Scripting (XSS)

    June 10, 2025

    Netsleuth calculates and analyzes IP subnet values

    April 9, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.