The Problem With Background Images
CSS lets you pin a background image to the viewport with background-attachment: fixed. Because the technique sees little use, the result can read as striking when applied sparingly. The catch is that it hinges on background-image, which treats the asset as decoration rather than content.
For an inline <img>, the trade-offs run the other way. The tag brings its own advantages:
- Lazy loading is straightforward (e.g.
<img loading="lazy">). alttext improves both SEO and accessibility.srcset/sizescan tune loading performance.- The
<picture>element can serve the best size and format for a given browser. - Users can save the image without opening DevTools.
So an inline tag is preferable wherever an image counts as content. In working out how to get the fixed effect that way, two approaches surface: the background-image method and a clip-path technique applied to an <img>.
The background-image Approach
.hero-section {
background-image: url("nice_bg_image.jpg");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
background-attachment: fixed;
}
This works, but only while the image spans the full viewport width or sits centered. An image placed at the left or right of the page produces positioning headaches, since background-position is measured from the center of the viewport; correcting it takes several media queries to hold the placement across devices.
The clip-path Trick
A Stack Overflow answer documents a clip-path approach that preserves the <img> tag and produces the fixed scrolling effect.
.image-container {
position: relative;
height: 200px;
clip-path: inset(0);
}
.image {
object-fit: cover;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
The drawbacks are real, though. The snippet is lengthy for what it accomplishes, and clipping comes with its own constraints:
border-radius: 10px;will not round the image corners; the rounding has to be drawn into the clip path itself.- Positioning the image within the
clip-pathis not obvious. It may require deep familiarity with clip paths or pre-cropping the source image.
The author of the original write-up ultimately abandoned the effect on inline images and went back to background-image, acknowledging that choice as limiting.



