← Back to HTML & CSS, page 4

html-css-0070 · Medium mid

How do :target and :checked let you build interactive effects without JavaScript, and when should you not?

:target matches the element whose id is in the page address fragment, and :checked matches checked inputs, so CSS can react to them with no script. They are useful for highlighting and custom form controls, but menus and dialogs built from them have accessibility and history problems.

Full answer

Some CSS selectors match state that the browser already tracks, so CSS can react without scripts.

:target matches the element whose id equals the fragment in the page address. When the address ends in #faq, the element with id="faq" matches :target. It is handy for highlighting the section a link points to, or for simple in-page tabs.

:checked matches a checked checkbox or radio button, and a selected option. With the sibling combinators + and ~, a hidden checkbox can toggle styles on later elements. This is called the checkbox hack. :has() allows the reverse, such as styling a parent when its checkbox is checked.

Both techniques have costs.

  • :target changes the address and adds a history entry, and the browser scrolls to the element. That may be unwanted for a menu or a popup.
  • A checkbox used as a toggle is read by screen readers as a checkbox, not as a menu button, and it has no aria-expanded state.
  • Content that appears in CSS only is easy to build and hard to make fully accessible.

For real menus, dialogs and accordions, prefer details, dialog or a button with a small script. Use :target for highlighting and :checked for form controls styled by the state they already have.

After clicking the Fees link, the address ends in #fees and that section gets a blue left border and a light background. The radio inputs are visually hidden but still focusable. The label next to the checked one turns blue, and a keyboard user sees an outline on the label when its input has focus.

Note that the hidden input uses opacity: 0 and not display: none. With display: none the control would leave the keyboard order.

html
<style>
  section { padding: 8px; border-left: 4px solid transparent; }
  section:target { border-left-color: #0a6cff; background: #eef4ff; }
  .opt input { position: absolute; opacity: 0; }
  .opt label { display: inline-block; padding: 4px 12px; border: 1px solid #999; cursor: pointer; }
  .opt input:checked + label { background: #0a6cff; color: #fff; }
  .opt input:focus-visible + label { outline: 3px solid #0a6cff; outline-offset: 2px; }
</style>
<nav><a href="#fees" id="l1">Fees</a> <a href="#dates" id="l2">Dates</a></nav>
<section id="fees">Fees information</section>
<section id="dates">Dates information</section>
<div class="opt">
  <input type="radio" name="plan" id="p1" checked><label for="p1" id="lab1">Monthly</label>
  <input type="radio" name="plan" id="p2"><label for="p2" id="lab2">Yearly</label>
</div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • :target matches the element named by the URL fragment.
  • :checked with + or ~ lets a control style later elements.
  • Visually hide inputs but keep them focusable.
  • :target changes history and scrolls the page.
  • Prefer details, dialog or a button for menus and popups.

Common mistakes

  • Hiding the radio or checkbox with display: none, which removes it from keyboard focus.
  • Building menus that open with :target, which scroll the page and fill the history with entries.
  • Using a checkbox as a menu toggle without any accessible name or state for screen reader users.

Follow-ups

  • Why use opacity: 0 instead of display: none on the hidden input?
  • What does :has() add to the checkbox hack?

Tips

  • Say what each selector reads: :target reads the fragment, :checked reads a control's state.
  • Give one good use for each, and one case where you would use a different tool.