Search code examples
cssbackground-image

There's a way to set a default directory for the images used in css?


I have a css file which, for organizational sake, is in another directory from the one where the images are. The thing is I always end up setting all the background-images like this: background-image: url(../img/Untitled.jpg). There's anything like the HTML's <base> tag that can be used in css so I don't need to put that "../img/" to every single background-image?


Solution

  • Another possible option would be to create your styles using or another server side language. You can then use a directory variable to spit out the full path for each item.


    styles.php

    <?php
        header("Content-Type: text/css");
        $imageDir='../images';
        // or
        // $imageDir='/newsletter/2010/jan/17/business/category/7/img';
    ?>
    
    .divClass{
        background : url('<?php echo $imageDir; ?>/kitten.jpg');
    }