17

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.

11 Answers 11

34

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

Sign up to request clarification or add additional context in comments.

Comments

10

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/';

7 Comments

Thanks for the reply let me try this and tell you
what is the URL printed out for CSS?
The folder CSS was not found on your server. Make sure you uploaded the CSS folder and all.css file
check out the link i have posted an another post
@Alaa Badran hi,when i use this i don't get my styles and also in my inspection on firebug,it shows that access forbidden,please help me...!!i'm trying it for 3 days:(
|
4

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!

1 Comment

The OP asked for a PHP / CodeIgniter way to load css. Your response is unrelated.
3

To attach a CSS, JS, Images .etc you just have to do is go to your config folder and write at the end of the constant.php file.

define('URL','ADD YOUR LOCAL/REMOTE PATH');

define('CSS',URL.'public/css/');
define('IMAGES',URL.'public/images/');
define('JS',URL.'public/images/');

After that goto your view and in the link just add

<link rel="stylesheet" type="text/css" href="<?php echo CSS; ?>index.css">

this will solve your problem.

Hope it helps.

Comments

3

Add below line in your controller action /application/controllers/yourController.php

$this->load->helper('url');

Then add below line to your view file's head tag.

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

Assuming that you have assets/css/ folder is created in your app directory.

<path to your app folder>/assets/css

Comments

2

before using the base_url() you should have to load the URL helper class.

something like $this->load->helper('url'); in your controller

base_url() return you the path something like
'http://webscarlets.com/'
if you have set it directly in the root or 'http://webscarlets.com/dir/'

and also make sure about the location of your CSS file.

follow the link to know more about URL Helper

1 Comment

Thanks for the reply. I loaded the base url helper $autoload['helper'] = array('url','form','html');
2

another way would be

define a constant in constants.php (in config directory)

define("LAYOUT_URL","http://localhost/yoursite/css/");

"css" folder here i m assuming is inside application folder. NOw you can attach css in page like

<link rel="stylesheet" type="text/css" href="<?php echo LAYOUT_URL;?>all.css">

8 Comments

but can you tell me why this is not working <link rel="stylesheet" type="text/css" href="<? echo base_url();?>/css/all.css">
define("LAYOUT_URL","localhost/yoursite/css/"); <link rel="stylesheet" type="text/css" href="<?php echo LAYOUT_URL;?>all.css"> I dont know this is proper way to access the css files?
I m not sure, but your base_url() is not loading your css file because of index.php in the config. It would likely search for the controller. My way is a clean way, there is nothing wrong with my way, it only matters if you're comfortable using it.
ok now check the link css is not loading. After adding the htaccess file to remove index.php from url css is not loading webscarlets.com/ci now if i remove the .htaccess file css is working
Can you tell me how to fix it? here is my .htaccess code RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt|css) RewriteRule ^(.*)$ /ci/index.php/$1 [L] I added this htaccess script in the ci folder
|
2

As Jogesh_p.

you use base_url as follow put in controller (your controller)

$this->load->helper('url');

in controller . if you want to use

as follow put in where you want use base_url.

echo base_url()

NOTE: better you create new folder at root

(Example: theme) same: application, system, user_guide, theme)

i hope can you do

Comments

2

Edit autoload.php as follows

$autoload['helper'] = array('url');

Then load css , js,image like that

img src="<?php echo base_url(); ?>assets/images/master.jpg"</img> 

Comments

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

and try to load css by adding application folder

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

EDIT

Here base_url() echos 'http://webscarlets.com/ci/' then adding the file with path application /css/all.css

1 Comment

Thanks for reply. I tried as you said but it not working. What should i do next?
1

Include $this->load->helper('html'); in a controller function.

And use linktag key word in view file something like this:

<html>
<head>
    <title></title>
    <?php echo link_tag('resources/style.css');?>
</head>
<body>
    <?php
    ....
    ?>
</body>

Here the resources is the folder that contain the style.css file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.