可以在css“background url”中使用css变量吗

时间:2017-09-15 08:05:57

标签: css background-image css-variables css-import

我想在CSS文件中设置一个包含我所有图片的基本域,这是我尝试的内容:

global.css中

:root
{
  --bgd: #C0C0C0;
  --picdomain: "https://somedomain.com/";
}

s1.css

@import url("global.css");

body
{
  background-color: var(--bgd); //works if only it exist
  background: url(var(--picdomain)"some/path/images/pic.png"); //no effect
}

在html中加载s1.css

<link rel="stylesheet" type="text/css" href="s1.css">

HTML的背景颜色确实发生了变化,但背景没有显示出来。所以我尝试了其他方式:

global.css中

@picdomain: "https://somedomain.com";

s1.css

background: url("@picdomain/some/path/images/pic.png");  //can't read variable from imported css

没有帮助

仅在我为图片设置完整网址时才有效,如下所示:

global.css中

:root
{
  --bgd: #C0C0C0;
  --picdomain: url("https://somedomain.com/some/path/images/pic.png");
}

s1.css

@import url("global.css");

body
{
  background-color: var(--bgd); //works if only it exist
  background: var(--picdomain); //no effect
}

但这不是我想要的......是否可以在“背景”中使用css变量?

1 个答案:

答案 0 :(得分:-1)

[Wed Nov 15 19:44:28 2017] [error] [client 209.90.225.243] File does not exist: /path/to/vhosts/docroot/wp-content, referer: http://my.domain/wp-content/plugins/revslider/temp/
[Mon Nov 13 19:05:51 2017] [error] [client 209.90.225.243] File does not exist: /path/to/vhosts/docroot/wp-content, referer: http://my.domain/wp-content/themes/famous/style.css
[Sat Nov 11 22:21:38 2017] [error] [client 209.90.225.243] File does not exist: /path/to/vhosts/docroot/wp-content, referer: http://my.domain/wp-content/plugins/complete-gallery-manager/frames/upload-images.php