Respond to Me: The Art of Making Responsive Websites
Tags: beginners, webdev, design, css
Published: 2023-03-23 13:20:52

To provide a satisfactory browsing experience to most global netizens, developers must ensure that their websites are user-friendly, easily accessible, and adaptable to various devices and screen dimensions. How can developers achieve this seemingly Herculean task? By adhering to Responsive Web Design (RWD) principles.

## Prerequisites

Kindly have an understanding of the following:

* Basic HTML and CSS
    

## Goal

The article will guide you through the following:

* What is RWD?
    
* Factors to consider when creating a responsive website.
    
* How to add responsiveness to a website.
    

### Definition of terms

Below are some of the technical terms present in the article:

* Viewport - This is the visible portion of the web page within the browser window. It is the area that a user can see without scrolling.
    
* Render - Refers to displaying a web page or application on a user's device.
    
* Breakpoint - The point at which a website's layout or design changes to better fit a different screen size or device type.
    
* Polyfill - Code that provides modern functionality to older browsers without natively supporting it.
    

### Understanding What Responsive Web Design Is

![Start of the explanation section](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1lct0cjonrehuh0c4jv.png)
RWD is a web design methodology that aims to produce web pages that appear visually appealing on all screen sizes and devices. This article delves into highly recommended factors to consider when creating responsive websites.

### A. Add Limits to Make The Viewport Responsive

Many browsers render web pages at a fixed width. This can cause horizontal scroll issues for users with smaller devices. To address this, we can add the `<meta>` tag to the HTML header, which sets the width and scaling of the viewport. The meta tag allows a webpage to adapt to different screen sizes and display properly on devices of varying widths.

```HTML
<meta name="viewport" content="width=device-width, initial-scale=1.0">
```

### B. How About Giving Responsive Images a Shot?

Any website that you develop should display consistently on all major devices. It should arouse pleasure and satisfaction for your users as they navigate and read through your content. Your images should load in a snap and be clear. But if you fail on any of these tenets, it will be like serving your friend uncooked food.

We can use CSS `max-width` and `object-fit` properties to ensure images respond to different device widths and heights.

#### **i. The** `max-width` property

```css
.example-img {
max-width: 100%;
height: auto;
}
```

The above rule restricts the browser from rendering an image that exceeds its container's width but preserves the image's aspect ratio by adjusting its height accordingly. The `max-width` Property is useful when there is a need to maintain the layout of a site and ensure that an image doesn't overlap with other elements.

#### **ii. The** `object-fit` Property

The rule directs the browser to either preserve the image's aspect ratio or resize and fill as much space as possible in its container. For instance, we add the value `contain` to the `object-fit` property to preserve the image's aspect ratio.

```css
img{
height: 200px
width: 300px
/* preserves the image's aspect ratio but resizes it to fit the container */
object-fit: contain;
}
```

Sometimes, you need to have an image that always fills its container without leaving whitespaces. The `cover` value will do just that.

```css
img{
height: 200px
width: 300px
/* respects the image's aspect ratio, fills the container but can crop some parts */
object-fit: cover ;
}
```

### C. Responsive Units Are Better Than Fixed Units

Fixed units, such as pixels (px), remain constant regardless of screen size. These units are useful when developing logos and navigation bars that need to retain a certain size or position on the page.

Responsive units, such as percentages(%), are calculated relative to the screen size and adjusted accordingly. This makes the units preferable for designing flexible elements that resize proportionally according to their container size and maintain their alignment with other features.

Instead of using fixed widths like this:

```css
.container {
  /* The container will always take up 960px no matter the type of device */
  width: 960px;
}

.column {
  /* The column will always take up 480px no matter the type of device */
  width: 480px;
}
```

Use fluid widths like this:

```css
.container {
  /* The container will dynamically readjust its width to match the device's width */
  width: 100%;
}
.column {
  /* The column will dynamically readjust to occupy half of its container's width */
  width: 50%;
}
```

Other responsive units include:

| Responsive Unit | Application |
| --- | --- |
| `rem` | They are based on the font size of the `<html>` element. |
| `em` | They are relative to the parent font size. |
| `vh`(Viewport height) `vw`(Viewport width) | They are based on the viewport's size and can be used to create responsive layouts that adjust to the screen or window size. |

### D. Unleash the Strength of Media Queries

Media queries are CSS rules that apply distinctive styles depending on various factors, including device screen size, resolution, orientation, and other conditions. We can effortlessly create breakpoints through media queries that dynamically modify the layout or appearance of a website based on the device or screen size.

It is imperative to adopt a mobile-first design approach during development. This means creating the site for mobile devices before scaling to larger screens.

The following table outlines the primary device categories and their corresponding breakpoints.

> Note: I generally prefer bootstrap's breakpoint standards.

| Device | Description | Breakpoints |
| --- | --- | --- |
| Phones | Small device | 576px |
| Tablets | Medium devices | 768px |
| Laptops | Large devices | 992px |
| Desktops | Extra large devices | 1200px |
| Large Desktops | Extra-extra large devices | 1400px |

The above standards allow us to design a website that adapts to different devices and is supported by most browsers.

Explore the sample code below for a comprehensive demonstration of how to apply media queries.

```css
/* Smartphones  */
@media (min-width: 576px) { 
  ... code goes here
 }

/* Tablets with a screen size of 768px and up */
@media (min-width: 768px) { 
  ...code goes here
 }

/* Desktops with a screen size of 992px and up */
@media (min-width: 992px){ 
  ...code goes here
 }

/* Large desktops with a screen size of 1200px and up */
@media (min-width: 1200px) { 
  ...code goes here
 }

/* Larger desktops with a screen size of 1400px and up */
@media (min-width: 1400px) { 
  ...code goes here
 }
```

### E. Try Responsive Layouts Like Flexbox and Grid

Modern CSS layout methods such as Flexbox and Grid are powerful tools for creating responsive layouts that adapt to different screen sizes and devices.

#### **i. Flexbox**

Flexbox is a one-dimensional layout method that allows you to create flexible and responsive layouts. It provides a set of properties enabling us to specify how items should be positioned, aligned, and distributed within a container. With Flexbox, you can easily create layouts that adapt to different screen sizes without using media queries extensively.

```HTML
//Example of using Flexbox
<div class="container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
</div>
```

```CSS
.container {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.item {
  width: 200px;
  height: 150px;
  background-color: #f2f2f2;
  margin: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}
```

In this example, we have a container with the `display: flex` property set, which turns it into a flex container. The `flex-wrap` property wraps the items onto multiple lines as needed. Additionally, the `justify-content` property centers the items horizontally within the container. Each item has the `display: flex` property set as well, which makes it a flex item. The `align-items` and `justify-content` properties center the content of each item vertically and horizontally, respectively.

#### **ii. Grid**

Grid is a two-dimensional layout method that allows you to create complex and responsive layouts. It provides a grid system enabling us to define rows and columns, and you can place items anywhere in the Grid. With Grid, we can create more flexible and customizable layouts than those made with traditional layout methods like [floats](https://developer.mozilla.org/en-US/docs/Web/CSS/float).

Example of using Grid:

```HTML
<div class="container">
  <div class="item item1">Item 1</div>
  <div class="item item2">Item 2</div>
  <div class="item item3">Item 3</div>
  <div class="item item4">Item 4</div>
  <div class="item item5">Item 5</div>
  <div class="item item6">Item 6</div>
</div>
```

```css
.container {
  display: Grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 10px;
}

.item {
  background-color: #f2f2f2;
  padding: 20px;
  text-align: center;
}

.item1 {
  grid-column: 1 / 3;
  grid-row: 1 / 3;
}

.item2 {
  grid-column: 3;
  grid-row: 1 / 4;
}

.item3 {
  grid-column: 1;
  grid-row: 3 / 5;
}

.item4 {
  grid-column: 2;
  grid-row: 3;
}

.item5 {
  grid-column: 3;
  grid-row: 4;
}

.item6 {
  grid-column: 2 / 4;
  grid-row: 5;
}
```

In this example, we turn the container class into a grid container using the `display: grid` property. The `grid-template-columns` property defines the columns of the Grid, and the `grid-gap` property sets the gap between the grid items. Each item has a unique class name and is positioned within the Grid using the `grid-column` and `grid-row` properties.

Current web browsers provide full support for Grid and Flexbox. However, to ensure backward compatibility with earlier browser versions, it may be necessary to implement fallback techniques or [polyfills](https://developer.mozilla.org/en-US/docs/Glossary/Polyfill) that bring seamless rendering of any design.

### Benefits of RWD

* Improved user experience
    
* Cost-effective development
    
* Increased reach across devices
    
* Reduced maintenance efforts
    
* Better search engine optimization (SEO)
    
* Easier content management
    
* Future-proofing
    

### Join the RWD Revolution to Improve your Users' Experience

Creating a responsive website involves various elements that work together to achieve its ultimate objective. Each factor plays a significant role in ensuring the website is responsive and user-friendly. In today's web landscape, responsive web design demands meticulous planning and execution. That's why developers must recognize the importance of implementing responsive web design to achieve optimal results

### Join the conversation!

I value your feedback and want to hear your thoughts. Leave a comment and let me know what you think about the article. Your insights could inspire new ideas and help shape future content.

[Online illustrations by Storyset](https://storyset.com/online)