代码点火器:CSS 文件中的 base_url() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15129319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Code Igniter : base_url() at CSS file doesn't work
提问by Esgi Dendyanri
base_url() doesn't work at CSS file...
base_url() 在 CSS 文件中不起作用...
here's my php :
这是我的 php :
<link rel="stylesheet" type="text/css" href="<?=base_url()?>css/style.css"/>
<body>
</body>
here's my css/style.css :
这是我的 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...
文本颜色变为白色,但图像不显示...如果我使用 url(../img/background.png),它会显示...但是当我打开 url localhost/project/index. php/control/function/var1/var2,它没有出现...
It's Solved, Thanks every one... :]
已解决,谢谢大家... :]
i make php file at view folder like this :
我在这样的视图文件夹中创建 php 文件:
<?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 :
然后我加载我刚刚在控制器上使用函数创建的 php 文件,然后我链接它:
<link type="text/css" rel="stylesheet" href="<?=base_url()?>control/style.php"/>
It's work, Thanks guys...
它的工作,谢谢大家...
采纳答案by Starx
CSS file does not get parse as PHP file. If you really want to do something like that, rename your file as styles.php
CSS 文件不会被解析为 PHP 文件。如果您真的想做类似的事情,请将您的文件重命名为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
这告诉页面被视为基于文本的 CSS 文件。然后你可以简单地回显你剩余的 CSS 就像
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.
要解决base_url()
无法通过styles.php
设置会话变量来保持它的问题。您可以将其保留在您index.php
的 codeignitor 上。
$_SESSION['base_url'] = base_url();
Now, use this inside styles.php
.
现在,在里面使用它styles.php
。
background: url("<?php echo $_SESSION['base_url']; ?>"/../../some.jpg");
回答by Muhammad Raheel
You should do it like this
你应该这样做
Structure of your files
你的文件结构
myapp/
application/
system/
css/
img/
And in css write this
在 css 中写下这个
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.
这是标准的做法。此外,您的 css 不是动态的,因此您不必担心在其中使用 php 代码。我在答案中提出的结构肯定会正确使用样式并加载图像。
回答by Ivan Ivkovi?
Get your base_url() out of the CSS file. That would fix the problem. You can't put PHP code in a CSS file.
从 CSS 文件中取出 base_url()。那将解决问题。您不能将 PHP 代码放在 CSS 文件中。
回答by Pirman
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
您不需要将 php 文件制作为 css。只需将您的图像文件放在“应用程序”文件夹之外,例如在 assets/img 上。然后编辑你的css文件
.error {
color: #D8000C;
background-color: #FFBABA;
background-image: url('../img/error.png');
background-size: 20px 20px;
}
call the css file using
使用调用 css 文件
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/style.css" media="all">
It will absolutely works dude!.
伙计,它绝对有效!
回答by Rahul Chipad
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:
并在 css 中将其用于图像:
background:#356aa0 url(../img/background.png) repeat-x;
回答by user3509249
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 :
还要替换 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
注意:如果它不起作用,请尝试使用一个点(.) -> ./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/ "
和你的 base_url() : " http://localhost/sitename/ "
回答by Nasim Bahar
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
这是你的外部 css 文件
body {
background:url(<? echo base_url('your image path from base url')?>) repeat-x;
color:#fff;
}