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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • Programmatic Brand Extraction: Pulling Logos, Colors, and Assets from Any URL
  • Liquibase: Database Change Management and Automated Deployments
  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch

Trending

  • DevOps and Platform Engineering Readiness Checklist: Everything Needed for a Scalable, Secure, High-Velocity Delivery Platform
  • Liquibase: Database Change Management and Automated Deployments
  • Spec-Driven Integration: Turning API Sprawl Into a Governed Capability Fleet for AI
  • Why Google Data Migration Gets Stuck at 99%: Causes and Proven Fixes
  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.8K 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

  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • Programmatic Brand Extraction: Pulling Logos, Colors, and Assets from Any URL
  • Liquibase: Database Change Management and Automated Deployments
  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook