Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Add Text Over an Image in React

Use a shared React wrapper with relative positioning and place live JSX text over the image with absolute positioning. See sizing, accessibility, and troubleshooting tips.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Put the image and text in the same wrapper, make that wrapper position: relative, and position the text with position: absolute. React renders both elements; CSS places the text over the image. This web React pattern keeps the words as selectable, accessible HTML text rather than baking them into the image.

Build the overlay in React

Use a wrapper as the positioning reference. The text can be a caption, heading, button, or other JSX content.

function ImageWithText({ src, alt, children }) {
  return (
    <div className="image-overlay">
      <img className="image-overlay__image" src={src} alt={alt} />
      <div className="image-overlay__text">{children}</div>
    </div>
  );
}

Render it with the image URL, appropriate alternative text, and the content to overlay:

<ImageWithText src="/images/mountain.jpg" alt="Snow-covered mountain above a lake">
  <h2>A view worth the climb</h2>
</ImageWithText>

React uses className to assign CSS classes. Keep the layout rules in a stylesheet:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
.image-overlay {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  overflow: hidden;
}

.image-overlay__image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-overlay__text {
  position: absolute;
  inset: auto 1rem 1rem;
  color: white;
  background: rgb(0 0 0 / 65%);
  padding: 0.75rem 1rem;
}

The 16 / 9 aspect ratio is just an example; choose a shape that fits the design. The wrapper remains in normal document flow, while its positioned text is removed from normal flow and placed relative to that wrapper. Without position: relative on the intended wrapper, the text may instead align to a different positioned ancestor.

Fit the image without distorting it

The CSS above sizes the image to the wrapper, then uses object-fit to decide how the image fills that box. Choose based on whether cropping or empty space is acceptable:

  • object-fit: cover fills the box while preserving the image’s proportions, but may crop its edges.
  • object-fit: contain shows the complete image, preserving its proportions, but may leave unfilled space in the box.

Avoid relying on the default fill behavior when the image and box have different proportions: it can stretch and distort the image. object-fit affects a resized image element, so set its rendered width and height as in the example.

Keep the overlay readable and accessible

Use real text and check contrast

Keep the overlay words in JSX rather than drawing them into the image. Actual text can be selected and styled, and it scales better than text embedded in an image. Check contrast where the text appears on the actual photo: WCAG 2.2 guidance specifies at least 4.5:1 for normal text and 3:1 for large text, subject to the criterion’s exceptions. A translucent panel or gradient can help maintain contrast over varied backgrounds, but verify the resulting text/background combination.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose alternative text for the image itself

The image and overlay caption are separate content. For an informative image, give alt a concise description of the meaningful image content; do not merely write “image” or repeat the overlaid caption. If the image is decorative and the overlay already provides the relevant information, use alt="" so assistive technology can skip it.

When the words must become part of an image

For ordinary page content, a live HTML overlay is usually the more flexible choice. Text burned into an image or drawn on a canvas becomes image-like output, not ordinary text. Embedded text can be harder to scale and to adjust for foreground and background needs. If a canvas is used to produce a single composed graphic, provide an accessible description—for an image-like canvas, MDN recommends role="img" with an aria-label.

Troubleshoot common overlay problems

  • Text appears in the wrong place: ensure the wrapper containing both elements has position: relative, and that the text is positioned absolutely. Check for another positioned ancestor if it still uses the wrong reference.
  • Text and image drift apart when the layout changes: keep both inside the same wrapper and size the image to that wrapper.
  • The image looks stretched: set the image’s rendered dimensions and choose cover if cropping is acceptable or contain if all image content must remain visible.
  • The caption disappears against part of the photo: check contrast over the exact region behind the text and add or adjust a backing panel or gradient.
  • A screen reader announces the wrong thing: decide whether the image conveys information independently of the overlay, then provide descriptive alt text or alt="" for a decorative image.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If your goal is a screenshot of a page that already contains the React overlay, ScreenshotNeo can capture the rendered page with one API request. It does not create the React component or replace the CSS implementation above.

For example, capture a public page as WebP:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for parameters and response details. ScreenshotNeo removes cookie banners, popups, and chat widgets before the shot; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The Free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000. Learn about ScreenshotNeo, then sign up free.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.