Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Responsive Web Design – Images

Responsive Web Design - Images


Using the width property

If the width property is set to 100%, the image will scale responsively according to the available range:

Example

img

{

   

width:

100%;

   

height:

auto;

}

Try it »

Note that in the example above, the image will be larger than its original. We can use the max-width property to solve this problem.


Using the max-width property

If the max-width property is set to 100%, the image will never be larger than its original size:

Example

img

{

   

max-width:

100%;

   

height:

auto;

}

Try it »


Adding images to a web page

Example

img

{

   

width:

100%;

   

height:

auto;

}

Try it »


Background images

Background images can be resized or scaled responsively.

Here are three different methods:

  1. If the background-size property is set to "contain", the background image will scale to fit the content area while maintaining its aspect ratio:

This is the CSS code:

Example

div

{

   

width:

100%;

   

height:

400px;

   

background-image:

url('img_flowers.jpg');

   

background-repeat:

no-repeat;

   

background-size:

contain;

   

border:

1px solid red;

}

Try it »

  1. If the background-size property is set to "100% 100%", the background image will stretch to cover the entire area:

Example

This is the CSS code:

div

{

   

width:

100%;

   

height:

400px;

   

background-image:

url('img_flowers.jpg');

   

background-size:

100% 100%;

   

border:

1px solid red;

}

Try it »

  1. If the background-size property is set to "cover", the background image is scaled large enough to completely cover the background area. Note that this property maintains the image's aspect ratio, so some parts of the background image may not be visible in the background positioning area.

This is the CSS code:

Example

div

{

   

width:

100%;

   

height:

400px;

   

background-image:

url('img_flowers.jpg');

   

background-size:

cover;

   

border:

1px solid red;

}

Try it »


Displaying different images on different devices

Large images can be displayed on large screens, but not well on small screens. There is no need to load large images on small screens, as this greatly affects loading speed. So we can use media queries to display different images on different devices.

The following large and small images will be displayed on different devices:

Example

/* For width smaller than 400px: */

body

{

   

background-image:

url('img_smallflower.jpg');

}

/* For width 400px and larger: */

@media only screen and (min-width: 400px)

{

    body

{

       

background-image:

url('img_flowers.jpg');

    }

}

Try it »

You can use the min-device-width of media queries instead of the min-width property; it detects the device width rather than the browser width. When the browser size is reset, the image size does not change.

Example

/* Devices smaller than 400px: */

body

{

   

background-image:

url('img_smallflower.jpg');

}

/* Devices larger than 400px (and equal): */

@media only screen and (min-device-width: 400px)

{

    body

{

       

background-image:

url('img_flowers.jpg');

    }

}

Try it »


HTML5 Element

The HTML5 The element can hold multiple images.

Browser support

Element
Not supported 38.0 38.0 Not supported 25.0

The element is similar to the and element. It can specify different resources, with the first resource specified being preferred:

Example

<

picture

<

source

srcset=

"img_smallflower.jpg"

media=

"(max-width: 400px)"

 

<

source

srcset=

"img_flowers.jpg"

<

img

src=

"img_flowers.jpg"

alt=

"Flowers"

<

/picture

Try it »

srcset The attribute is required and defines the image resource.

media The attribute is optional; see the media query section for CSS @media rule details.

For systems that do not support For browsers, you can also define a element as a replacement.