The CSS property that helps maintain spacing in responsive layouts.
Aspect ratio
Aspect ratio is most commonly expressed as two integers and a colon in the dimensions of: width:height, or x:y. The most common aspect ratios for photography are 4:3 and 3:2, while video, and more recent consumer cameras, tend to have a 16:9 aspect ratio.
With the advent of responsive design, maintaining aspect ratio has been increasingly important for web developers, especially as image dimensions differ and element sizes shift based on available space.
Some examples of where maintaining aspect ratio become important are:
- Creating responsive iframes, where they are 100% of a parent's width, and the height should remain a specific viewport ratio
- Creating intrinsic placeholder containers for images, videos, and embeds to prevent re-layout when the items load and take up space
- Creating uniform, responsive space for interactive data visualizations or SVG animations
- Creating uniform, responsive space for multi-element components such as cards or calendar dates
- Creating uniform, responsive space for multiple images of varying dimension (can be used alongside
object-fit)
Object-fit
Defining an aspect ratio helps us with sizing media in a responsive context. Another tool in this
bucket is the object-fit property, which enables users to describe how an object (such an as image)
within a block should fill that block:
object-fit values. See demo on Codepen.The initial and fill values re-adjust the image to fill the space. In our example, this causes
the image to be squished and blurry, as it re-adjusts pixels. Not ideal. object-fit: cover uses
the image's smallest dimension to fill the space and crops the image to fit into it based on this
dimension. It "zooms in" at its lowest boundary. object-fit: contain ensures that the entire image
is always visible, and so the opposite of cover, where it takes the size of the largest boundary
(in our example above this is width), and resizes the image to maintain its intrinsic aspect ratio
while fitting into the space. The object-fit: none case shows the image cropped in its center
(default object position) at its natural size.
object-fit: cover tends to work in most situations to ensure a nice uniform interface when dealing
with images of varying dimensions, however, you lose information this way (the image is cropped at
its longest edges).
If these details are important (for example, when working with a flat lay of beauty products), cropping important content is not acceptable. So the ideal scenario would be responsive images of varying sizes that fit the UI space without cropping.