Put the image and text in the same wrapper, set the wrapper to position: relative, then position the text over it. Use an HTML <img> when the image carries meaning; use a CSS background when it is decorative. The choice affects accessibility, cropping, and how you keep the text readable.
Use a positioned wrapper for an image element
Make the intended parent the positioning context. The absolutely positioned text is then placed relative to that wrapper rather than an unrelated ancestor. Because absolute positioning removes the text from normal document flow, the image or another explicit sizing rule must give the wrapper a dependable size.
<div class="hero">
<img class="hero__image" src="scene.jpg" alt="A hiker looking across a mountain valley">
<h1 class="hero__title">Find your next trail</h1>
</div>
.hero {
position: relative;
overflow: hidden;
}
.hero__image {
display: block;
width: 100%;
height: 24rem;
object-fit: cover;
object-position: center;
}
.hero__title {
position: absolute;
inset: auto 1.5rem 1.5rem;
margin: 0;
color: white;
background: linear-gradient(transparent, rgb(0 0 0 / 70%));
padding: 3rem 1rem 1rem;
}
Here, inset: auto 1.5rem 1.5rem leaves the top edge unset and places the title 1.5rem from the wrapper’s left and bottom edges. The image fills a fixed-height frame; object-fit: cover crops it to fill, while object-position determines which part remains visible. This is a starting pattern, so adjust the frame, crop, and placement to suit the design. See MDN’s positioning reference and image fit guidance.
Choose between an img and a CSS background
Use an img when the picture conveys information
An image of a product, person, place, or meaningful event should generally be an HTML <img> with appropriate alternative text. It has a text alternative for assistive technology, and its dimensions or aspect ratio can reserve space as the page loads. Use object-fit and object-position if it should crop within a set frame. The alternative text should describe the image’s relevant information, not repeat the overlaid heading.
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
Use a background image for decoration
A CSS background-image is suitable when the image is decorative and the page’s meaning is already expressed in real HTML text. Background images do not have a built-in text alternative and are not announced as meaningful images to screen readers. Add a fallback color so the text remains readable if the image does not load.
.hero--decorative {
color: white;
background-color: #263238;
background-image:
linear-gradient(to top, rgb(0 0 0 / 70%), transparent 65%),
url("scene.jpg");
background-size: cover;
background-position: center;
}
In a layered background, the gradient is listed before the image so it sits above it. Adjust the gradient direction and stop to place contrast where the text actually appears. MDN’s background-image guidance explains background layers and accessibility considerations.
Rank #2
Make the overlay responsive and readable
Absolute placement is useful when text belongs at a particular point on an image, but it does not reserve space for that text. Constrain its width, allow wrapping, and use spacing that can shrink. Check longer headings and translated text, not just the short sample. At narrow widths, move the text, reduce the image frame, or let the text stack in normal flow if the overlay no longer fits.
- Test the crop at each important viewport: a centered crop can cut off the subject or leave text over a busy part of the image.
- Check enlarged text and zoom. Positioned content should not obscure other content or overflow its container.
- Use a gradient or solid panel only as needed, and verify the contrast where the letters actually sit. A treatment that works on one crop may fail on another.
- When image dimensions are known, provide width and height or an aspect ratio to reserve predictable space while it loads.
Check contrast and accessibility
Keep the words as selectable HTML text rather than baking them into the image. Real text can adapt to user-agent presentation settings; text embedded in an image cannot adapt in the same way. W3C describes this in its CSS technique for text over images.
Rank #3
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
MDN’s accessibility guidance gives WCAG contrast thresholds of 4.5:1 for ordinary text and 3:1 for large text (at least 24px, or bold text of approximately 18.66px or larger). These are standards guidance, not a guarantee that a particular gradient will pass: assess the foreground against the actual area behind the text. See MDN’s background-image accessibility guidance.
Troubleshoot common overlay problems
The text appears in the wrong place
Check that the wrapper containing both the image and overlay has position: relative. Without that positioning context, the absolute element may be placed relative to a different ancestor or the initial containing block.
Rank #4
The wrapper collapses or the image leaves a gap
An absolutely positioned overlay does not contribute to normal-flow sizing. Give the wrapper a reliable size through the image, an aspect ratio, or an explicit height. Set the image to display: block to avoid the inline-image baseline gap.
The text is clipped or overflows on mobile
Review fixed heights, padding, and offsets at narrow widths. Allow the heading to wrap, reduce its maximum width, or change to a stacked layout; remove or adjust overflow: hidden if it clips necessary content.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Best Value
The image subject is obscured or the text is hard to read
Change object-position for an <img> or background-position for a background to improve the crop. Then tune the gradient or use a solid panel behind the text and recheck contrast over the resulting image.
Or skip the browser setup
If you need a screenshot of a page showing this layout, ScreenshotNeo can return an image or PDF with one GET request. For example, this cURL call saves a WebP screenshot of the page:
Quick Recap
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 request options. It removes cookie banners, popups, and chat widgets before capture; bot checks, blank pages, failed loads, timeouts, and cache hits are not billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000.
Sign up for ScreenshotNeo’s free plan.
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.
Recommended Free Tools




