Home 〉 CSS Tutorials 〉 CSS Media Query Tutorial for Responsive Design with Examples
CSS Media Query Tutorial for Responsive Design with Examples !
Want your site to look good on all screen sizes? Media queries help you apply CSS rules based on screen width, height, resolution, or device type. Learn how to build responsive layouts for mobiles, tablets, and desktops. It’s essential for modern web design.
CSS Media Queries are a powerful feature in CSS that let you apply styles depending on the screen size, resolution, orientation, and more. It's the core of responsive web design.
In this tutorial, we'll cover:
A CSS media query lets you conditionally apply CSS styles based on the user's device or screen. This is useful for adjusting layouts and design for mobile, tablet, and desktop views.
Syntax ✍
@media media-type and (media-feature) {
/* CSS rules here */
}
Example 📄
@media screen and (max-width: 768px) {
body {
background-color: lightblue;
}
}
Meaning: When the screen width is 768px or smaller, the background color of the body will be light blue.
Media Feature | Description |
---|---|
'max-width' | Target screens smaller than a given width |
'min-width' | Target screens larger than a given width |
'max-height' | Similar but for height |
'orientation' | Detects landscape or portrait mode |
'resolution' | Based on pixel density |
<!DOCTYPE html>
<html>
<head>
<style>
.para {
font-size: 14px;
background-color: white;
padding: 20px;
border: 1px solid black;
border-radius: 10px;
}
@media (max-width: 600px) {
.para {
background-color: yellow;
}
}
</style>
</head>
<body>
<p class="para">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nostrum minima facilis necessitatibus soluta eligendi a qui cum, provident voluptatibus iure aut nulla dicta atque ipsum impedit perferendis placeat. Perferendis, quidem?</p>
</body>
</html>
output 📌
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nostrum minima facilis necessitatibus soluta eligendi a qui cum, provident voluptatibus iure aut nulla dicta atque ipsum impedit perferendis placeat. Perferendis, quidem?
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Explanation: On screens smaller than 600px, the background color turns to yellow.
<!DOCTYPE html>
<html>
<head>
<style>
.container {
width: 100%;
background: coral;
padding: 20px;
}
@media (min-width: 768px) {
.container {
width: 50%;
margin: auto;
background: pink;
}
}
</style>
</head>
<body>
<div class="container">Responsive Box</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Explanation: The '.container' takes full width on mobile and 50% width on tablets and above.
Example 📄
@media (orientation: landscape) {
body {
background-color: #f9f9f9;
}
}
Explanation: The background color will change only when the screen is in landscape mode.
Example 📄
@media screen and (min-width: 600px) and (orientation: portrait) {
.menu {
display: block;
}
}
Explanation: Styles apply only if the screen is 600px or wider and in portrait mode.
Device | Width Range |
---|---|
Mobile | 0 - 600px |
Tablet | 601px - 1024px |
Desktop | 1025px and above |
Use these as your base for writing responsive media queries.
CSS media queries are the foundation of modern, responsive websites. They help you build layouts that look great on all screen sizes—from mobile phones to large monitors. Practice using breakpoints and writing flexible CSS to improve your frontend skills.
To apply CSS styles based on device properties like width, height, and orientation.
Yes, all modern browsers fully support media queries.
Yes, you can use as many as needed for different screen sizes.
Yes, it's a good practice for performance and better UX on smaller devices.