Background Url dynamic Css

0

To change a html property dynamically by php , I use code similar to this:

<img src="<?php echo $foo;?>"/>

You can change the url of background in css by php

example:

.foo{background:url('foo.jpg');}
    
asked by anonymous 26.01.2017 / 18:21

2 answers

2

You can create a PHP page to generate dynamic CSS according to your needs as follows:

  • Create the style.php page
  • Set header of it to css header("Content-type: text/css")
  • Example:

    <?php
        header("Content-type: text/css");
    ?>
    .foo{
        <?php
            //condições aqui
        ?>
    }
    

    And on your page you can call the file with the link equal to a normal css

    <link rel="stylesheet" type="text/css" href="estilo.php" />
    

    If you want to change the CSS of an element directly in HTML, you can use inline CSS with PHP, for example:

    <div class="foo" style="background-image:<?php echo $foo; ?>;"></div>
    

    In this way even if the% cc_de% CSS document has some foo , it will be disregarded and the inline will be interpreted in its place.

    You can also directly change the background-image tag or put a src in the element you want to handle with JS, eg

    $('#seu-elemento').attr("src", "caminho-para-nova-imagem");
    

    Or manipulate the style properties of it

    $('#seu-elemento').css({"background-image" : "caminho-para-nova-imagem"});
    
        
    26.01.2017 / 18:35
    0

    Only if your CSS is generated in the (phtml) file in the .css file it does not process PHP, so it is not possible to do this.

    It is possible to do via javascript.

        
    26.01.2017 / 18:24