4. Responsive Web Design Principles


1. Create a Media Query

Here’s an example of a media query that returns the content when the device’s width is less than or equal to 100px:

@media (max-width: 100px) { /* CSS Rules */ }

and the following media query returns the content when the device’s height is more than or equal to 350px:

@media (min-height: 350px) { /* CSS Rules */ }

2. Make an Image Responsive

Making images responsive with CSS is actually very simple. You just need to add these properties to an image:

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

3. Use a Retina Image for Higher Resolution Displays

The simplest way to make your images properly appear on High-Resolution Displays, such as the MacBook Pros “retina display” is to define their width and height values as only half of what the original file is. Here is an example of an image that is only using half of the original height and width:

  img { height: 250px; width: 250px; }
<img src="coolPic500x500" alt="A most excellent picture">

4. Make Typography Responsive

Instead of using em or px to size text, you can use viewport units for responsive typography. Viewport units, like percentages, are relative units, but they are based off different items. Viewport units are relative to the viewport dimensions (width or height) of a device, and percentages are relative to the size of the parent container element.

The four different viewport units are:

  • vw (viewport width): 10vw would be 10% of the viewport’s width.
  • vh (viewport height): 3vh would be 3% of the viewport’s height.
  • vmin (viewport minimum): 70vmin would be 70% of the viewport’s smaller dimension (height or width).
  • vmax (viewport maximum): 100vmax would be 100% of the viewport’s bigger dimension (height or width).

Here is an example that sets a body tag to 30% of the viewport’s width.

body { width: 30vw; }