Question

base_url() doesn't work at CSS file...

here's my php :

<link rel="stylesheet" type="text/css" href="<?=base_url()?>css/style.css"/>
<body>
</body>

here's my css/style.css :

body {
  background:#356aa0 url(<?=base_url()?>img/background.png) repeat-x;
  color:#fff;
}

the text color change to white, but the image doesn't show up... if i use url(../img/background.png), it show up... but when i open url localhost/project/index.php/control/function/var1/var2, it doesn't show up...

It's Solved, Thanks every one... :]

i make php file at view folder like this :

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

body {
    background:url(<?=base_url()?>img/background.png);
}

and i load the php file i just make with function at controller, and then i link it :

<link type="text/css" rel="stylesheet" href="<?=base_url()?>control/style.php"/>

It's work, Thanks guys...

Was it helpful?

Solution 2

CSS file does not get parse as PHP file. If you really want to do something like that, rename your file as styles.php

OPEN the page and add

header("content-type: text/css");

This tells the page to be treated as a text based CSS file. Then you can simple echo your remaining CSS like

echo "
body {
....
....
";

To fix the base_url() not being accessible from styles.php set a session variable to keep that. You can keep this on your index.php of codeignitor.

$_SESSION['base_url'] = base_url();

Now, use this inside styles.php.

background: url("<?php echo $_SESSION['base_url']; ?>"/../../some.jpg");

OTHER TIPS

You should do it like this

Structure of your files

myapp/
    application/
    system/ 
    css/
    img/

And in css write this

body {
  background:#356aa0 url(../img/background.png) repeat-x;
  color:#fff;
} 

And now call it

<link rel="stylesheet" type="text/css" href="<?=base_url()?>css/style.css"/>

That is the standard way of doing it. Also your css is not dynamic so you dont have to worry about php code using in it. The structure i presented in the answer will surely use the styles correctly and load the images.

Get your base_url() out of the CSS file. That would fix the problem. You can't put PHP code in a CSS file.

You don't need to make php file as css. Simply put your images file out of "application" folder, e.g on assets/img. Then edit your css file

.error {
  color: #D8000C;
  background-color: #FFBABA;
  background-image: url('../img/error.png');
  background-size: 20px 20px;
}

call the css file using

  <link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/style.css" media="all">

It will absolutely works dude!.

if your folder structure is:

YourAppFolder/
    application/
           public/
               css/
               img/
         system/

Then use this in your view:

<link rel="stylesheet" type="text/css" href="<?=base_url()?>public/css/style.css"/>

and in css use this for image:

background:#356aa0 url(../img/background.png) repeat-x;

Try this..

Replace this:

<link rel="stylesheet" type="text/css" href="<?=base_url()?>css/style.css"/>
<body>
</body>

With this:

<link rel="stylesheet" type="text/css" href="<?php base_url(CSS/style.css)?>"/>
<body>
</body>

Also replace the following in css/style.css :

body {
  background:#356aa0 url(<?=base_url()?>img/background.png) repeat-x;
  color:#fff;
}

With this:

body {
  background:#356aa0 url(../img/background.png) repeat-x;
  color:#fff;
}

NB: if it doesn't work, try use one dot(.) -> ./img/background.png

Assuming your file structure is like this:

-ci
--application
--CSS
---file.css
--img
---background.png
--system
.
.
.

and your base_url() : " http://localhost/sitename/ "

or you can write ...

 <link rel="stylesheet" type="text/css" href="<? echo base_url('your css file from base url') ?>/>

this is your external css file

    body {
  background:url(<? echo base_url('your image path from base url')?>) repeat-x;
  color:#fff;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top