Html CSS 中的 wordpress 3.3 相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10058167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:38:57  来源:igfitidea点击:

wordpress 3.3 relative paths in CSS

htmlcsswordpress

提问by Marco Martins

I want to add an image to be a background of the header, the thing is that I don't want to add an absolute path since I'm doing this on my pc and them is to upload to my server.

我想添加一个图像作为标题的背景,问题是我不想添加绝对路径,因为我在我的电脑上这样做并且它们要上传到我的服务器。

should the <?php bloginfo('template_directory'); ?>work in the css? It is not working here.

应该<?php bloginfo('template_directory'); ?>在css中工作吗?它在这里不起作用。

code:

代码:

#branding {
    background: url("<?php bloginfo('template_directory'); ?>/images/background2.jpg") repeat-x repeat-y;
    height: 150px;
}

回答by developdaly

No, you can't use PHP within a CSS file.

不,您不能在 CSS 文件中使用 PHP。

You can still use a relative path. This example will work if your CSS file and images directory are in the same directory. WordPress knows this is relative to the theme.

您仍然可以使用相对路径。如果您的 CSS 文件和图像目录在同一目录中,则此示例将起作用。WordPress 知道这与主题有关。

#branding {
    background: url("images/background2.jpg") repeat-x repeat-y;
    height: 150px;
}

If the images directory is in a parent of the CSS file:

如果图像目录位于 CSS 文件的父目录中:

#branding {
    background: url("../images/background2.jpg") repeat-x repeat-y;
    height: 150px;
}

回答by Syed

Somehow @developdaly solution did not work for me, but this helped:

不知何故@developdaly 解决方案对我不起作用,但这有帮助:

.img-div {
  background-image: url("wp-content/themes/your-theme/assets/images/your-img.jpg");
}

回答by Mr. Zarik

.img-div {
    background-image: url("./assets/images/your-img.jpg");
}