CSS / CSS Media Queries and Responsive Design

Handling Different Screen Sizes with CSS

This tutorial will teach you how to handle different screen sizes using CSS. We will look at how to use CSS techniques such as media queries, flexbox, and grid to create responsiv…

Tutorial 3 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 explore how to handle different screen sizes using CSS. By the end of this tutorial, you will be able to create responsive designs that adapt to different screen sizes using CSS techniques such as media queries, flexbox, and grid.

To make the most out of this tutorial, you should have a basic understanding of HTML and CSS.

Step-by-Step Guide

Media Queries

Media queries are a feature of CSS that allow you to apply different styles for different media types and screen sizes.

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

In the above example, the background color of the body will change to blue when the screen width is less than or equal to 600px.

Flexbox

Flexbox is a CSS module that provides a more efficient way to layout, align and distribute space among items in a container.

.container {
  display: flex;
  flex-wrap: wrap;
}

In the above example, we set the display of the container to flex which allows us to use flex properties. The flex-wrap: wrap; property ensures that the items in the container wrap onto multiple lines.

Grid

The CSS Grid Layout Module offers a grid-based layout system, with rows and columns, making it easier to design web pages.

.container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}

In the above example, we set the display of the container to grid which allows us to use grid properties. The grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); property ensures that each grid item is at least 200px wide and the remaining space is distributed evenly.

Code Examples

Let's create a responsive navigation bar using the techniques we've learned.

<html>
<head>
<style>
  .nav {
    display: flex;
    justify-content: space-around;
    background-color: #333;
  }

  .nav a {
    color: white;
    padding: 14px 20px;
    text-decoration: none;
  }

  @media screen and (max-width: 600px) {
    .nav {
      flex-direction: column;
    }
  }
</style>
</head>
<body>
  <div class="nav">
    <a href="#">Home</a>
    <a href="#">About</a>
    <a href="#">Contact</a>
  </div>
</body>
</html>

In this example, the navigation bar is horizontal on large screens and becomes vertical on screens smaller than 600px.

Summary

In this tutorial, we've learned how to handle different screen sizes using CSS via media queries, flexbox, and grid. You can now create responsive designs that adapt to any screen size. To continue learning, explore more about these topics and practice creating your own responsive designs.

Practice Exercises

  1. Create a responsive grid layout that changes from a 3-column layout to a 1-column layout when the screen size is less than 600px.
  2. Create a responsive image gallery using flexbox that wraps images onto the next line when there is not enough space.
  3. Create a responsive header that changes its background color and alignment of items when the screen size is less than 500px.

Remember, practice is key when it comes to mastering responsive web design. Don't shy away from experimenting with different layouts and screen sizes!

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

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

XML Sitemap Generator

Generate XML sitemaps for search engines.

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

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