Center text over an image with CSS

To center text over an image, set position: relative on their wrapper and give the caption position: absolute; top: 50%; left: 50%. Add transform: translate(-50%, -50%) to align the caption’s center with the image’s center.

Prepare the sample image

Save the following markup in UTF-8 as sample.svg. It is a 640×360 landscape with a 16:9 ratio. If you swap in another image, update src, set the width and height attributes to match its dimensions, and choose suitable alt text.

<svg xmlns="http://www.w3.org/2000/svg" width="640" height="360" viewBox="0 0 640 360">
  <rect width="640" height="360" fill="#a8d6eb" />
  <circle cx="490" cy="85" r="48" fill="#f6bd4f" />
  <path d="M0 360 L210 110 L420 360 Z" fill="#168b85" />
  <path d="M250 360 L460 160 L640 360 Z" fill="#215d8a" />
</svg>

Run the absolute positioning example

Save the HTML as text-on-image.html and open it from the same folder as sample.svg. If the picture is missing, check the file name and extension. No external resources or JavaScript are required.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Center text over an image</title>
  <style>
    body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
    * { box-sizing: border-box; }
    h1 { font-size: 24px; }
    .hero { position: relative; max-width: 720px; margin: 0; }
    .hero > img { display: block; width: 100%; height: auto; }
    .caption { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
      width: calc(100% - 32px); max-width: 520px; margin: 0; padding: 16px;
      background: rgb(0 0 0 / 0.6); color: white; text-align: center; }
    .caption h2 { margin: 0; font-size: clamp(18px, 3vw, 28px); line-height: 1.4; }
    .caption p { margin: 8px 0 0; }
  </style>
</head>
<body>
<h1>A centered image caption</h1>
<figure class="hero">
  <img src="sample.svg" width="640" height="360" alt="Sample landscape with a yellow sun and green and blue mountains">
  <figcaption class="caption">
    <h2>Centered on the image</h2>
    <p>The caption stays centered as the image resizes.</p>
  </figcaption>
</figure>
</body>
</html>

The image fills the wrapper and keeps its aspect ratio. The caption appears in a translucent black box in the middle. Because the caption is separate HTML text, you can edit it without touching the image file.

Compare the two percentage references and the Grid option

The percentages in top and left place the caption’s top-left corner at the center of its containing block. The percentages in translate() use the caption’s own box, so the negative halves bring its center into alignment. Disable just the transform declaration to see the offset, then restore it. See CSS Positioned Layout · Box Insets and CSS Transforms · translate().

To try Grid, add the rules below at the end of the existing style element. They place the image and caption in the same cell, reset the absolute offsets and transform, and use place-self: center to center the caption in the cell. See CSS Box Alignment · place-self.

.hero { display: grid; }
.hero > img, .hero > .caption { grid-area: 1 / 1; }
.hero > .caption {
  position: static;
  top: auto;
  left: auto;
  transform: none;
  place-self: center;
}

When the caption fits within the image height, both approaches center it in the same place. A taller caption may make the grid row grow or extend beyond the image when positioned absolutely. Remove the added Grid rules to restore the original layout after comparing.

Keep the caption readable on small images

Here the caption is at least 32px narrower than the image and the font size adapts. If a long caption becomes hard to read on a narrow screen, keep a short heading over the image and put the details below it. Keep the overlay visible without requiring hover.

The alpha in rgb(0 0 0 / 0.6) makes only the background translucent and keeps the text white. Check readability against light and dark parts of your photo. If the caption is off center, check that the two elements share the relatively positioned wrapper and that another rule has not overridden transform.