Media Queries: Responsive Design with CSS
Welcome to this tutorial on Media Queries! In web development, responsive design is all about creating websites that adapt seamlessly to different screen sizes and devices – from smartphones to large desktop monitors. Media Queries are a powerful CSS feature that allows you to apply different styles based on the characteristics of the device, such as screen width, height, orientation, and resolution. They’re a cornerstone of building websites that look great on any device.
Understanding the Basics
Media Queries are a set of CSS rules that apply different styles based on the characteristics of the device. They’re essentially conditional CSS. The core concept is that you define rules that apply when a specific device meets certain criteria. This is crucial for creating a consistent user experience across a wide range of devices.
Syntax
The basic syntax for a Media Query is:
@media (media-feature) {
/* CSS rules to apply when the media feature is true */
}
@media: This keyword indicates that we're defining a media query.(media-feature): This is a selector that specifies the device characteristics to consider. Common media features include:screen: For screens with a pixel density of 72 DPI (dots per inch) or higher.print: For printing.all: Applies to all devices.max-width: Applies when the screen width is less than or equal to a specified width.min-width: Applies when the screen width is greater than or equal to a specified width.
{}: The curly braces enclose the CSS rules that will be applied when the media feature is true.
Practical Examples
Let's look at some practical examples to illustrate how Media Queries work:
Example 1: Basic Responsive Design - Smaller Screens
/* Default styles (for larger screens) */
body {
font-size: 16px;
margin: 20px;
}
/* Styles for screens with a width less than 768px (typical mobile devices) */
@media (max-width: 768px) {
body {
font-size: 14px;
margin: 10px;
}
}
/* Styles for screens with a width of 768px or greater (typical tablets) */
@media (min-width: 769px) {
body {
font-size: 16px;
margin: 20px;
}
}
In this example, the body font size is reduced on smaller screens, and the margin is increased. This ensures the content is readable on mobile devices.
Example 2: Image Responsiveness
/* Default styles (for larger screens) */
.image-container {
width: 300px;
height: 200px;
margin: 20px auto;
border: 1px solid #ccc;
}
/* Styles for screens with a width less than 768px (typical mobile devices) */
@media (max-width: 768px) {
.image-container {
width: 100%;
height: auto;
}
}
Here, the image container's width is reduced on smaller screens, allowing it to fit within the viewport.
Example 3: Conditional Styling - Different Fonts
/* Default styles (for larger screens) */
h1 {
font-size: 2em;
}
/* Styles for screens with a width less than 768px (typical mobile devices) */
@media (max-width: 768px) {
h1 {
font-size: 1.5em;
}
}
This example demonstrates how to apply different font sizes based on screen size.
Key Takeaways
- Media Queries are a fundamental part of responsive design.
- They allow you to create websites that adapt to different devices and screen sizes.
- Understanding media features is essential for effective responsive design.
- Experiment with different media queries to achieve the desired look and feel for your website.
💡 Tip: Use browser developer tools (like Chrome DevTools or Firefox Developer Tools) to simulate different screen sizes and observe how your website adapts. This is invaluable for testing your media queries.
🖥️ Try It Yourself
-
Create a Simple HTML File: Create a new HTML file (e.g.,
index.html) and include a basic HTML structure:<!DOCTYPE html> <html> <head> <title>Responsive Example</title> </head> <body> <h1>Welcome to the Responsive Example</h1> <p>This is a simple example demonstrating responsive design.</p> </body> </html> -
Open in a Browser: Open the
index.htmlfile in your web browser. -
Resize the Browser Window: Resize your browser window to simulate different screen sizes. You should see the heading and paragraph change size accordingly.
-
Experiment: Try changing the
max-widthvalue in the@mediaqueries to see how the styles change. Try adding more media features to your queries for more complex scenarios.

