Reputation: 604
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?
Upvotes: 28
Views: 11154
Reputation: 3260
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
Upvotes: 33
Reputation: 17601
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;
}
}
Upvotes: 3
Reputation: 652
This is now supported out of the box, snippet from official page:
foo = 'width'
bar = 30em
@media (max-{foo}: bar)
body
color #fff
Upvotes: 4
Reputation: 1856
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
Upvotes: 6
Reputation: 1
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...
Upvotes: 0
Reputation: 1653
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.
Upvotes: 1
Reputation: 217
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
Upvotes: 5
Reputation: 3547
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
Upvotes: 0
Reputation: 111
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;
}
}
Upvotes: 3
Reputation: 43244
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.
Upvotes: 26