问题描述
是否可以像使用WordPress时通常在HTML中那样在CSS中获得背景图像.我已经尝试过这样做,但是没有用.
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 ");
推荐答案
PHP代码无法在.css
文件中运行,但是您可以使用内联样式,例如:
PHP code cannot run in .css
file, however you can use inline style, such as:
<div style="background-image: url("<?php //url ?>");">
或
<style>
.class-name {
background-image: url("<?php //url ?>");
}
</style>
使用动态图像路径的自定义字段时,以上内容将非常有用.
The above would be useful when working with custom fields for dynamic image paths.
如果图像位于主题目录中,则不需要PHP,假设图像文件夹位于主题文件夹/theme-name/images
下,而样式表为/theme-name/style.css
,则只需添加背景图像到css文件,如下所示:
If the image is located in the theme directory, PHP won't be needed, let's say the image folder is directly under the theme folder /theme-name/images
, and the stylesheet is /theme-name/style.css
, then you can just add the background image to the css file as follows:
.class-name {
background-image: url("images/file.jpg")
}
这篇关于WordPress中的CSS背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!