16

i have a stylesheet link like so

<link href="css/<? echo $theme;?>/styles.css" rel="stylesheet" type="text/css" />

Inside the CSS I want to be able to echo a background image outputted by the db

body{ background-image:url(../../images/<?php echo $theme.'/'.$background;?>);}

I tried adding the following to the beginning of my page but it just output everything in html code

<? header ("Content-type: text/css");?>

Im sure there is a bit to this, can anyone advise the best course of action?

Thanks in advance

Woz
  • 475
  • 2
  • 7
  • 14
  • Possible duplicate of http://stackoverflow.com/questions/3483213/how-can-i-include-css-in-php?rq=1 – Pavlo Sep 11 '12 at 10:01

2 Answers2

22

Change the file ending to php so it gets run by the server, and once you've done that you can link to it as if it was a regular css file and make sure the header is correct set with that header code you have at the top of the file

Change:

<link href="css/<? echo $theme;?>/styles.css" rel="stylesheet" type="text/css" />

To:

<link href="css/<? echo $theme;?>/styles.php" rel="stylesheet" type="text/css" />

and top of that file you have

<? header ("Content-type: text/css");?>

And btw since you seem to have php shorttags already enabled you might aswell use instead of using

<? echo $var;?>

Use

<?=$var;?>
Breezer
  • 10,410
  • 6
  • 29
  • 50
  • Hmm ok I did what you said changing the name to styles.php and then ran a quick test with a session variable. So at the beginning of my index.php file I have $_SESSION['bg'] = 'bg.jpg';?> Then in my CSS file I have session_start();?> header ("Content-type: text/css");?> followed by body{ background-image:url(../../images/ echo $theme.'/'.$_SESSION['bg']; ?> );} Does this look ok? I am not seeing the background image and its definitely there – Woz Sep 11 '12 at 10:07
  • just open the php file and check what gets printed, there might be errors in your css file or php file but if everything is done right you should have no problem – Breezer Sep 11 '12 at 10:12
  • It jsut prints this body{ background-image:url(../../images// );} – Woz Sep 11 '12 at 10:33
  • well of what you've provided me i can tell that the `$_SESSION['bg']` isnt getting set have you `session_start();` in the beggining of your index.php file you need to have that before any session variable is getting set – Breezer Sep 11 '12 at 10:44
  • God im an idiot! ha its always something so simple! Yes I totally overlooked the session_start();?> at the beginning of index.php!! Thanks Breezer – Woz Sep 11 '12 at 10:53
2

Here is a Way: Use that part of the CSS inside the HTML file. Use double quotes " " and write the PHP file inside it. Here is an example to use PHP inside CSS file.

This CSS is in the HTML file and this code is working fine on my project. Let me know if any issue will test it.

<style type="text/css">

        @media screen and (max-width: 650px) {
            .benifit {
                background-image:url("<?= $this->getBaseUrl() ?>/www/images/Capture.png") ;
                height:440px;
                border-radius: 5px 0px 0px 5px;
            }

        }
        @media screen and (min-width: 400px) {
            .benifit {

                width: 47%;
                background-image:url("<?= $this->getBaseUrl() ?>/www/images/Capture.png") ;
                height:440px;
                border-radius: 5px 0px 0px 5px;
            }
        }
        #ajax-loader {

            height:     100%;
            width:      100%;
            background: rgba( 255, 255, 255, .8 ) url("<?= $this->getBaseUrl() ?>/www/img/ajax-loader1.gif") 50% 50% no-repeat;
            overflow: hidden;
        }

    </style>
Raj
  • 946
  • 12
  • 16