Variables in Stylus CSS Media Queries
Asked Answered
S

10

28

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?

Shrewish answered 25/10, 2012 at 2:29 Comment(2)
This may or may not be helpful, but Sass can do it easily.Calvary
I know, and I'm sure LESS can do it too, but I just really prefer Stylus' syntax and this is the only hurdle I haven't been able to leap. :(Shrewish
O
26

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.

Ogham answered 25/10, 2012 at 8:18 Comment(0)
D
33

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
Decamp answered 3/2, 2013 at 1:39 Comment(0)
O
26

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.

Ogham answered 25/10, 2012 at 8:18 Comment(0)
H
6

With version 0.43.0 of Stylus, media queries are supported either as you have it in your example or without a colon like this:

t = 1000px

@media screen and (max-width t)
    html
        background blue

via Github

Hamlett answered 15/7, 2014 at 19:3 Comment(0)
Q
5

This is what worked for me.

medium = 'screen and (min-width: 768px)'
large = 'screen and (min-width: 992px)'
xlarge = 'screen and (min-width: 1200px)'

.box
    background: #000
    @media medium
        background: #111
    @media large
        background: #222
    @media xlarge
        background: #333
Quandary answered 14/1, 2015 at 13:25 Comment(0)
Y
4

This is now supported out of the box, snippet from official page:

foo = 'width'
bar = 30em
@media (max-{foo}: bar)
body
    color #fff
Yasmineyasu answered 14/5, 2015 at 10:1 Comment(0)
S
3

I wrote a mixin, although it's not completely ideal either:

// media
media(args...)
  output = null
  for arg in args
    // check for tuple
    if arg[1]
      push(output,unquote('(%s: %s)' % (arg[0] arg[1])))
    else
      push(output,unquote(arg))

  unquote(s('%s',output))

It can be used like this:

$_media = media(screen,'and',(min-width $screen-tablet))
@media $_media
  .container
    max-width 728px

CSS Output:

@media  screen and (min-width: 768px) {
  .container {
    max-width: 728px;
  }
}
Scrawly answered 3/6, 2013 at 17:34 Comment(0)
T
3

Should work now:

t = 1000px

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

http://stylus-lang.com/docs/media.html

From the documentation:

Interpolations and variables

You can use both interpolations and variables inside media queries, so it is possible to do things like this:

foo = 'width'
bar = 30em
@media (max-{foo}: bar)
  body
    color #fff

This would yield

@media (max-width: 30em) {
  body {
    color: #fff;
  }
}

It is also possible to use expressions inside MQ:

.foo
  for i in 1..4
    @media (min-width: 2**(i+7)px)
      width: 100px*i

would yield to

@media (min-width: 256px) {
  .foo {
    width: 100px;
  }
}
@media (min-width: 512px) {
  .foo {
    width: 200px;
  }
}
@media (min-width: 1024px) {
  .foo {
    width: 300px;
  }
}
@media (min-width: 2048px) {
  .foo {
    width: 400px;


 }
}
Trestlework answered 10/2, 2015 at 19:50 Comment(1)
It's not quite everything. For instance, I can't do this: small-min = '(min-width: ' + lower-bound(small-range) + ')' large-max = '(max-width: ' + upper-bound(large-range) + ')' @media screen {small-min} and {large-max}Marimaria
S
1

If I may provide a clean and readable way, I use hashes to my advantage like so:

// Custom media query sizes
--query = {
    small: "screen and (min-width: 554px)",
    medium: "screen and (min-width: 768px)",
    large: "screen and (min-width: 992px)",
    extra-large: "screen and (min-width: 1200px)",
}

And how I would call it for example:

.main-logo
    font-large(--font.uni-sans-heavy)
    position: relative
    top: 50px
    left: 35px

    .logo-first
        color: --color.white
        margin-right: 3px

    .logo-second
        color: --color.bright-red

    @media --query.large
        left: 70px

Super obvious, and easy to read. Keep it nice and simple.

Seasoning answered 6/5, 2016 at 10:56 Comment(0)
I
0

I like to create a media mixin which makes it unnecessary to create a named variable for the media query:

media(query)
  @media query
    {block}

Usage proceeds as follows:

+media("screen and (min-width:" + width + "), print")
  .class
    foo: bar
Irreproachable answered 30/10, 2014 at 17:51 Comment(0)
S
0

I liked the answer from Tony Tai Nguyen. Here's another iteration:

sizes = {
  mobile: 0 768
  tablet: 769 1023
  desktop: 1024 1215
  widescreen: 1216 1407
  fullhd: 1408 99999999
}
query = {}

for name, pxs in sizes
  min = '(min-width:' + pxs[0] + 'px)'
  max = '(max-width:' + pxs[1] + 'px)'
  query[name] = min + ' and ' + max
  query[name + '-up'] = 'screen and ' + min
  query[name + '-down'] = 'screen and ' + max

// Usage: @media query.mobile or @media query.tablet-up etc...
Substructure answered 9/12, 2017 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.