Responsive Web Design
Responsive Web Design
Responsive web design (RWD) is an approach to web design that makes web pages render well on a variety of devices and window or screen sizes. Recent work also considers the viewer proximity as part of the viewing context as an extension for RWD. Content, design and performance are necessary across all devices to ensure usability and satisfaction.
You should include the following
<meta>
viewport element in all your web pages:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
A
<meta>
viewport element 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.
First ensure that all HTML elements have the
box-sizing
property set to border-box
. This makes sure that the padding and border are included in the total width and height of the elements.
* {
box-sizing: border-box;}
box-sizing: border-box;}
What is a Media Query?
Media query is a CSS technique introduced in CSS3.
It uses the
@media
rule to include a block of CSS properties only if a certain condition is true.
@media only screen and (max-width: 600px) {
body {
background-color: lightblue;
}}
body {
background-color: lightblue;
}}
RWD - Images
If the
width
property is set to a percentage and the height is set to "auto", the image will be responsive and scale up and down:
img {
width: 100%;
height: auto;}
width: 100%;
height: auto;}
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:
img {
max-width: 100%;
height: auto;}
max-width: 100%;
height: auto;}
If the
background-size
property is set to "contain", the background image will scale, and try to fit the content area. However, the image will keep its aspect ratio (the proportional relationship between the image's width and height):
div {
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
background-repeat: no-repeat;
background-size: contain;
border: 1px solid red;}
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
background-repeat: no-repeat;
background-size: contain;
border: 1px solid red;}
If the
background-size
property is set to "100% 100%", the background image will stretch to cover the entire content area:
div {
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
background-size: 100% 100%;
border: 1px solid red;}
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
background-size: 100% 100%;
border: 1px solid red;}
The
<picture>
element works similar to the <video>
and <audio>
elements. You set up different sources, and the first source that fits the preferences is the one being used:
<picture>
<source srcset="img_smallflower.jpg" media="(max-width: 400px)">
<source srcset="img_flowers.jpg">
<img src="img_flowers.jpg" alt="Flowers">
</picture>
<source srcset="img_smallflower.jpg" media="(max-width: 400px)">
<source srcset="img_flowers.jpg">
<img src="img_flowers.jpg" alt="Flowers">
</picture>
No comments