WordPress 中的 CSS 背景图像 [英] CSS background images in WordPress

查看:32
本文介绍了WordPress 中的 CSS 背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 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 背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆