8 minutes read. Quick Summary – This article will explain how background-size and object-fit work. It also includes some useful use cases and suggestions. Let’s get started. It is not possible to load images of different sizes for an HTML element. The image may be stretched or compressed if the width and height are not proportional to its aspect ratio. This is not a good thing. It can either be fixed with object-fit to an img element, or using background-size. Let’s first define the problem. Take the following example: Consider the following image. A nice-looking photograph gets compressed when it is used as a component of a card. This is why it happens. A browser will automatically fill the box with an image that has the correct aspect ratio. The result of an image with an aspect ratio other than its width or height will either be stretched or squeezed. This is illustrated in the following image: If the aspect ratio of the image differs from that of the containing element, the image will be stretched. (Large Preview) The solution You don’t have to always add an image of a different size if the aspect ratio is not equal or in line with the width and height of the containing elements. Let me show you how I used to do it in photo-editing software. First we would center vertically and then add a mask. It preserves the image’s aspect ratio, and stops it being compressed. (Large Preview) Once we have a better understanding of how it works, let us now look at how the browser works. This is a much simpler process! CSS object-fit CSS The object-fit property determines how the replaced element, such as video or img should be resized in order to fit into its container. Fill is the default value of object-fit. This can cause an image to be stretched or squeezed. We’ll now look at the possibilities. Continue reading! Possible Values for object-fit object-fit: contain In this case, the image will be resized to fit the aspect ratio of its container. Letterboxing will occur if the aspect ratio of the image does not match that of the container. The image that uses object-fit is contained will either be letterboxed, or resized. (Large Preview) object-fit. Cover Here the image is resized according to the dimensions of the container. If the image’s dimension does not match that of the container it will be cut to make the image fit. The object-fit cover option will allow you to either crop the image or adjust its size accordingly. (Large Preview) object-fit. Fill. This will resize the image to match its container’s aspect ratio. If the ratio is not correct, it will either be squeezed or stretched. This is not what we want. The image is stretched or squeezed when object-fit:fill is used. (Large Preview) object-fit no In this instance, the image will not be resized, stretched, or squeezed. Although it works as the cover value, it does not respect its container’s aspect proportion. If the dimensions of the object-fit property is not used, it won’t resize the image. (Large Preview) In addition to object-fit, there is also the object-position property. This allows you to position an image inside its container. There are several possible values for object-position. The object-position property is similar to CSS’ background position property. Usually, the default value (i.e. center, 50%`). (Large Preview) Top and bottom keywords work when the aspect ratios of the containing boxes are vertically greater. Compare object-position (left), and object-position (right). CSS background-size (Large Preview) The first thing to note about background-size is that it’s the background and not the HTML (img). Background-size Values Possible The background-size options are cover, contain and auto. Background-size: auto The default setting for background size is auto. If the size of the image is too small, it may result in blurred images. Background-size: Cover (large preview) The image will then be reduced to fit into the container. The image will be mask to make it fit if the aspect ratios of the images are different. Background-size cover covers should be considered when determining the image’s aspect ratios. Background-size: cover (large preview) In this instance, the image is resized in order to fit into the container. The image will then be letterboxed if the aspect ratios differ. Background-position is similar to object-position. Only difference: object-position’s default position is not the same as background-position. What to Avoid: Background-size or object-fit? If an element, or image, is set at a fixed height, and then has either a background-size cover or object fit: cover, it will become too large. This can cause the image to lose important details that could affect the way the user views the image. Take the following image as an example. The image has been given a fixed size. Large preview This is because the aspect ratio we specify is not being specified. This can be fixed by one of the two options. To create an intrinsic ratio, the first option is to use padding hack. .card__thumb position: relative; padding–bottom: 75%; width: 100%; Height: 100%; cover:; The second is to use new aspect-ratio. CSS property. It can be used to do:.card_thumb img: aspect-ratio 4/3;. Note: The aspect-ratio CSS property has been covered in great detail by me: “Let’s Learn About Aspect Ratio in CSS”. Example Use Cases and Examples of User avatars Cover is a great use case for object fit: user avatars. Avatars are often rectangular in aspect. The image could be distorted if it is placed in a rectangular container. Comparison of an avatar with and without object-fit. Large preview This is a common use of logos. We need to be able to change the size of logos without causing distortion. The solution is object-fit: include. .logo__img: width: 150px height: 80px object-fit; We can resize logos for clients using object-fit; contain. Large preview of Article Thumbnail. This is an extremely common usage case. An article thumbnail container might have an image that has a different aspect ratio. The content management system (CMS), should fix this issue in the first instance, however it doesn’t always. Article__thumb; object-fit cover. Adjusting article thumbnails using a little help from object fit: cover. (Large Preview) Hero Background. In this case, choosing whether an img or CSS background is a decision that will be made based on: Does the image matter? Would we like the user to be able to view the image if CSS was disabled? Is the purpose of the image purely decorative? We can then decide what feature we will use based on this answer. Consider the importance of the image. Let’s say that it is a website about food. (Large preview)
.hero position: relative; .hero__thumb position: absolute; left: 0; top: 0; width: 100%; height: 100%; object-fit: cover; If the image is decorative, we can go with background-image: .hero position: relative; background-image: linear-gradient(to top, #a34242, rgba(0,0,0,0), url(“https://www.smashingmagazine.com/2021/10/object-fit-background-size-css/thumb.jpg”); background-repeat: no-repeat; background-size: cover; The CSS is shorter in this case. Any text that is placed on top of the image should be easily readable. You can also add background to your image with object-fit. This would also be useful when using object-fit. As you can see, there is a grid consisting of multiple images. The background color will be displayed if the aspect ratios for the image and container differ. img: object-fit; background-color is #def4fd. To add background to images, you can use object fit: contain. Video Element (Large Preview) Have you ever used a video background to enhance your image? You probably desired the video to be as wide and tall as its parent. .heroposition: relative; background_color: #def4fd..hero__videoposition: aboslute. Width: 100%; Height: 100%;..hero__video’s default object-fit is contain. The video does not cover the hero’s background even though its position is absolute, width 100%,, and height 100%.. (Large Preview) In order to make the video cover its parent’s width and height, it is necessary to change the default object fit value. (Large Preview) Conclusion Both object-fit as well as background-size can be very helpful in handling various aspect ratios. These CSS options are very useful because we don’t have the ability to control every aspect ratio of an image. An easy reminder about the accessibility implications when choosing between an img and a CSS Background: A CSS background is best if the image is only decorative. An img would be more appropriate. This article was helpful, I hope. We appreciate your time. (vf, yk, il, al)







