↓ Ir para o conteúdo principal

← todas as notas

📎 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: cover is 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-label in place of alt.
  • The container uses padding-bottom to create a 16:9 ratio.
  • object-fit: cover is described as the newer option and is not available for older IE versions without a polyfill.
  • The img version keeps standard image semantics and uses alt instead of aria-label.
  • The img is constrained with height, and object-fit: cover centers and crops it within the container.
  • Use background-size when the image is decorative, when the container needs extra content, or when pseudo-elements and other background effects are needed.
  • Use object-fit when a normal img fits the context and its semantics should be preserved.