CREATING A RESPONSIVE WEB DESIGN WITH CSS MEDIA QUERIES

Iheukwumere vivian Nneoma
LearnFactory Nigeria
6 min readMay 25, 2023
Image by Patrick Skrbinjek from Pixabay

INTRODUCTION

People spend, on average, 3.3 hours a day engaging with digital media on their smartphones. Because of this shift in consumer behavior, it’s no longer enough to have a website that only works from a desktop computer.

Responsive web design is undoubtedly a hot topic in web design right now. To some degree, the popularity of the concept of responsive web design is well deserved because site users are increasingly diversifying their methods of accessing a website. iPad, iPhone, Android mobile devices, desktops, netbooks — we’re in a time where our web designs must function in a multitude number of ways.

Understanding the Meaning of Responsive Design

Responsive web design is an approach to web design where the layout and content of a website are automatically adjusted to fit the size of the user’s device or viewport. This means that a website will look different on a desktop computer than it does on a phone.

Responsive web design is an approach to designing and developing websites that aim to provide an optimal viewing and interaction experience across a wide range of devices and screen sizes. It involves using CSS media queries to apply different styles or layout rules based on the characteristics of the device or browser window.

In order for a web design to be considered “responsive,” it needs to have three key features.

· The site must be built with a flexible grid foundation.

· Images that are incorporated into the design must be flexible.

· Different views must be enabled in different contexts via media queries.

The main focus would be on how to build a responsive website using Media Queries

MEDIA QUERIES

Media Queries are used to target different device types and viewports with different style sheets. They allow designers to adjust the stylesheet based on the size of the device.

Media queries are arguably the most exciting (and most intimidating to web designers unfamiliar with them) feature of a responsive web design. Oftentimes, people get carried away with media queries, thinking of them as the core component of a responsive design and leaving flexible page components optional. The reality of the situation is that media queries are hardly useful without the existing implementation of a rock-solid HTML and CSS foundation that includes not only a flexible grid but flexible images as well.

Media queries allow designers to build multiple layouts using the same HTML documents by selectively serving stylesheets based on the user agent’s features, such as the browser window’s size. Other parameters are orientation (landscape or portrait), screen resolution, color (i.e. how many colors the screen can render), and so on.

Media queries can be used to check many things, such as:

  • width and height of the viewport
  • width and height of the device
  • orientation (is the tablet/phone in landscape or portrait mode?)
  • resolution

Using media queries is a popular technique for delivering a tailored style sheet to desktops, laptops, tablets, and mobile phones (such as iPhone and Android phones).

For example, the following media query tests to see if the current web page is being displayed as screen media (therefore not a printed document) and if the viewport is at least 80rem wide. The CSS for the .container selector will only be applied if these two things are true

@media screen and (min-width: 80rem) {
.container {
margin: 1em 2em;
}
}

You can add multiple media queries within a stylesheet, tweaking your whole layout or parts of it to best suit the various screen sizes. The points at which a media query is introduced, and the layout changed, are known as breakpoints.

A common approach when using Media Queries is to create a simple single-column layout for narrow-screen devices (e.g. mobile phones), then check for wider screens and implement a multiple-column layout when you know that you have enough screen width to handle it. Designing for mobile first is known as mobile-first design.

If using breakpoints, best practices encourage defining media query breakpoints with relative units rather than absolute sizes of an individual device.

There are different approaches to the styles defined within a media query block; ranging from using media queries to <link> style sheets based on browser size ranges to only including custom properties variables to store values associated with each breakpoint.

Find out more in the MDN documentation for Media Queries.

Media queries can help with RWD but are not a requirement. Flexible grids, relative units, and minimum and maximum unit values can be used without queries.

CSS media queries allow you to specify different CSS rules based on various conditions such as screen size, screen resolution, device orientation, and more. By using media queries, you can adapt your website’s layout, typography, and other design elements to fit different devices, ensuring a seamless experience for users.

Here’s a basic example of how you can use CSS media queries to create a responsive design:

/* Default styles for all devices */
/* Styles for devices with a maximum width of 768px (e.g., tablets) */
@media (max-width: 768px) {
/* CSS rules specific to tablets */
}
/* Styles for devices with a maximum width of 480px (e.g., smartphones) */
@media (max-width: 480px) {
/* CSS rules specific to smartphones */
}

In the example above, the default styles apply to all devices. The `@media` rule is used to define different sets of styles based on the device’s screen width. The `max-width` condition specifies the maximum width at which the styles inside the media query will be applied. In this case, tablets (with a maximum width of 768px) and smartphones (with a maximum width of 480px) are targeted.

You can include as many media queries as needed to cover various devices and screen sizes. Within each media query, you can define different styles to adjust the layout, font sizes, margins, or any other CSS properties to ensure the content looks good on each device.

Media queries are not limited to screen width. You can also use other conditions such as `min-width`, `orientation`, `resolution`, and more to target specific devices or scenarios.

To create a responsive web design using CSS media queries, follow these steps:

1. Set up the HTML structure: Begin by creating the HTML structure of your webpage. This typically includes a `<head>` section for including CSS stylesheets and a `<body>` section for the content of your webpage.

2. Link the CSS file: In the `<head>` section of your HTML, link an external CSS file or add an internal `<style>` tag to write your CSS code.

3. Define the basic styles: Start by defining the basic styles for your webpage. These styles will be applied to all screen sizes unless overridden by media queries. For example:

body {
font-family: Arial, sans-serif;
color: #333;
}

4. Add media queries: Media queries allow you to apply specific CSS styles based on the screen size or device capabilities. Use the `@media` rule followed by a media type and conditions to specify when the styles should be applied. For example:

/* Media query for screens with a maximum width of 768px */
@media screen and (max-width: 768px) {
/* Adjust styles for smaller screens */
body {
font-size: 16px;
}
}
/* Media query for screens with a minimum width of 1200px */
@media screen and (min-width: 1200px) {
/* Adjust styles for larger screens */
body {
font-size: 20px;
}
}

5. Define responsive styles: Within each media query, you can adjust the styles to make your webpage responsive. This can include modifying font sizes, margins, paddings, layout, or even hiding/showing specific elements. Here’s an example:

/* Media query for screens with a maximum width of 768px */
@media screen and (max-width: 768px) {
/* Adjust styles for smaller screens */
body {
font-size: 16px;
}
/* Example: Hide a navigation bar for smaller screens */
.navbar {
display: none;
}
}
/* Media query for screens with a minimum width of 1200px */
@media screen and (min-width: 1200px) {
/* Adjust styles for larger screens */
body {
font-size: 20px;
}

/* Example: Increase margins for larger screens */
.content {
margin: 40px;
}
}

6. Test and iterate: Test your webpage on different devices and screen sizes to ensure that the responsive styles are working as expected. Make adjustments to the media queries and styles as needed to achieve the desired responsiveness.

It’s important to note that responsive web design goes beyond just using media queries. It also involves considerations like flexible grid systems, fluid images, scalable fonts, and other techniques to create a truly responsive and user-friendly experience.

I hope this explanation helps you understand the basis of responsive web design with CSS media queries. If you have any further questions, feel free to ask!

--

--