CSS Media Queries
CSS Media Queries are used to apply CSS styles according to the screen size.
- Media queries detect device features like screen width, height, and resolution.
- Breakpoints define the screen sizes where the design needs to change.
- They ensure a smooth, user-friendly experience across all devices.
Syntax:
@media mediatype and (condition) {
/* CSS styles */
}
<style>
.gfg {
color: black;
}
@media screen and (max-width: 500px) {
.gfg {
color: green;
}
}
</style>
- Media queries apply CSS rules based on device characteristics like screen width.
- In your code, screens ≤500px wide change .gfg text color to green.
Note: It is compulsory to add the viewport meta tag for media queries to work correctly. Learn more about it here.
Media Types in CSS
Media types specify which devices the styles should apply to. Commonly used types include:
Media Type | Description |
---|---|
all | Suitable for all media devices. |
print | Used for printers. |
screen | Targeted at computer screens, tablets, smartphones, etc. |
speech | Designed for screen readers that read the content aloud. |
More Examples of CSS Media Queries
Media Queries for Multiple Screen Sizes
<style>
.gfg {
color: black;
}
@media screen and (max-width: 800px) {
.gfg {
color: blue;
}
}
@media screen and (max-width: 500px) {
.gfg {
color: green;
}
}
</style>
In this Example:
- Styles adjust dynamically based on screen width using media queries.
- For screens 800px or smaller, the text color changes to blue.
- For screens 500px or smaller, the text color changes to green.
Media Queries for Multiple Screen Sizes with Additional Styles
<style>
.gfg {
color: black;
font-size: 20px;
padding: 10px;
}
@media screen and (max-width: 800px) {
.gfg {
color: blue;
font-size: 18px;
}
}
@media screen and (max-width: 500px) {
.gfg {
color: green;
font-size: 16px;
text-align: center;
}
}
</style>
In this example:
- Styles dynamically adjust based on screen width using media queries.
- For screens 800px or smaller, the text color becomes blue, and the font size decreases to 18px.
- For screens 500px or smaller, the text color changes to green, the font size reduces to 16px, and the text is center-aligned.
CSS Media Query Features
Media queries allow developers to check various device characteristics. Here are some important features:
Feature | Description |
---|---|
color | Specifies the number of bits per color component for the device. |
grid | Checks whether the device is grid or bitmap. |
height | Represents the height of the viewport. |
aspect ratio | Defines the width-to-height ratio of the viewport. |
color-index | Indicates how many colors the device can display. |
max-resolution | The highest resolution the device can achieve, measured in dpi or dpcm. |
monochrome | Shows the number of bits per color on a monochrome device. |
scan | Refers to the method of scanning used by the output device. |
update | Describes how fast the device can update its display. |
width | Represents the width of the viewport. |
Why Use Media Queries?
- Improves user experience: Responsive designs provide an optimal viewing experience, making it easier for users to interact with the website.
- Ensures functionality: Media queries help ensure that all content is accessible and looks good, regardless of the device being used.
- Boosts SEO: Search engines prioritize mobile-friendly sites. A responsive design improves your website’s search engine ranking.
Best Practices for Media Query
- Start with a mobile-first approach, then enhance for larger screens.
- Use relative units like em or rem for flexibility.
- Test designs across multiple devices and browsers.
CSS Media Queries – FAQs
What are media queries in CSS?
Media queries in CSS allow developers to apply different styles depending on the characteristics of the device, such as screen size, resolution, or orientation. This makes websites responsive across various devices.
Can media queries detect device orientation?
Yes, media queries can detect device orientation using the orientation feature. This allows you to apply different styles based on whether the device is in portrait or landscape mode.
What is the difference between ‘min-width’ and ‘max-width’ in media queries?
min-width applies styles when the viewport is at least the specified width, supporting a mobile-first approach. max-width applies styles up to the specified width, often used in desktop-first designs.
Can media queries be combined using logical operators?
Yes, you can combine media queries using operators like and, not, and only for more specific conditions.
How do media queries enhance accessibility?
Media queries detect user preferences, such as reduced motion or high contrast settings, allowing styles to be adjusted for improved accessibility.