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…
Section overview
5 resourcesCovers 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
- 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.
- Create a responsive image gallery using flexbox that wraps images onto the next line when there is not enough space.
- 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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article