Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS background images in WordPress

Tags:

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 ");
like image 497
Reece Avatar asked May 08 '17 15:05

Reece


People also ask

Is it possible to add a background image to a WordPress site?

Overall, if custom background support is enabled in a theme, the user has the ability to upload an image or choose a color to fill the entirety of the site background. The settings are located in the WordPress dashboard under Appearance > Customize > Background Image.

How do you make a background for WordPress?

In your WordPress admin find the Appearance » Customize page. Open the Customizer and look for the Background Image option to upload or select a background image. Then click on the Choose Image button and change different settings, such as background-position or the way the image will be displayed.


3 Answers

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

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

or

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

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:

.class-name {   background-image: url("images/file.jpg") } 
like image 128
Stickers Avatar answered Oct 08 '22 14:10

Stickers


You don't need to use PHP in this question, your CSS file is already in template folder, so you can call image just like this:

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

So you don't need to know template path to call images from your theme.

like image 43
Alex Avatar answered Oct 08 '22 14:10

Alex


If the image folder is in the theme folder you can use:

background-image: url("/wp-content/themes/themeNameHere/images/parallax_image.jpg ");
like image 33
mattD Avatar answered Oct 08 '22 14:10

mattD