Web design and digital-business ideas grounded in real tasks.

CSS focus-within can show the context around a focused control

A laptop displaying a clean form with one input outlined and its enclosing section softly highlighted.
AI-generated photographic illustration; not documentary photography.

A keyboard user can benefit from seeing both the active control and the group it belongs to. CSS :focus-within lets a container respond when focus is on the container itself or one of its descendants.

MDN’s reference describes the selector and its matching behaviour. It changes styling; it does not move focus, make a non-interactive element operable or give a group a meaningful accessible name.

Highlight a relationship that already exists

Consider a contact form with an address fieldset containing street, city and postcode inputs. A subtle border around the fieldset can help show which section is active. The individual input still needs its own clear focus indicator, so the group styling should support rather than replace it.

A small rule can express the idea:

.address-group:focus-within {
  border-color: #245b9b;
  background-color: #f1f6fc;
}

This example assumes an existing visible border and suitable contrast in the surrounding design. It is a starting point for testing, not a complete accessible form. Labels, instructions, error messages and keyboard behaviour need their own attention.

Try the whole journey

Move through the form using the keyboard. Check that the group highlight appears when you enter its first control, remains while you move within the group and ends when focus leaves. Confirm that the active input is still easy to identify when several fields share the highlighted container.

Test narrow screens and any supported high-contrast or forced-colour settings. Do not rely on a very faint background change as the only useful signal. Avoid adding a thick border only on focus if it shifts the layout and makes neighbouring content jump.

The best use is modest: make an existing relationship easier to perceive. When the styling calls more attention to the container than to the actual task, reduce it. Context should help people complete the form, not compete with the control they are using.