CSS本机变量在媒体查询中不起作用 [英] CSS native variables not working in media queries

查看:196
本文介绍了CSS本机变量在媒体查询中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在媒体查询中使用CSS变量,但它不起作用.

I am trying to use CSS variables in media query and it does not work.

:root {
  --mobile-breakpoint: 642px;
}

@media (max-width: var(--mobile-breakpoint)) {

}

推荐答案

来自

var()函数可以代替值中的任何部分 元素上的任何属性. var()函数不能用作 属性名称,选择器或除属性值之外的其他任何内容. (这样做通常会产生无效的语法,否则会产生一个其值 含义与变量无关.)

The var() function can be used in place of any part of a value in any property on an element. The var() function can not be used as property names, selectors, or anything else besides property values. (Doing so usually produces invalid syntax, or else a value whose meaning has no connection to the variable.)

因此,您不能在媒体查询中使用它.

So no, you can't use it in a media query.

那是有道理的.因为您可以设置--mobile-breakpoint例如到:root,即<html>元素,并从那里继承到其他元素.但是媒体查询不是元素,它不能从<html>继承,因此无法正常工作.

And that makes sense. Because you can set --mobile-breakpoint e.g. to :root, that is, the <html> element, and from there be inherited to other elements. But a media query is not an element, it does not inherit from <html>, so it can't work.

这不是CSS变量要完成的任务.您可以改用CSS预处理器.

This is not what CSS variables are trying to accomplish. You can use a CSS preprocessor instead.

这篇关于CSS本机变量在媒体查询中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆