我有css文件列表,每个css文件都有不同的图像位置代码,如url(../_ images / headerImages.jpg),现在我将我的图像放到其他域,因此我想创建一个变量比如var domainPath = http://sitename.com所以在每个url(../)我都可以写一些像url(domainPath +'/ images / headerImages.jpg')。
实施例
var DOMAIN_PATH = http://www.mysite.com;
#header {background: url(DOMAIN_PATH/_images/headerimage.jpg)no-repeat; }
请帮帮我..
答案 0 :(得分:3)
您不能在纯CSS中使用变量。我建议使用CSS预处理器,这是一种编译为CSS的语言。 SASS非常棒,您可以按照自己的意愿行事:
$domainpath: url(http://www.mysite.com/_images/bg-header-noodletools-express.jpg);
#header {background: $domainpath no-repeat; }
另一种选择是LESS。
答案 1 :(得分:0)
正如 https://ishadeed.com/article/css-vars-101/ 和其他人提到的,现在可以在 CSS 中使用变量了。
你可以这样做:
:root {
--domainpath: url(http://www.yourdomain.com/_images/bg-header-noodletools-express.jpg);
}
#header {
background: var(--domainpath) no-repeat;
}