Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables in Stylus CSS Media Queries

Tags:

css

stylus

I've tried everything but I can't get the Stylus CSS preprocessor to use variables in media queries.

For instance this doesn't work:

t = 1000px  @media screen and (max-width: t)     html         background blue 

Anyone know how to do this?

like image 468
Cory Avatar asked Oct 25 '12 02:10

Cory


People also ask

Can you use CSS variables in media queries?

Well... if you're using CSS custom properties (CSS variables) and wanted to reference them in a media query, you probably discovered that you can't use custom properties in this context. CSS custom properties weren't designed for that use case. Bummer!

What does @media in CSS mean?

The @media CSS at-rule can be used to apply part of a style sheet based on the result of one or more media queries. With it, you specify a media query and a block of CSS to apply to the document if and only if the media query matches the device on which the content is being used.

Are CSS media queries still used?

And even though media queries are still a valid tool to create responsive interfaces, there are many situations where it's possible to avoid using width at all. Modern CSS allow us to create flexible layouts with CSS grid and flex that adapts our content to the viewport size without a need to add breakpoints.

What does @media do in SCSS?

The @media directive could be embedded within the SASS selector but is popped to the top of the stylesheet. The @media directive is one of the commonly used methods for ensuring the media content is displaying in a responsive way.


2 Answers

It looks like stylus supports a little cleaner way to do the same thing as of this pull request.

Here, given a block size, I can make styles that center the container in the page, and set the container size to be 1, 2, or 3 blocks wide based on the browser size. Letting the media query be a variable (instead of sticking variables inside the media query line) makes it a bit cleaner than using the unquote method metioned above.

/* in app.styl */  full_block_width = 300px  three_blocks = "all and (min-width: " + 3*full_block_width + ")" two_blocks = "all and (min-width: " +  2*full_block_width + ") and (max-width: " + (3*full_block_width - 1) + ")" one_block = "all and (max-width: " + (2*full_block_width - 1) + ")"  .container    margin: auto  @media three_blocks   .container      width: (3*full_block_width)  @media two_blocks   .container      width: (2*full_block_width)  @media one_block   .container      width: full_block_width 
like image 156
mr rogers Avatar answered Sep 18 '22 08:09

mr rogers


It's sad, but you can't use variables or interpolations on media queries. I stumbled upon similar task yesterday and came with this solution:

t = 1000px  unquote("@media screen and (max-width: " + t + ") {") html     background blue unquote("}") 

This is not pretty, but it works — you can use unquote to workaround most of the such Stylus problems actually.

like image 39
kizu Avatar answered Sep 22 '22 08:09

kizu