CSS / CSS Media Queries and Responsive Design

Introduction to Media Queries

This tutorial will introduce you to the concept of media queries, a pivotal tool in responsive web design. You will learn how to use them to apply different styles to different de…

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Covers media queries and techniques to create responsive designs.

Introduction

In this tutorial, we will delve into the world of Media Queries, an essential tool in responsive web design. The goal is to help you understand how to use media queries to style your web pages differently depending on the characteristics of the device viewing them, such as screen resolution, orientation, and type.

By the end of this guide, you will:
- Understand what media queries are
- Know how to write basic media queries
- Be able to apply different styles for different devices

Prerequisites

You should have basic knowledge of HTML and CSS. Knowledge of basic responsive design principles will be beneficial but not mandatory.

Step-by-Step Guide

What are Media Queries?

Media queries are a feature of CSS3. They allow you to apply different styles to different devices based on characteristics like their screen size, device type, and orientation.

How to Write a Media Query

A media query is composed of a media type and at least one expression that limits the style sheets' scope by using media features, such as width, height, and color.

A basic media query syntax looks like this:

@media screen and (max-width: 600px) {
  /* CSS styles here */
}

In the example above, the styles within the brackets will be applied to screens smaller than or equal to 600px wide.

Best Practices

  • Try to design your site mobile-first. That means you first style for small screens, then use media queries to add styles for larger screens.
  • Test your media queries on actual devices when possible. Simulators and emulators can help, but they may not always be 100% accurate.

Code Examples

Example 1: Changing font size based on screen width

p {
  font-size: 18px;
}

@media screen and (max-width: 600px) {
  p {
    font-size: 16px;
  }
}

In the above example, the font size of the paragraph tag is set to 18px by default. However, if the screen size is 600px or less, the font size will be 16px.

Example 2: Changing layout for smaller screens

.container {
  display: flex;
}

@media screen and (max-width: 600px) {
  .container {
    display: block;
  }
}

Here, we have a container that uses flex display. However, for screens that are 600px wide or less, we change the display to block.

Summary

In this tutorial, we have covered:
- The concept of media queries
- How to write a basic media query
- How to apply different styles for different devices

To continue learning, you might want to explore different media features and how to use them in media queries. You can find a comprehensive list of media features on the Mozilla Developer Network site.

Practice Exercises

Exercise 1: Write a media query that changes the background color of the body to blue when the screen width is less than or equal to 500px.

Solution:

@media screen and (max-width: 500px) {
  body {
    background-color: blue;
  }
}

In this exercise, we use a media query to change the background color of the body when the screen width is 500px or less.

Exercise 2: Write a media query to change the layout of a div with a class .main from grid to flex when the screen orientation is landscape.

Solution:

@media screen and (orientation: landscape) {
  .main {
    display: flex;
  }
}

In this exercise, we use a media query that checks the orientation of the screen. If the orientation is landscape, it changes the display of the .main div to flex.

Remember, practice is the key to mastering media queries, so try to use them in your own projects and see the results.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Timestamp Converter

Convert timestamps to human-readable dates.

Use tool

Image Compressor

Reduce image file sizes while maintaining quality.

Use tool

Markdown to HTML Converter

Convert Markdown to clean HTML.

Use tool

Backlink Checker

Analyze and validate backlinks.

Use tool

File Size Checker

Check the size of uploaded files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help