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 ");
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 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")
}
If the image folder is in the theme folder you can use:
background-image: url("/wp-content/themes/themeNameHere/images/parallax_image.jpg ");
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.
Use a style attribute in the tag and use the css.
<div style="background-image: url("<?php bloginfo('template_directory'); ?>/images/parallax_image.jpg ");">
Your other html here
</div>
I was having this issue with adding a background image to my Underscores based theme and I found this works:
background: url('assets/img/tile.jpg') top left repeat;
I first tried:
background: url('/wp-content/themes/underscores/assets/img/tile.jpg');
but that didn't work for me.
Short answer is you can not render PHP in a CSS file. I would suggest making sure your <rel>
tag is setup correctly and just using the /images/parralax_iamge.jpg
part.
Just find the corresponding element (tag, class or ID) with the browser tools/inspector in your page and add a CSS rule for that element, either in the stylesheet of the child theme or in the "customCSS" field of the theme options.
One way would be to add this CSS to a PHP page that has access to the bloginfo()
function. Say in index.php
, you would add...
<style>
.some-element {
background-image: url("<?php bloginfo('template_directory'); ?>/images/parallax_image.jpg ");
}
</style>
you can't add php code into .css files. but if you wanna do this using php see this.
As many people have suggested already do not use php in .css ext as it won't get interpreted.
But if you really need to use php because of some reasons you only know as you not responding, you can change the extension of your stylesheet to .php
then you can have
customestyles.php
<?php
header("Content-type: text/css; charset: UTF-8");
$sectionImage = bloginfo('template_directory')."/images/parallax_image.jpg";
?>
<style type="text/css">
.selector{
background-image: url(<?php echo "$sectionImage";?>);
}
</style>
Another way is accessing image using css file location as the reference point, e.g. .class-name{ background-image:url("../images/file-name"); //one level up, then images folder background-image:url("../../images-directory-02/images/file-name"); //two levels up, then two folders in }