DEV Community

Cover image for A Step-by-Step Guide to Building a Responsive and Mobile-Friendly website from scratch
Adegboyega Adedamola Wilson
Adegboyega Adedamola Wilson

Posted on

A Step-by-Step Guide to Building a Responsive and Mobile-Friendly website from scratch

A Web developer’s step-by-step guide to creating and designing websites that are fully responsive in the multi-device world we are in today

Table of content

  • Introduction
  • Prerequisite
  • What is website responsiveness?
  • Implementing media queries for different screen sizes
  • Optimizing images and assets for faster mobile loading
  • Testing on various devices and browsers for cross-device compatibility
  • HTML and CSS best practices for responsiveness
  • Conclusion

Introduction

We live in a multi-device world, and because of this, front-end developers must build fully equipped, compatible, and responsive websites on all devices. In this guide, we will learn how to build responsive and mobile-friendly websites.

Prerequisite

This guide is for people with basic knowledge of HTML and CSS, If you are not familiar with these you must familiarize yourself with them before going through this guide.

What is website responsiveness?

First, we will understand what responsiveness is.
Responsiveness is the ability of the interface of a website or application to change or adapt to changes in the environment or device.

Now we will understand what website responsiveness is.
Website responsiveness popularly known as responsive web design focuses on creating websites that automatically adjust and optimize their layout and content based on the screen size and device used to access the website.
Contents like images, texts, videos, and navigation bars automatically resize themselves to fit the screen size of the device being used to access the website. This is essential for optimal user interaction and experience on the website.

Implementing media queries for different screen sizes

What are media queries?
Media queries are CSS(cascading style sheets) rules that allow web developers to create responsive websites. Media queries allow web developers to conditionally apply styles based on the device or browser screen size, orientation, and resolution.
For example, they can apply styles for small screen sizes (280px-760px), medium screen sizes (760px-1440px), and large screen size (1440px and above), doing this makes the website adjust automatically to different screen sizes and layouts.

Use the '@media' rule followed by the condition you want it to meet and the styles you wish to apply to implement the media query. An example of this will be shown below.

Media query usage

The image above shows the '@media' rule, followed by the condition, and the styles to be applied.

Optimizing images and assets for faster mobile loading

Optimizing images and assets for faster mobile loading involves compressing and reducing file size, and improving the efficiency of images to ensure that assets load faster. Below are techniques and tips which you can use to optimize images and assets for faster loading:

  1. Lazy Loading: Lazy loading simply means delaying image loading until it the image becomes visible to the user. This enables the initial load of the page to be faster, and loads only the image in the user’s viewport

  2. Use the proper image format: Using the proper image format enables your website to load faster on mobile. The popular image formats are:

  • PNG: PNG is preferred for images with transparency such as logos.
  • JPEG: JPEG is the best choice for photographs and images with many colors.
  • SVG: SVG(Scalable Vector Graphics) is the best choice for icons or simple graphics on your website.
  • WebP: WebP provides high-quality images with lower file sizes, but not all browsers support this format. It is recommended to use this format if the web browser supports the format.
  1. Compress images: This involves the use of image compressing tools to significantly reduce the size of an image while keeping its original quality.

  2. Use responsive image: Use of ‘’ tag, and CSS media queries enables you to serve suitable and different images for different devices or screen sizes.

  3. Use SVG icons or Icon fonts: Using image files for icons can increase your website load time because of their sizes, instead use icon fonts or SVG icons. These are some examples of icon fonts and SVG fonts: IcoMoonApp, Material Design, Font Awesome, free Icons, and Icon Packs. They are scalable and have lower file sizes compared to using image files.

  4. Test and Monitor: Test and Monitor simply means using tools like WebPageTest, Google PageSpeed Insights, or GTmetrix to test and monitor your current load speed and ensure necessary improvements are made.

Testing on various devices and browsers for cross-device compatibility

Testing on various devices and browsers for cross-device compatibility simply means ensuring that your website appears and functions well on different devices and browsers, ensuring users enjoy a smooth experience on any device or browser they use.
Below are some examples of testing tools:

  1. Browser DevTools
  2. CrossBrowser Testing
  3. Lambda Test
  4. Screenfly
  5. BrowserStack
  6. Sauce Labs

These tools enable developers to ensure that their website works effectively on multiple devices.
By testing for cross-device compatibility web developers detect issues with their websites and fix them.

HTML and CSS best practices for responsiveness

Below are some tips on best practices for responsiveness:

  1. Use media queries appropriately
  2. Use relative units like 'vh', 'vw', and '%' instead of fixed pixels, this ensures that elements resize according to the screen size.
  3. Use CSS Flexbox or Grid for creating responsive layouts.
  4. Use relative font sizes like 'em' and 'rem'.
  5. Apply the mobile-first approach.
  6. Compress images with large file sizes to reduce page load time.

Following this best practice will help you to create websites that are responsive easily.

Conclusion

In conclusion, it is vital as a Web developer for your websites to be compatible and fully responsive on all devices. Following the techniques in this guide will enable you to build responsive websites that are mobile-friendly and compatible with all devices.
Now that you have learned how to build responsive websites, consider learning the significance of responsive design in a multi-device world.

Top comments (0)