How to Reveal Text on Hover with CSS

To show supporting text when someone points to an element, style the text inside a parent that matches :hover. Add :focus-within so a keyboard user can reveal it by focusing the link. The examples compare a caption that expands the layout with one that overlays a decorative panel.

Choose how the text should appear

Use display: none and display: block when it is fine for the content below to move. To preserve the surrounding layout, position the caption with position: absolute inside a reference element and use opacity for a fade. Keep essential information or long explanations visible by default.

Copy and run the HTML and CSS

Save the HTML as index.html and the CSS as style.css in the same folder. Open the HTML file in your browser. Each link leads to the note at the end of the demo. You can compare the reveal behavior by pointing or pressing Tab without activating the links.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>CSS hover examples</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <h1>Two ways to reveal text</h1>
  <div class="demo">
    <a href="#notes">Read the expanding caption notes</a>
    <p class="caption caption--flow">The caption takes up space below.</p>
  </div>
  <div class="demo">
    <a href="#notes">Read the overlay caption notes</a>
    <div class="demo-art">
      <p class="caption caption--overlay">The caption appears over the decorative panel.</p>
    </div>
  </div>
  <p id="notes">Compare the position of the text below.</p>
</body>
</html>
* {
  box-sizing: border-box;
}

body {
  margin: 1rem;
  font: 1rem/1.5 system-ui, sans-serif;
  color: #172033;
}

.demo {
  max-inline-size: 28rem;
  margin-block: 1rem;
  padding: 1rem;
  border: 1px solid #cbd5e1;
  border-radius: 0.75rem;
}

.demo a:focus-visible {
  outline: 2px solid #1d4ed8;
  outline-offset: 3px;
}

.demo-art {
  position: relative;
  display: grid;
  align-items: end;
  min-block-size: 9rem;
  margin-block-start: 0.75rem;
  border-radius: 0.5rem;
  background: linear-gradient(135deg, #bfdbfe, #ced5ff);
}

.caption {
  margin: 0.75rem 0 0;
  padding: 0.75rem;
  border-radius: 0.5rem;
  background: #172033;
  color: #fff;
}

.caption--overlay {
  margin: 0.5rem;
}

@media (hover: hover) {
  .caption--flow {
    display: none;
  }

  .demo:hover .caption--flow,
  .demo:focus-within .caption--flow {
    display: block;
  }

  .caption--overlay {
    position: absolute;
    inset-inline: 0.5rem;
    inset-block-end: 0.5rem;
    margin: 0;
    opacity: 0;
    visibility: hidden;
    transition: opacity 160ms ease;
  }

  .demo:hover .caption--overlay,
  .demo:focus-within .caption--overlay {
    opacity: 1;
    visibility: visible;
  }
}

@media (prefers-reduced-motion: reduce) {
  .caption--overlay {
    transition: none;
  }
}

Expand the layout with display

.caption--flow participates in normal document flow. On a hover-capable device, display: none removes its space and display: block restores it, moving the content below. If you applied :hover only to the link, the caption could vanish as you move toward it. The demo instead matches .demo, which contains both. W3C selectors for hover and focus

Overlay the caption with position and opacity

position: relative on .demo-art supplies the positioning reference. With hover available, the demo absolutely positions .caption--overlay at the bottom of the decorative panel. The caption is outside normal flow, so revealing it does not push the following paragraph down. The panel keeps it clear of links and other text.

opacity: 0 alone makes an element transparent but can leave it as a pointer target. The demo also uses visibility: hidden when hidden, then restores visibility: visible and sets opacity: 1 to reveal it. The transition fades the caption in, and turns off for people who request reduced motion. Reduced motion media feature

Test mouse, keyboard, and touch

@media (hover: hover) hides the captions only when the primary input can hover conveniently. With a touchscreen as the primary input, or whenever the query is false, both captions start visible. Use the input capability to decide whether to enable the hover behavior, since screen width alone cannot tell you that. Hover capability of the primary input

Point to each card, move onto its caption, then leave the card. Use Tab to reach each link and check for both a focus outline and a caption. On a touchscreen or in touch emulation, check that both captions are visible before interacting.

If a popup covers other content or contains controls of its own, design a way to dismiss and operate it. Consider native <details> when visitors need an explicit open-and-close control. WCAG explains when additional content needs to remain visible or be dismissible in Content on Hover or Focus.