Show Opening Hours in a Popover

The FAQ you built in Build an FAQ Accordion opens in place, inside the about page’s text. The garden’s opening hours are a different kind of help. A visitor on any page wants to check them in a second and then carry on with what they were doing, so the hours belong in a small panel that floats over the page and gets out of the way again.

In this lesson, you’ll add an Opening hours button to the home page’s header. The button opens such a panel with no JavaScript. You’ll style the open panel, test it with a mouse and keyboard, and learn when to reach for it instead of a <details> element. Start with the panel itself.

Open a Panel From a Button

The hours shouldn’t take up room in the header until a visitor asks for them. The popover attribute turns an element into a popover: the browser hides it until something opens it, then draws it on top of the page. The popovertarget attribute on a <button> names the popover’s id, the way a label’s for names its field’s id. The browser then opens and closes the popover when a visitor activates the button.

In index.html, add the button and the panel inside <header>, after the closing </nav> tag:

Language: HTML
<button class="hours-button" popovertarget="opening-hours">
  Opening hours
</button>
<div id="opening-hours" class="hours" popover>
  <p>The garden is open on:</p>
  <ul>
    <li>Saturdays, 9 a.m. to 1 p.m.</li>
    <li>Wednesdays, 5 p.m. to 7 p.m.</li>
  </ul>
</div>

The header will go on every page of the site, so the id must be unique on each of them. That’s why it’s opening-hours: the work day page’s sign-up form already uses hours for its field.

Save and click Opening hours. The panel opens on a white background inside a thick black border:

The home page with a filled green Opening hours button at the right end of the header, and the hours in a white box with a black border in the middle of the window.
The Popover Opens in the Middle of the Window

The panel doesn’t drop down under its button like a menu. The browser’s own styles give an open popover position: fixed, inset: 0, and margin: auto, which center it in the window.

Placing it next to its button takes anchor positioning, a CSS feature that’s only newly available in Baseline, so design the popover as a centered panel. That’s all you need to know about anchor positioning for now.

Popovers are widely available in Baseline. Every major browser has supported them since April 2024. A browser older than that ignores the popover attribute. It shows the hours as plain text after the button, and the button does nothing. The hours are still there to read.

You can simulate that fallback in your own browser. Delete the word popover from the start tag of the <div> and save. The <div> is now ordinary content, and popovertarget names no popover, so this is what an older browser shows:

A simulation with the popover attribute removed. The hours show as plain text in the header, right after a filled green Opening hours button that opens nothing.
Simulated Fallback With the Hours as Plain Text

This simulation runs in a current browser. It shows how your HTML falls back, but a real older browser may also skip some of the page’s newer CSS, so the rest of the page can look different there. Put the popover attribute back and save before you go on.

Style and Test the Open Panel

The default panel looks like a debugging box, and the button picks up the site’s filled green style for form buttons, which looks heavy next to the header’s plain links. Style both at the end of the components layer in style.css, before the layer’s closing }:

Language: CSS
.hours-button {
  padding-inline: var(--space-s);
  border: 2px solid var(--green);
  border-radius: 8px;
  background-color: var(--page);
  color: var(--green);
  font-weight: bold;
}

.hours:popover-open {
  padding: var(--space-m) var(--space-l);
  border: 2px solid var(--green);
  border-radius: 8px;
  background-color: var(--page);
  color: var(--text);
}

The :popover-open pseudo-class matches a popover only while it’s open, like [open] on a <details> element. A plain .hours rule could set a display value that overrides the browser’s rule for hiding a closed popover, so the hours would show all the time.

An older browser doesn’t know :popover-open and skips the rule, so its text stays unstyled. Save and open the panel again:

The home page with a green-bordered Opening hours button in the header, and the hours in a pale green panel with a rounded green border in the middle of the window.
The Open Panel in the Garden's Colors

Now click anywhere outside the panel, and it closes. This is light dismiss. Open it again, as in the screenshot above, and click the link under Tomás’s card, on the page behind the panel. The click reaches the link, and the volunteers page opens with no panel on it:

The volunteers page, reached from the link behind the open panel, with Volunteers in bold in the header and no hours panel showing.
The Link Acted and the Panel Closed

Watch out: a popover isn’t a modal that holds the visitor until they close it. The rest of the page stays usable, which suits help text like the hours.

The Opening hours button is only on the home page so far, so click Home in the header to go back there. Then test the panel without the mouse. Press Tab until the focus ring reaches Opening hours, and press Enter to open the panel and Esc to close it. Open it again and press Tab. The focus moves on to the next link, the Sign up link under Next Work Day.

Finally, check the header at every width. Its row now has a third item, so it needs more room. Open device mode and drag the page wider from 832 pixels, where the row starts at 52rem. The garden’s name and the button each break over two lines until about 1,090 pixels.

So in the layout layer, change the header’s @media (width >= 52rem) to 70rem, a little above that width. Below 70rem, the header stacks instead. At 1,000 pixels, where the row used to break, the name and the links now each fit on one line, with the button on its own line under them:

The home page at 1,000 pixels, with the garden's name on one line, then the seven links in one row, then the Opening hours button on its own line above the welcome heading.
The Stacked Header at 1,000 Pixels After the Change

Narrow the page to 320 pixels too. The links wrap into two rows, the button still sits on its own line, and the page doesn’t scroll sideways.

Choose the Right Built-In Element

The garden’s site now uses two kinds of built-in interactivity, and you’ll need to pick one for the next interface you build. The deciding question is what the visitor should be able to do while the content is showing:

The content The element JavaScript?
Belongs in the page’s text, open in place <details> and <summary> No
Several such entries, one open at a time <details> with a shared name No
A small panel on top, page still usable A popover and its button No
A question that temporarily blocks the page A modal <dialog> To open it, JavaScript or HTML invoker commands

A modal <dialog> suits a question such as “Withdraw your plot application?” Opened as a modal, it makes the page behind it inert, so clicks and Tab can’t reach anything else until it closes. It doesn’t force an answer, though, because Esc normally dismisses it.

HTML gives you the element, but opening it as a modal needs JavaScript or a newer HTML feature called invoker commands, which is newly available in Baseline. That’s where HTML and CSS end for this course.

To practice choosing an element for your own interface, ask the mentor:

Check Your Understanding

Try these before you open the answers:

  1. Kofi moves the Opening hours button to the footer at the very bottom of the home page. Where does the panel appear when he clicks it?
  2. Rosa wants a Free Plot notice that visitors can glance at without leaving the plots table, and an Are You Sure? question before an application is withdrawn. Which element suits each one?
  1. The panel appears in the middle of the window, as before. The browser’s own styles center an open popover, wherever its button sits.
  2. A popover suits the notice, since the table stays usable behind it. A modal <dialog> suits the question, since the page stays inert behind it until the visitor answers or dismisses it, and opening it needs JavaScript or invoker commands.

The home page now shows its opening hours from a button in the header, in a panel that closes as soon as the visitor moves on. Rosa’s plots page needs the same kind of help. In the next exercise, Exercise: Add Help to the Plots Page, you’ll give it a button that explains what a plot is, plus two questions that open one at a time.

It’s the course’s first exercise. You write the code right in the browser and run it. Automated tests tell you what works. Run the tests early and often with Run Tests, read each failure for clues, and use Full screen for more room.

Become a Member to join the conversation.