📎 Webclip
CSS-Only Full-Width Responsive Images 2 Ways
The post compares two ways to build full-width responsive images with CSS. One uses background-size: cover on a container with a background image and a set aspect ratio. The other uses object-fit: cover on a standard img, with height used to constrain the image.
Reading notes#
background-size: coveris presented as the older option, widely used before broad support for newer CSS image handling.- The background-image version places the image in the element’s style attribute and uses
aria-labelin place ofalt. - The container uses
padding-bottomto create a 16:9 ratio. object-fit: coveris described as the newer option and is not available for older IE versions without a polyfill.- The
imgversion keeps standard image semantics and usesaltinstead ofaria-label. - The
imgis constrained withheight, andobject-fit: covercenters and crops it within the container. - Use
background-sizewhen the image is decorative, when the container needs extra content, or when pseudo-elements and other background effects are needed. - Use
object-fitwhen a normalimgfits the context and its semantics should be preserved.
