DEV Community

Onyedikachi Emmanuel Nnadi
Onyedikachi Emmanuel Nnadi

Posted on

New Css Media Query Syntax(Media Query Level 4 or MQ4)

The new CSS media query syntax, often referred to as "Media Query Level 4" or "MQ4", introduces several improvements and new features. Here are the key aspects of the new syntax:

1.Range Syntax:
Instead of using min-width and max-width, you can now use a more concise range syntax:
@media (width >= 768px) and (width <= 1024px) {
/* Styles for screens between 768px and 1024px */
}

2.Comparison Operators:
You can use <, >, <=, >= for more intuitive comparisons:
@media (width > 1200px) {
/* Styles for screens wider than 1200px */
}

3.'not' Keyword:
The 'not' keyword can be used for inverse conditions:
@media not (color) {
/* Styles for non-color devices */
}

4.Custom Media Queries:
You can define reusable media query aliases:
@custom-media --small-viewport (width <= 30em);

@media (--small-viewport) {
/* Styles for small viewports */
}

5.Media Query Ranges:
You can use the 'to' keyword for ranges:
@media (400px <= width <= 700px) {
/* Equivalent to the above */
}

6.Improved Logical Operators:
'and', 'or', and 'not' can be used more flexibly:
@media ((hover: hover) and (pointer: fine)) or (width > 1200px) {
/* Styles for devices with hover and fine pointer, or large screens */
}

7.Feature Detection:
New features for detecting device capabilities:
@media (hover: hover) {
/* Styles for devices that support hover */
}

8.Aspect Ratio:
Easier syntax for targeting aspect ratios:

@media (aspect-ratio: 16/9) {
/* Styles for 16:9 aspect ratio */
}

It's important to note that while these features are part of the CSS Media Queries Level 4 specification, browser support may vary. Always check the current browser support before using these features in production. For wider compatibility, you might need to provide fallbacks or use a preprocessor that compiles to the older syntax for browsers that don't yet support the new features.

Top comments (0)