Responsive styles and focus

W3C CSS Snapshot 2026

A responsive layout adapts to the space it has. Start with a usable narrow layout, then add rules when there is more room. Keyboard users also need a visible indicator of which control is active.

On this page

When does a media query apply?

A media query is a condition about the browser environment. @media (min-width: 600px) applies its rules when the viewport is at least 600 CSS pixels wide. The viewport meta line in the example lets a phone use its device width for layout.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>When does a media query apply?</title>
  <style>
    .cards { display: grid; gap: 10px; }
    .cards > div { background: #d6efea; padding: 8px; }
    @media (min-width: 600px) { .cards { grid-template-columns: 1fr 1fr 1fr; } }
  </style>
</head>
<body>
<div class="cards"><div>Pen</div><div>Notebook</div><div>Folder</div></div>
</body>
</html>

What to expect Below 600px the cards stack. At 600px and above they share a row. Use the DevTools device toolbar or resize the window.

Respond to a component’s own space

Container queries check a containing element rather than the whole viewport. container-type: inline-size enables width queries for this wrapper. The query changes a descendant inside the wrapper. This is useful when an identical component can live in a main area or in a narrow sidebar.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Respond to a component’s own space</title>
  <style>
    .panel { container-type: inline-size; width: 100%; max-width: 480px; }
    .cards { display: grid; gap: 8px; }
    .cards > div { background: #d6efea; padding: 8px; }
    @container (min-width: 360px) { .cards { grid-template-columns: repeat(3, 1fr); } }
  </style>
</head>
<body>
<section class="panel"><div class="cards"><div>Pen</div><div>Notebook</div><div>Folder</div></div></section>
</body>
</html>

What to expect At a panel width of 360px and above the cards sit in a row. Change max-width to 300px to stack them even in a wide browser.

Show keyboard focus

Focus is the current keyboard destination. Press Tab to move between controls. :hover matches a pointer resting on a control. :focus-visible lets you style focus when a visible indicator is appropriate, such as keyboard navigation.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Show keyboard focus</title>
  <style>
    button:hover { background: #d6efea; }
    button:focus-visible { outline: 3px solid purple; outline-offset: 3px; }
  </style>
</head>
<body>
<button type="button">Pen</button> <button type="button">Notebook</button>
</body>
</html>

What to expect Moving the mouse over a button changes its background. Press Tab to see the purple focus ring move.

Official references