DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Why Database Migrations Take Months and How to Speed Them Up
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • How Trustworthy Is Big Data?
  • Subtitles: The Good, the Bad, and the Resource-Heavy

Trending

  • Understanding and Mitigating IP Spoofing Attacks
  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • Using Python Libraries in Java
  • Teradata Performance and Skew Prevention Tips
  1. DZone
  2. Data Engineering
  3. Databases
  4. CSS Variables in Media Queries

CSS Variables in Media Queries

How to use the syntax of custom media queries and convert them to a numeric data type syntax that is supported by browsers.

By 
Alexey Shepelev user avatar
Alexey Shepelev
DZone Core CORE ·
Oct. 12, 21 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
12.0K Views

Join the DZone community and get the full member experience.

Join For Free

Many novice developers find it difficult to read nested properties used in preprocessors using the "&" symbol. Instead of formulas and mixins, they prefer to write regular rules. And instead of SASS variables, they use CSS custom properties. In this case, the only reason to continue using SASS is the ability to add variable breakpoints into media queries to change the layout. For example, $tablet: 768px or $dektop: 1100px.

This is why I’m very glad to learn about the existence of the PostCSS plugin PostCSS Custom Media. This allows you to use the syntax of custom media queries from the draft Media Queries Level 5 specification and convert them to a numeric data type syntax that is supported by browsers.

In addition to the basic description on the main page of the plugin, a separate document contains examples of its use with the help of gulp, webpack, etc., as well as in Node.js.

Excerpt From Specification

When creating documents that use media queries, the same expressions may be repeated in different places, for example, to specify multiple @import statements. Repeating media queries may be risky. If a breakpoint needs to be changed, the developer will have to change the values in all cases. If the change is required in some particular case, it will be quite difficult to catch the errors that have arisen.

To avoid this, the specification describes methods for defining custom media queries, which are simply-named aliases for longer and more complex regular media queries. Thus, a media query used in several places can be replaced with one custom query that will be reused as many times as needed. And if you need to change its value, you will have to do this only once.

Example

To see how the plugin works, let’s consider a simple example of its use. I will be using gulp.

Announcement

As shown on the plugin page, the first step is to declare a custom media query. This will be the screen size to which the alternate CSS rule will apply.

CSS
 
@custom-media --desktop (min-width: 900px);
  1. A custom media query declaration starts with the @custom-media statement. It is an alternative to the var keyword in JavaScript and creates a variable.
  2. The second place is occupied by the custom media query name --desktop, which, like CSS variables, starts with two hyphens.
  3. And then comes the condition, which in this case is the screen-width breakpoint.

Use in Styles

After creating a variable, you can use it in the media query instead of the condition.

CSS
 
body {
  background-color: gray;
}

@media (--desktop) {
  body {
    min-block-size: 100vh;   
    background-color: red;
  }
}

Gulp Configuration

My gulpfile.js configuration code given in the documentation did not work, so I changed it a little.

JavaScript
 
const gulp = require("gulp")
const postcss = require("gulp-postcss");
const postcssCustomMedia = require("postcss-custom-media");
const rename = require("gulp-rename");

gulp.task("css", function() {
  return gulp.src("./src/style.css")
    .pipe(postcss([
      postcssCustomMedia()
    ]))
    .pipe(rename("style.css"))
    .pipe(gulp.dest("."))
});

Result

After processing, we get the following regular styles:

CSS
 
body {
  background-color: gray;
}

@media (min-width: 900px) {
  body {
    min-block-size: 100vh;   
    background-color: red;
  }
}

Conclusion

I’m very glad that we’ve overcome the last barrier to pure CSS. I wish you all a pleasant web development!

Database CSS Media (communication) Media queries

Opinions expressed by DZone contributors are their own.

Related

  • Why Database Migrations Take Months and How to Speed Them Up
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • How Trustworthy Is Big Data?
  • Subtitles: The Good, the Bad, and the Resource-Heavy

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!