A small piece of HTML can change what a visitor sees or how the browser understands a page. In this lesson, you will explore popover advanced patterns: anchoring and light dismiss using a short example that you can inspect and change.
The Idea in Plain English
A popover can show temporary content without manually writing all of the open/close JavaScript. A button can target the popover by ID.
You can test this using a small local HTML file. Change one part at a time and check the result in the browser before adding more code.
A Working Example
Read the sample once before copying it. Notice the element names and attribute values, then compare them with the explanation below.
<button type="button" popovertarget="help-popover">Help</button>
<div id="help-popover" popover>
<p>Use a clear subject line.</p>
</div>
What Each Part Does
- A button performs an action; type="submit" can submit its form.
- A div groups content without adding a special meaning.
- A paragraph keeps one idea together as readable text.
- type chooses the relevant kind of script, input, media or embedded object.
- id names an element for labels, fragments, CSS or JavaScript; IDs should be unique.
The browser can toggle the popover when the button is pressed. Auto popovers generally support light dismiss.
Keep the example small while learning. Once it behaves as expected, try it inside a larger page and make sure the URLs, files and surrounding HTML are correct.
Try It Yourself
Try this next: create a contextual actions menu. Then inspect the page and explain what changed.
Common Beginner Mistakes
- Using a popover for a task that needs modal focus restriction, which is better served by a modal dialog.
- Changing several parts of the example at once, making it difficult to see which part caused a different result.
- Choosing a tag only because its default appearance looks right, without checking whether it describes the content correctly.
Key Takeaways
- A popover can show temporary content without manually writing all of the open/close JavaScript. A button can target the popover by ID.
- The browser can toggle the popover when the button is pressed. Auto popovers generally support light dismiss.
- Test the markup in a browser, and check accessibility and real-world behavior where relevant.