Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load css in codeigniter

I am new to codeigniter, and I am using v.2.12. I am getting an error when I try to load the css from the external file.

I create the css folder inside the application folder. And I create the css file in the name of all.css.

In the view file I use the following code to link the css file.

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

But the css file is not loading. I'm getting 404 error. Here is my configuration settings:

$config['base_url'] = 'http://webscarlets.com/ci/index.php';
$config['index_page'] = 'index.php';

Website Link: http://webscarlets.com/ci/index.php/welcome.

like image 714
Soundhar Raj Avatar asked Aug 06 '12 08:08

Soundhar Raj


People also ask

Where do I put CSS in CodeIgniter?

Adding JavaScript and CSS (Cascading Style Sheet) file in CodeIgniter is very simple. You have to create JS and CSS folder in root directory and copy all the . js files in JS folder and . css files in CSS folder as shown in the figure.

How do I link CSS to ci4?

Now create a folder named CSS in the public directory, and create a style. css fine and keep this file in the same folder you just created. Now create a js folder in the public directory, create a custom. js file, and keep the file in the same directory you have just created.

How will you add or load a model in CodeIgniter?

Auto-loading Models If you find that you need a particular model globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload. php file and adding the model to the autoload array.


3 Answers

This is how you include CSS files in CodeIgniter:

<?php echo link_tag('css/mystyles.css'); ?>

That snippet will output this HTML:

<link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />

The function link_tag is in the HTML helper, which must first be loaded.

(Note that you probably shouldn't put your CSS files in /application/css. It's just easier to put them in /css or maybe /assets/css.)

like image 85
Christian Davén Avatar answered Oct 20 '22 18:10

Christian Davén


The function base_url() should return the base path (without index.php)

You may fix it by adding a backslash like:

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

or remove the index.php from your config:

$config['base_url'] = 'http://webscarlets.com/ci/';
like image 35
Alaa Badran Avatar answered Oct 20 '22 18:10

Alaa Badran


I just find the solution to avoid index.php file and load ours CSS files. Just copy the code below in a .htaccess file:

Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|styles|scripts|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Greeting!

like image 20
Jorge Luis Jiménez Prada Avatar answered Oct 20 '22 20:10

Jorge Luis Jiménez Prada