How to Add Background Images in WordPress – as Like CSS

PHP code cannot run in .css file, however you can use inline style, such as:

[php]<div style="background-image: url("<?php //url ?>");">[/php]

OR

[css].class-name {
background-image: url("<?php //url ?>");
}[/css]

The above would be useful when working with custom fields for dynamic image paths.

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:

[css].class-name {
background-image: url("images/file.jpg")
}[/css]

[custom-related-posts title=”You may Also Like:” none_text=”None found” order_by=”title” order=”ASC”]

Leave a Comment