如何在codeigniter中加载css

Sou*_*Raj 15 php codeigniter

我是codeigniter的新手,我正在使用v.2.12.我尝试从外部文件加载css时收到错误.

我在应用程序文件夹中创建了css文件夹.我以all.css的名义创建了css文件.

在视图文件中,我使用以下代码链接css文件.

<link rel="stylesheet" type="text/css" href="<? echo base_url();?>css/all.css">
Run Code Online (Sandbox Code Playgroud)

但是css文件没有加载.我收到404错误.这是我的配置设置:

$config['base_url'] = 'http://webscarlets.com/ci/index.php';
$config['index_page'] = 'index.php';
Run Code Online (Sandbox Code Playgroud)

网站链接:http://webscarlets.com/ci/index.php/welcome.

Chr*_*vén 33

这是你在CodeIgniter中包含CSS文件的方法:

<?php echo link_tag('css/mystyles.css'); ?>
Run Code Online (Sandbox Code Playgroud)

该片段将输出此HTML:

<link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />
Run Code Online (Sandbox Code Playgroud)

该函数link_tag位于HTML帮助程序中,必须首先加载.

(请注意,您可能不应该将CSS文件/application/css放入其中.将它们放入/css或者可能更容易/assets/css.)


Ala*_*ran 10

函数base_url()应该返回基本路径(没有index.php)

您可以通过添加反斜杠来修复它:

<link rel="stylesheet" type="text/css" href="<? echo base_url();?>/css/all.css">
Run Code Online (Sandbox Code Playgroud)

或者从配置中删除index.php:

$config['base_url'] = 'http://webscarlets.com/ci/';
Run Code Online (Sandbox Code Playgroud)