Wordpress 3.3 relative paths in CSS

I want to add an image as the background of the header, the point is I don't want to add an absolute path since I am doing this on my computer and they have to upload to my server.

should it <?php bloginfo('template_directory'); ?>

work in css? He doesn't work here.

code:

#branding {
    background: url("<?php bloginfo('template_directory'); ?>/images/background2.jpg") repeat-x repeat-y;
    height: 150px;
}

      

+3


source to share


3 answers


No, you cannot use PHP in a CSS file.

You can still use a relative path. This example will work if your directory of CSS files and images is in the same directory. WordPress knows it's about theme.

#branding {
    background: url("images/background2.jpg") repeat-x repeat-y;
    height: 150px;
}

      



If the image directory is in the parent CSS file:

#branding {
    background: url("../images/background2.jpg") repeat-x repeat-y;
    height: 150px;
}

      

+11


source


Somehow @developdaly's solution didn't work for me, but it did help:



.img-div {
  background-image: url("wp-content/themes/your-theme/assets/images/your-img.jpg");
}

      

+1


source


.img-div {
    background-image: url("./assets/images/your-img.jpg");
}

      

0


source







All Articles