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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups
  • Should Developers Switch From SQLite to DuckDB?
  • Adding Graph Retrieval to a RAG Pipeline With Python
  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance

Trending

  • Build a Voice Assistant With Gemini Live API
  • Part 4: Multi-Agent Orchestration — Pairing Goose With Quarkus Flow State Machines
  • Designing the Runtime Layer for Stateful AI Agents: Where Agent Substrate Fits
  • PWA on iOS, Part 3: Chrome on iPhone Is WebKit Too. How One Engine Sets the Rules for PWAs on iOS

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
DZone Core CORE ·
Oct. 12, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
13.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups
  • Should Developers Switch From SQLite to DuckDB?
  • Adding Graph Retrieval to a RAG Pipeline With Python
  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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