【发布时间】:2018-01-27 05:04:28
【问题描述】:
我想在一个 css 文件中定义全局变量,并在其他 css 文件中使用这些变量。
这可能吗?
Global.css:
:root {
--main-color: #192100;
--main-background: #89b66b;
}
html, body {
min-height: 100%;
width: 100%;
font-family: Arial;
color: var(--main-color);
}
SomeFile.css:
.some-rule {
display: table;
border-radius: 12px;
border: 4px solid var(--main-color);
padding-left: 10px;
padding-right: 10px;
color: var(--main-color);
}
Html:(Global.css 如果在 SomeFile.css 之前引用)
<link href="Global.css" rel="stylesheet" type="text/css" />
<link href="SomeFile.css" rel="stylesheet" type="text/css" />
【问题讨论】:
-
等待 CSS4 或使用 sass
-
为什么不把一个根类添加到你想要的元素中呢?