Stylus CSS媒体查询中的变量

问题描述 投票:26回答:10

我已经尝试了一切,但我无法让Stylus CSS预处理器在媒体查询中使用变量。

例如,这不起作用:

t = 1000px

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

有人知道怎么做吗?

css stylus
10个回答
23
投票

很遗憾,但您不能在媒体查询中使用变量或插值。昨天我偶然发现了类似的任务,并提出了这个解决方案:

t = 1000px

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

这不是很好,但它可以工作 - 您可以使用unquote来解决大多数此类Stylus问题。


0
投票

我喜欢Tony Tai Nguyen的回答。这是另一个迭代:

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...

31
投票

看起来手写笔支持一种更清洁的方式来做同样的事情,这个pull request

在这里,给定块大小,我可以创建使容器在页面中居中的样式,并根据浏览器大小将容器大小设置为1,2或3个块宽。让媒体查询成为变量(而不是在媒体查询行中粘贴变量)使其比使用上面提到的unquote方法更清晰。

/* 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

5
投票

这对我有用。

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

5
投票

使用Stylus的0.43.0版本,您可以在示例中使用媒体查询,也可以不使用冒号来支持媒体查询:

t = 1000px

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

via Github


4
投票

现在支持开箱即用,snippet from official page

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

3
投票

我写了一个mixin,虽然它也不是完全理想的:

// 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))

它可以像这样使用:

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

CSS输出:

@media  screen and (min-width: 768px) {
  .container {
    max-width: 728px;
  }
}

2
投票

现在应该工作:

t = 1000px

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

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

从文档:

插值和变量

您可以在媒体查询中使用插值和变量,因此可以执行以下操作:

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

这会产生

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

也可以在MQ中使用表达式:

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

屈服于

@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;


 }
}

1
投票

如果我可以提供干净和可读的方式,我会像我这样使用哈希:

// 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)",
}

我将如何称之为例如:

.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

超级明显,易读。保持简洁。


0
投票

我想创建一个media mixin,这使得不必为媒体查询创建一个命名变量:

media(query)
  @media query
    {block}

使用方法如下:

+media("screen and (min-width:" + width + "), print")
  .class
    foo: bar
© www.soinside.com 2019 - 2024. All rights reserved.