Code Igniter : base_url() at CSS file doesn't work

别说谁变了你拦得住时间么 提交于 2019-12-10 15:59:23

问题


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...


回答1:


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");



回答2:


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.




回答3:


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




回答4:


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!.




回答5:


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;



回答6:


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/ "




回答7:


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;
}


来源:https://stackoverflow.com/questions/15129319/code-igniter-base-url-at-css-file-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!