CSS background-size: cover and background-attachment: fixed Clipping Background Images
Problem:
In a list of figures with background images set to "cover" and "fixed," the images are clipped when the figure is offset from the viewport.
Explanation:
This behavior is inherent to the way "fixed" positioning works in CSS. "Fixed" positioning removes the background image from the element's positioning context and aligns it with the viewport. As a result, the "cover" value in "background-size: cover" is calculated relative to the viewport, not the element itself.
Proposed Solution:
Using "fixed" positioning and "cover" for background images is not possible with pure CSS.
Alternative Solution:
Instead of "fixed" positioning, use "scroll" for background-attachment and bind an event listener to the scroll event in JavaScript. This manually updates the background position based on the window's scroll distance, simulating fixed positioning while maintaining "cover" relative to the container element.
The above is the detailed content of Why Are My Fixed Background Images Clipped When Using `background-size: cover`?. For more information, please follow other related articles on the PHP Chinese website!