The Media query in CSS is used to create a responsive web design. It means that the view of a web page differs from system to system based on screen or media types. The breakpoint specifies for what device-width size, the content is just starting to break or deform. The CSS2 rule introduced different style rules for different media types. Media queries can be used to check many things:
- width and height of the viewport
- width and height of the device
- Orientation
- Resolution
A media query consists of a media type that can contain one or more expressions that can be either true or false. The query’s result is true if the specified media matches the type of device the document is displayed on. If the media query is true then a style sheet is applied.
Syntax:
@media not | only mediatype and (expression) {
// Code content
}
@media screen and (min-width: 720px)Â {
 body {
    background-color: crimson;
  }
}
@media screen and (min-width: 720px)Â {
  #rightsidebar {width: 300px; float: right; height: 150px}
  #main {margin-right: 200px;}
}
This code snippet ensures that a menu will float to the left side of the page when the viewport is 720 pixels wide or wider. If the viewport is less than 720 pixels, the menu will be positioned on top of the content.
Example: Implementation of the CSS media query with the different device width for making it responsive.
HTML
<!DOCTYPE html>
<html>
<head>
<title>CSS media query</title>
<style>
body {
text-align: center;
}
.gfg {
font-size: 40px;
font-weight: bold;
color: green;
}
@media screen and (max-width:800px) {
body {
text-align: center;
background-color: green;
}
.gfg {
font-size: 30px;
font-weight: bold;
color: white;
}
.geeks {
color: white;
}
}
@media screen and (max-width:500px) {
body {
text-align: center;
background-color: blue;
}
}
</style>
</head>
<body>
<div class="gfg">
GeeksforGeeks
</div>
<div class="geeks">
A computer science portal for geeks
</div>
</body>
</html>
|
Output: From the output, we can see that if the max-width of the screen is reduced to 800px then the background color changes to green & if the max-width of the screen is reduced to 500px then the background color will turn to blue. For the desktop size width, the background color will be white.

Media Types in CSS: There are many types of media types which are listed below:
|
all | It is used for all media devices. |
print | It is used for printers. |
screen | Used for computer screens, smartphones, etc. |
speech | Used for screen readers that read the screen aloud. |
Features of Media query: There are many features of media query which are listed below:
|
color | The number of bits per color component for the output device. |
grid | Checks whether the device is grid or bitmap. |
height | The viewport height. |
aspect ratio | The ratio between the width and height of the viewport. |
color-index | The number of colors the device can display. |
max-resolution | The maximum resolution of the device using dpi and dpcm. |
monochrome | The number of bits per color on a monochrome device. |
scan | The scanning of output devices. |
update | How quickly can the output device modify. |
width | The viewport width. |
Supported Browsers
- Chrome 21.0
- Mozilla 3.5
- Microsoft Edge 9.0
- Opera 9.0
- Safari 4.0
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
17 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...