如何使用背景图像:与 php

5 html css php wordpress

如何将background-image: url() 与php 结合使用以获得我想要显示的图像的目录。

CSS

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

目前,这个 css 不起作用。不显示背景图像。

超文本标记语言

<div class="imageGallery">
</div>
Run Code Online (Sandbox Code Playgroud)

例如:

这很好用

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

但这并不

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

小智 1

您可以尝试以下两个选项

CSS

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

带有内联 CSS 的HTML

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

如果您使用第二个选项,则需要从该元素的 css 规则中删除背景图像 url。希望能帮助到你