CSS background images in WordPress

2019-02-08 22:11发布

Is it possible to get a background image in CSS like you normally do in HTML when working with WordPress. I've tried doing this but it doesn't work.

background-image: url("<?php bloginfo('template_directory'); ?>/images/parallax_image.jpg ");

11条回答
一纸荒年 Trace。
2楼-- · 2019-02-08 22:36

Another way is accessing image using css file location as the reference point, e.g. .class-name{ background-image:url("../images/file-name"); //one level up, then images folder background-image:url("../../images-directory-02/images/file-name"); //two levels up, then two folders in }

查看更多
可以哭但决不认输i
3楼-- · 2019-02-08 22:44

you can't add php code into .css files. but if you wanna do this using php see this.

查看更多
干净又极端
4楼-- · 2019-02-08 22:47

I was having this issue with adding a background image to my Underscores based theme and I found this works:

background: url('assets/img/tile.jpg') top left repeat;


I first tried:

background: url('/wp-content/themes/underscores/assets/img/tile.jpg');

but that didn't work for me.

查看更多
甜甜的少女心
5楼-- · 2019-02-08 22:49

You don't need to use PHP in this question, your CSS file is already in template folder, so you can call image just like this:

background-image: url("images/parallax_image.jpg");

So you don't need to know template path to call images from your theme.

查看更多
疯言疯语
6楼-- · 2019-02-08 22:51

If the image folder is in the theme folder you can use:

background-image: url("/wp-content/themes/themeNameHere/images/parallax_image.jpg ");
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-02-08 22:51

Short answer is you can not render PHP in a CSS file. I would suggest making sure your <rel> tag is setup correctly and just using the /images/parralax_iamge.jpg part.

查看更多
登录 后发表回答