简体   繁体   中英

How to use background-image: with php

How can I use background-image: url() in conjunction with php in order to obtain the directory of the image I would like to display.

CSS

.imageGallery {
width: 960px;
position: relative;
background-image: url('<?php bloginfo("template_directory");?>/images/visitus.png');
}

Currently, this css does not work. No background image is displayed.

HTML

<div class="imageGallery">
</div>

For example:

this works fine

<img src='<?php bloginfo("template_directory");?>/images/home.png' class="headImage" />

but this doesn't

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

You can try this two options bellow

CSS

.imageGallery {
width: 960px;
position: relative;
background-image: url('images/visitus.png');
}

HTML with inline CSS

<div class="imageGallery" style="background-image: url('<?php bloginfo("template_directory");?>/images/visitus.png')">
</div>

If you use the second option, you need to remove background image url from your css rule for that element. Hope it helps

It depenteds to where is your css file , put your css file and image file into same place, and try below

background-image: url("frame.jpg");

Or you can use / to start from root folder.

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

Read this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM