Academic Block

CSS Media Queries Get Certified

What are CSS Media Queries?

CSS Media Queries let you apply different styles depending on the characteristics of the user’s device — most commonly the width of the viewport. They are the foundation of responsive web design and allow one website to look good on phones, tablets, and desktops without creating separate pages.

Why Use Media Queries?

Media queries let you change fonts, spacing, layout direction, navigation style, and almost any other CSS property when the screen becomes narrower or wider. This keeps content readable and usable on every device.

Common Media Query Breakpoints

Device
Screen Width
Common Usage
Mobile
0px – 767px
Phones and small devices.
Tablet
768px – 991px
Tablets and small laptops.
Desktop
992px and above
Desktop computers and large screens.

Basic Media Query Syntax

What this code does: Demonstrates the fundamental structure of a media query. Styles inside the @media (max-width: 768px) block are applied only when the browser window is 768 pixels wide or narrower.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    margin: 0;
}
@media (max-width: 768px) {
    body {
        background: lightblue;
    }
}
</style>
</head>
<body>
<h2>Resize the browser window</h2>
</body>
</html>

Step 1: Change the Background Color

What this code does: Starts with a white background and switches it to light blue when the viewport becomes 768 px wide or narrower. This is the simplest practical use of a media query.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    background: white;
    font-family: Arial, sans-serif;
    padding: 24px;
    margin: 0;
}
@media (max-width: 768px) {
    body {
        background: lightblue;
    }
}
</style>
</head>
<body>
<h2>Resize the browser window</h2>
</body>
</html>

Step 2: Change Font Size

What this code does: Sets a large 40 px heading on desktop and reduces it to a more comfortable 24 px on screens 768 px wide or smaller, improving readability on mobile devices.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    margin: 0;
}
.demo-title {
    font-size: 40px;
}
@media (max-width: 768px) {
    .demo-title {
        font-size: 24px;
    }
}
</style>
</head>
<body>
<h1 class="demo-title">Responsive Heading</h1>
</body>
</html>

Step 3: Create a Responsive Navigation

What this code does: Displays navigation links in a horizontal row on desktop and stacks them vertically on screens 768 px wide or smaller by changing flex-direction inside a media query.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    margin: 0;
}
.demo-nav {
    display: flex;
    gap: 10px;
}
.demo-nav a {
    padding: 10px 16px;
    background: #007BFF;
    color: white;
    text-decoration: none;
    border-radius: 4px;
}
@media (max-width: 768px) {
    .demo-nav {
        flex-direction: column;
    }
}
</style>
</head>
<body>
<nav class="demo-nav">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</nav>
</body>
</html>

Step 4: Create a Responsive Flexbox Layout

What this code does: Places two equal-width boxes side-by-side on desktop and stacks them on top of each other on screens 768 px wide or smaller by changing the flex direction inside a media query.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    margin: 0;
}
.demo-container {
    display: flex;
    gap: 10px;
}
.demo-box {
    flex: 1;
    background: #00a8ff;
    padding: 40px;
    color: white;
    text-align: center;
    border-radius: 8px;
}
@media (max-width: 768px) {
    .demo-container {
        flex-direction: column;
    }
}
</style>
</head>
<body>
<div class="demo-container">
<div class="demo-box">Box 1</div>
<div class="demo-box">Box 2</div>
</div>
</body>
</html>

Step 5: Complete Responsive Webpage

What this code does: Combines a responsive header and a two-column content area. On screens 768 px wide or smaller the columns stack and the header font size is reduced, producing a complete mobile-friendly layout.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    margin: 0;
    font-family: Arial, sans-serif;
    padding: 0;
}
.demo-header {
    background: #007BFF;
    color: white;
    padding: 20px;
    text-align: center;
}
.demo-container {
    display: flex;
}
.demo-box {
    flex: 1;
    padding: 40px;
    text-align: center;
    background: #f4f4f4;
    border: 1px solid #ddd;
}
@media (max-width: 768px) {
    .demo-container {
        flex-direction: column;
    }
    .demo-header {
        font-size: 20px;
    }
}
</style>
</head>
<body>
<header class="demo-header">Responsive Website</header>
<div class="demo-container">
<div class="demo-box">Content 1</div>
<div class="demo-box">Content 2</div>
</div>
</body>
</html>

Advantages of CSS Media Queries

Advantage
Benefit
Description
Responsive Design
Better User Experience
Webpages adapt automatically to different devices.
Single Website
Easy Maintenance
No need to create separate desktop and mobile websites.
Flexible Layout
Modern Design
Adjusts layouts, fonts, and images easily.
Improved Accessibility
Easy Reading
Content remains readable on all screen sizes.

Best Practices

  • Always include the viewport meta tag: <meta name="viewport" content="width=device-width, initial-scale=1">.
  • Consider a mobile-first approach (base styles for small screens, then add larger breakpoints).
  • Use flexible units (%, rem, fr) together with media queries instead of fixed pixel widths.
  • Choose breakpoints that match your actual content rather than arbitrary device sizes.
  • Test the layout by resizing the browser and on real phones and tablets.
  • Combine media queries with Flexbox or CSS Grid for powerful responsive layouts.
  • Keep media-query rules organized (group related changes together).
  • Avoid creating too many breakpoints — usually two or three are enough for most sites.

Web Resources on CSS Media Queries

1. MDN Web Docs : Using media queries
2. Web.dev : Media queries
3. CSS Tricks.com : CSS Media Queries Guide
4. Dev.to : Using CSS Media Queries

🧪 Test Your CSS Code

Edit the CSS code on the left and click “Run Code” to see the result on the right.

📝 CSS Code
👁️ Preview (HTML rendered with your CSS)

Click “Run Code” to see the result here.