2 min read

Responsive Web Design

Responsive Web Design (RWD) is about creating web pages that look good on all devices! It is not a program or a JavaScript script. It is a design approach that uses HTML and CSS to resize, hide, shrink, enlarge, or move the content to make it look good on any screen.

1. The Viewport

The viewport is the user's visible area of a web page. It varies with the device; it will be smaller on a mobile phone than on a computer screen.

To ensure proper rendering and touch zooming on mobile devices, you must include the following <meta> tag in the <head> element:

<meta name="viewport" content="width=device-width, initial-scale=1.0">
  • width=device-width: Sets the width of the page to follow the screen-width of the device.
  • initial-scale=1.0: Sets the initial zoom level when the page is first loaded.

2. Media Queries

Media queries allow you to apply different styles for different media types/devices. They are the core of responsive design.

Syntax: @media not|only mediatype and (expressions) { ... }

Common Breakpoints

There are no standard breakpoints, but these are commonly used for mobile, tablet, and desktop:

/* Extra small devices (phones, 600px and down) */
@media only screen and (max-width: 600px) {
  body {background-color: red;}
}

/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {
  body {background-color: green;}
}

/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {
  body {background-color: blue;}
}

/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {
  body {background-color: orange;}
}

3. Flexible Layouts

Instead of using fixed units like pixels (px), use relative units like percentages (%), viewport units (vw, vh), or flexible layouts like Flexbox and Grid.

  • Grid & Flexbox: These modern layout modules are inherently responsive. They allow items to wrap, shrink, or grow based on available space.

4. Responsive Images

If you want an image to scale down if it has to, but never scale up to be larger than its original size, use max-width: 100%.

img {
  max-width: 100%;
  height: auto;
}

5. Mobile First

Mobile First means designing for mobile before designing for desktop or any other device (This will make the page display faster on smaller devices).

This means we must make the following changes to our CSS:

  1. Write the styles for mobile devices first (outside any media query).
  2. Use min-width media queries to add styles for larger screens.

programming/css/css programming/css/css3 programming/css/flexbox programming/css/grid-layout