Background size mobile CSS

The background-size property in CSS is one of the most useful — and most complex — of the background properties. There are many variations and different syntaxes you can use for this property, all of which have different use cases. Here’s a basic example:

html {
  background: url(greatimage.jpg);
  background-size: 300px 100px;

That’s an example of the two-value syntax for background size. There are four different syntaxes you can use with this property: the keyword syntax, the one-value syntax, the two-value syntax, and the multiple background syntax.


In addition to the default value (auto), there are two keywords you can use with background-size: cover and contain

The difference

cover tells the browser to make sure the image always covers the entire container, even if it has to stretch the image or cut a little bit off one of the edges. contain, on the other hand, says to always show the whole image, even if that leaves a little space to the sides or bottom.

The default keyword — auto — tells the browser to automatically calculate the size based on the actual size of the image and the aspect ratio.

One value

If you only provide one value (e.g.

html {
  background: url(greatimage.jpg), url(wonderfulimage.jpg);
  background-size: 300px 100px, cover;
  /* first image is 300x100, second image covers the whole area */
0) it counts for the width, and the height is set to auto. You can use any CSS size units you like, including pixels, percentages, ems, viewport units, etc.

Two values

If you provide two values, the first sets the background image’s width and the second sets the height. Like the single value syntax, you can use whatever measurement units you like.

Multiple images

You can also combine any of the above methods and apply them to multiple images, simply by adding commas between each syntax. Example:

html {
  background: url(greatimage.jpg), url(wonderfulimage.jpg);
  background-size: 300px 100px, cover;
  /* first image is 300x100, second image covers the whole area */

Keep background image stacking order in mind when using multiple images.


This demo shows examples of cover, contain, and multiple background images with a mix of pixel and keyword values.

That every website that wants to stand a chance of being successful needs to implement a responsive design.

However, given that most modern websites have rich, layered designs that incorporate text, image, and video to attract, communicate with and retain users, implementing responsive design might not be too easy.

Using the right image is integral to creating an aesthetically pleasing and relevant website design. Nobody wants to skim through blocks of text with no image in between. Images depict, clarify and make information more accessible and understandable. Thus, no matter the device from which a website is being accessed, images have to render correctly and presentably. In other words, web developers must make images responsive.

This article will explore and describe the process by which web developers can create responsive images.

What is a Responsive Image?

Before wondering how to make images responsive, it’s worth taking the time to understand what a responsive image is.

Quite simply, a responsive image responds and adjusts to different screen sizes. In real-world usage, that means that the image should have the following attributes/abilities:

  • The image should be able to render crisply at different device-pixel-ratios. That means high-resolution screens display high-resolution images, but low-res screens should not have to deal with the extra pixels.
  • In the case of a fluid (responsive) layout, images need to stretch or squeeze to align with its changes.
  • For both the above cases, images will have to be made responsible in multiple resolutions so that they can scale accordingly.

Different browsers support diverse image formats. Newer formats like WebP may not be compatible with every browser, in which case images will have to be made available in JPEG format as well.

Recommended Read: How To Test Website in Different Screen Sizes

How to make an Image Responsive

Start with the question “how to make an image responsive in CSS?” When an image is uploaded to a website, it is endowed with default height and width. These need to be changed with CSS.

Simply assign a new value to the image’s width property. As a result, the image’s height will adjust itself in accordance. Make sure to use relative units (like a percentage) for the width property instead of absolute units like pixels.


img {
width: 800px;

The code above sets a fixed width of 800px. This image won’t be responsive as the unit is absolute and won’t adjust itself. However, look at the code below:

img {
width: 70%;

With a relative unit like 70% in place, the image will be fluid and resize itself, whatever the screen size.

Check Responsiveness

Using Media Queries

Media queries or CSS breakpoints are points defined in the code. Website content responds to these points and adjusts itself to the screen size to display the accurate layout. With CSS breakpoints in place, the website content will align itself with screen size and displays itself in a way that pleases the eye and facilitates visual consumption.

Learn everything you need to know about CSS and Media Query Breakpoints.

Obviously, responsive images require the use of media queries to resize themselves across to device screen size.

In the example below, the image carries a 50% width for any screen. In order to make them maximize to full size for any mobile devices, use media queries:

@media only screen and (max-width: 480px) {
img {
width: 100%;


By virtue of the media query above, any device with a screen size smaller than 480px will have the image take full-size along the screen’s width.

Images on a Fluid Layout

Creating responsive images on a fluid layout is a little more challenging. On most websites, images are the heaviest elements and need to start (and finish) loading before anything else. Therefore, browsers start out by scanning a web page’s HTML for image URLs and loading them. This happens before the browser builds the DOM, puts the layout together, or loads external CSS.

Consequently, the browser needs to know the image source even before they detect the image’s rendered size.

Keep in mind that the browser automatically knows the environments all web elements are rendering in: viewport size, users’ screen resolution, etc. This data is used to define media queries. Therefore, to adjust images to a fluid layout, devs can also define the images’ rendered size to the browsers.

Let the browser know how many pixels are required by using the sizes attribute. Use the w descriptors in srcset to communicate the number of pixels in each image source. Armed with this data, the browser can select the smallest image source that will allow the image to look presentable within its viewport.

Let’s take an example in which there are three image sizes:

  • large.jpg (1024 × 768 pixels)
  • medium.jpg (640 × 480 pixels)
  • small.jpg (320 × 240 pixels)

These images have to be placed in a flexible grid – one that starts from a single column, but in larger viewports, transforms into three columns:

<img srcset="large.jpg 1024w,
medium.jpg 640w,
small.jpg 320w"
sizes="(min-width: 36em) 33.3vw,


The code uses srcset with w descriptors to mention image sources – which communicate the actual pixel widths of the referenced file to the browser.

In this example, only image width is defined, not height. In this particular case, images in the layout have widths established explicitly by CSS but not their heights. Most responsive images tend to be the same, so this keeps things simple.

While the w descriptor defines the number of pixels for each image source, the sizes attribute relates to the browser how many pixels it needs by defining the image’s final rendered width. It offers the browser some data about the page’s layout early on to help it pick an image source before it renders the page’s CSS code.

Understanding the max-width property

To make images responsive, it is common for developers and designers to use the max-width property. It defines the maximum width for an element; no element can be wider than its max-width value.

That means that if an image carries the max-width of 800px and is being rendered on a screen size of only 360px, the device won’t display the complete image due to lack of space.

img {
max-width: 100%;
width: 800px; // assume this is the default size

By defining the max-width and setting it to 100%, the 800px image will shrink to fit the space on the 360px device. Using a relative unit (as shown above) will make the image fluid on any device less than 800px.

However, if the screen size is larger than 800px, the image will not expand to fit the same. This adversely affects its responsiveness. This is where the max-width property falls short.

Want to run a quick Responsive Image Test on your Website? Run now.

Images for Non-Supporting Browsers

Let’s say a particular image’s format is incompatible with browsers. This usually pops up when older browsers have to render images in newer formats. In such cases, use the code below:

<source type="image/svg" src="logo.svg" />
<source type="image/png" src="logo.png" />
<img src="logo.gif" alt="RadWolf, Inc." />


Note: Replace alt=”RadWolf, Inc.” with the relevant image name.

In case the browser cannot decipher the image/svg media type, it skips the first source. If it can’t work with image/png, then it ends up using the .gif file.

How to make background images responsive

Here’s how to create responsive background images with CSS:

  • Use the background-size property to encompass the viewport. Give this property a cover value that will tell a browser to scale the background image’s heights and width so that they always remain equal to or greater than the height/width of the device viewport.
  • Use a media query to offer scaled-down versions of the background image for mobile device screens. This is an optional step; given the widespread usage of mobile devices, it should be incorporated into the process.


<!doctype html>
...Your content here...


The background image goes in the body element so that it always covers the full browser viewport.

Note that the code will also work perfectly on block-level elements (a div or form). As long as the height and width of the block-level container is fluid, the background image will scale in order to cover the container in its entirety.

Now, declare the style rule for the body element with:

body {
/* Location of the image */
background-image: url(images/background-photo.jpg);

/* Background image is centered vertically and horizontally at all times */
background-position: center center;

/* Background image doesn't tile */
background-repeat: no-repeat;

/* Background image is fixed in the viewport so that it doesn't move when
the content's height is greater than the image's height */
background-attachment: fixed;

/* This is what makes the background image rescale based
on the container's size */
background-size: cover;

/* Set a background color that will be displayed
while the background image is loading */
background-color: #464646;


A few notes on the code

Pay particular attention to:

background-size: cover;

This property/value pair instructs the browser to adjust the background image so that its height and width are equal to the height and width of the element; here, the body element.

However, if the background image is smaller than the body element, the browsers will programmatically expand the image dimensions. Magnifying an image beyond its natural dimension usually causes it to degrade. This scenario usually pops up on high-res screens or on pages with far too much content.

To avoid this, try to use an image large enough for most screen sizes and resolutions.

Now look at:

background-position: center center;

This is used to ensure that the background image always remains at the center of the viewport.

Next, look at:

img {
width: 70%;

A scroll bar appears in cases where the content height is more than that of the visible viewport. But the background image must stay in its position even if the user scrolls down. Setting the background-attachment property (as done above) ensures this.

Test Image Responsiveness on Real Devices

Shaping and incorporating appropriate code to implement responsive images for all possible devices is not humanly possible. The best option is to deploy code, media queries, and breakpoints that fit the device preferences of the target audience. Additionally, keeping images adjustable and adaptable to change would also help accomplish more in the long term with reasonable levels of effort.

Once images have been rendered responsive, remember to test them on real devices to check their responsiveness. Depending on the number of screen sizes in question, checking responsive design is easiest when using a real device cloud.

BrowserStack offers 3000+ real browsers and devices for instant, on-demand cross-browser testing on the cloud. Simply sign up for free, choose from among the latest devices, navigate to the relevant website, and start verifying its responsive design.

Try BrowserStack for Free

BrowserStack’s responsive design checker provides an easy way to verify the responsive design. Simply enter the URL, click Check and receive a view of the site on multiple real devices, both desktop, and mobile. Ensure that your responsive design breakpoints are placed accurately and facilitate the most satisfying user experience that they possibly can.


Was this post useful?

Yes, Thanks Not Really

We're sorry to hear that. Please share your feedback so we can do better

Thanks a lot for your feedback!



Breakpoints for Responsive Web Design

What are Responsive Breakpoints? Why are they important? Know how to use the right breakpoint for yo...

Learn More

What is the ideal screen size for responsive design?

Are you testing your website's responsive design on the right screen sizes and resolutions? This art...

How do I change the background

The background-size property is used to set the background image size using CSS. Use height and width property to set the size of the background image.

How do I make my background

Here's how to create responsive background images with CSS: Use the background-size property to encompass the viewport. Give this property a cover value that will tell a browser to scale the background image's heights and width so that they always remain equal to or greater than the height/width of the device viewport.

What is background

The background-size CSS property sets the size of the element's background image. The image can be left to its natural size, stretched, or constrained to fit the available space.

How do I make the background of an image fit in CSS?

There are four different syntaxes you can use with this property: the keyword syntax ("auto", "cover" and "contain"), the one-value syntax (sets the width of the image (height becomes "auto"), the two-value syntax (first value: width of the image, second value: height), and the multiple background syntax (separated ...