Responsive Web Design

Introduction to Responsive Web Design

Responsive web design is about creating web pages that look good on all devices!

A responsive web design will automatically adjust for different screen sizes and viewports.


Best Experience For All Users

Web pages can be viewed with many different devices: desktops, tablets, and mobile phones.

Your web page should look good, and be easy to use, regardless of the device.

Responsive web design is about using HTML and CSS to automatically resize, hide, shrink, or enlarge a website, to make it look good on all devices.

Key components in responsive web design are:

Example:

Result:

Responsive Web Design – The Viewport

Setting The Viewport

The viewport is the user’s visible area of a web page.

The viewport varies with the device (will be a lot smaller on a mobile phone than on a computer screen).

You should include the following <meta> element in the <head> section of all your web pages:

<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

This gives the browser instructions on how to control the page’s dimensions and scaling.

The width=device-width part sets the width of the page to follow the screen-width of the device (which will vary depending on the device).

The initial-scale=1.0 part sets the initial zoom level when the page is first loaded by the browser.

Here is an example of a web page without the viewport meta tag, and the same web page with the viewport meta tag:

 

Without the viewport meta tag

With the viewport meta tag


 

Responsive Web Design – Media Queries

CSS Media Queries

CSS media queries allow you to apply styles based on the characteristics of a device or the environment displaying the web page.

CSS media queries are essential for creating responsive web pages.

The CSS @media rule is used to add media queries to your style sheet.

Example

Here we use a media query to add a breakpoint at 600px:

Result

Screen Width above 600px

 

Hide Elements With Media Queries

Here, we use media queries to hide an element on small screens:

Example

 

Result

Screen Width above 600px

Screen Width below 600px

Change Font Size With Media Queries

Here, we use media queries to change the font size of an element on different viewport widths:

Exanple

Result

Screen Width above 600px

Screen Width below 600px

Media Queries for User Preferences

Some users have motion sensitivity and prefer websites with less animation.

The prefers-reduced-motion media feature lets you check if a user has asked to reduce motion, such as animations or transitions. Use this feature to turn off animations and transitions for the users who has activated this setting on their computer:

 

Responsive Web Design – Images

Using The width Property

If the width property is set to a percentage and the height property is set to “auto”, the image will be responsive and scale up and down:

Example

Result

Using width: 100%

If the width property is set to 100%, the image will scale up and down. It will even scale up to be larger than its original size.

Resize the browser window to see the effect.

Using The max-width Property

If the max-width property is set to 100%, the image will scale down if it has to, but never scale up to be larger than its original size:Example


Add an Image to The Example Web Page

Example

Result

Screen Width above 600px

Screen Width below 600px

Background Images

Background images can also respond to resizing and scaling.

Here we will show three different methods:

1. Using background-size: contain;: The background image will scale up and down, and tries to fit the content area. However, the image will keep its aspect ratio (the proportional relationship between the image’s width and height):

Example

Result

Screen Width above 600px

Screen Width below 600px

2. Using background-size: 100% 100%;: The background image will stretch to cover the entire content area:

Example

Result

Screen Width above 600px

Screen Width below 600px

3. Using background-size: cover;: The background image will scale to cover the entire content area. Notice that the “cover” value keeps the aspect ratio, and some part of the background image may be clipped:

Example

Result

Screen Width above 600px

Screen Width below 600px

Different Images for Different Devices

A large image can be perfect on a big computer screen, but useless on a small device. Why load a large image when you have to scale it down anyway? To reduce the load, or for any other reasons, you can use media queries to display different images on different devices.

Example

Result

Screen Width above 400px

Screen Width below 400px

Responsive Web Design – Videos

Using The width Property

If the width property is set to 100%, the video player will be responsive and scale up and down:

Example

Result

Using width: 100%

If the width property is set to 100%, the video will scale up and down. It will even scale up to be larger than its original size.

Resize the browser window to see the effect.

Screen Width above 600px

Screen Width below 600px