RWD Images


Resize the browser window to see how the image scales to fit the page.


Using The width Property

If you set the width to a percentage and the height to 'auto' for an image, it will adjust its size to fit the screen and can be scaled both larger and smaller.

In the example above, you can make the picture bigger than its original size. However, in many situations, it's smarter to use the "max-width" property instead of doing that.


Using The max-width Property

When the 'max-width' property is set to 100%, an image can shrink in size if necessary, but it will never enlarge beyond its original dimensions.


Add an Image to The Example Web Page


Background Images

Background images can adapt and change when you resize and adjust them.

In this article, we'll explain three distinct approaches:

1. When you set the background-size property to "contain," the background image will resize itself to try and fit the content area. But it will still maintain its original proportions, meaning the width and height ratio of the image remains the same:


Here's the CSS code:


2. When you set the background-size property to "100% 100%", the background image will expand to fit the entire content area:


This is the CSS code:


3. When you set the background-size property to "cover," the background image will stretch to cover the whole content area. However, it retains its original proportions, so some of the image might be hidden:


This is the CSS code:


Different Images for Different Devices

If you put a huge picture on a large computer screen, that's great. But it's not so great on a small device. Why make the device load a big image and then make it smaller? To save loading time and for other reasons, you can use media queries to show different pictures on different devices.

There's a big picture and a small picture that will show up on various devices:

If you want to make sure an image stays the same when you resize the browser window, use min-device-width in your media query instead of min-width. This way, it checks the width of the device, not just the browser:


The HTML <picture> Element

The HTML <picture> element provides greater flexibility for web developers when defining image resources.

The <picture> element is typically used for pictures in responsive designs. Instead of using a single image that gets bigger or smaller depending on the screen size, you can use several images to better fit the size of the web browser window.

The <picture> element operates like the <video> and elements. You provide various sources, and the element selects the first source that matches the requirements:

You need the "srcset" attribute, which tells where the image comes from.

The "media" part is not a must-have, and it can take the media queries you see in the CSS "@media rule" found at this link: CSS @media rule.

You should also create an element with specified width and height for browsers that cannot display the element.