CSS 我使用什么路径在 codeigniter 中调用背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26989533/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 02:46:41  来源:igfitidea点击:

What path do I use to call a background image in codeigniter

cssimagecodeigniter

提问by Ralph Schipper

This is my root structure:

这是我的根结构:

enter image description here

在此处输入图片说明

And under the folder css, I have the file style.css

在文件夹 css 下,我有文件 style.css

I load my CSS file like this:

我像这样加载我的 CSS 文件:

<link rel="stylesheet" href="<?php print asset_url()?>css/style.css">

I created a helper asset_url_helper that has this function:

我创建了一个具有此功能的助手 asset_url_helper:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('asset_url()')) {
    function asset_url()
   {
    return base_url().'assets/';
   }
 }

And my .htacces file:

还有我的 .htacces 文件:

Deny from all
RewriteEngine on
RewriteCond  !^(index\.php|images|assets|robots\.txt)
RewriteRule ^(.*)$ /index.php/ [L]

Loading the CSS file works like a charm, thats no problem.

加载 CSS 文件就像一个魅力,那没问题。

Now I want to call a background image from my style CSS something like this:

现在我想从我的样式 CSS 中调用背景图像,如下所示:

body {
    background-image: url('/assets/img/bg-admin.jpg');
}

But this doesn't work

但这不起作用

Can someone tell me how I call the image file from CSS?

有人能告诉我如何从 CSS 调用图像文件吗?

回答by Rwd

background-image: url()in CSS works relatively.

background-image: url()在 CSS 中相对工作。

Try this:

尝试这个:

body {
    background-image: url('../img/bg-admin.jpg');
}

This is assuming that your css file is in assets/css and not a sub folder of it.

这是假设您的 css 文件位于 assets/css 而不是它的子文件夹。

Hope this helps!

希望这可以帮助!

回答by Niaz Muhammad

codeigniter will take the base_url. your url is http://localhost/rscms/external/bootstrap/css/style.cssyou will to go back to bootstrap first come out from style.css then css then bootstrap

codeigniter 将采用 base_url。你的 url 是http://localhost/rscms/external/bootstrap/css/style.css你会回到 bootstrap 首先从 style.css 然后 css 然后 bootstrap

background-image: url('../../../img/bg-admin.jpg');

背景图片:url('../../../img/bg-admin.jpg');