Krimson
Krimson

Reputation: 7674

Linking to Css file in CodeIgniter

So today I started using code-igniter and I have come across a problem. The problem is that I want to link a css file to my default view. My attempt was to put my css file in the root in root. So it looks like this

-Root
--application
--system
--styles
----main.css
--index.php

Then in my view I link my css file by pretending it was in the same folder like this:

<link href="styles/main.css" rel="stylesheet" type="text/css">

Now when I access my default controler like this: http://localhost/index.php it works!! But when I access it like this http://localhost/index.php/home/index it doesn't work. it tries to access the css file using this path http://localhost/index.php/styles/main.css

How do i fix this or moreover, what is the right way to do this?

Upvotes: 0

Views: 168

Answers (5)

Selim Reza
Selim Reza

Reputation: 1033

I hope you already kept your $config['base_url']= 'http://www.yourproject.com/';

in the application/config/config.php line number 17

So you need to use 'base_url' in your view files. such as for css ::::

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

It will be work in your project. Thanks

Upvotes: 0

Stephen Lake
Stephen Lake

Reputation: 1620

As Maarty answered, you should use:

<link href="<?php echo base_url() ?>styles/main.css" rel="stylesheet" type="text/css">

As for your second questions as to whether you have to do this everytime, check out Comper Template Parser, it's pretty good and extremely easy to setup. It creates pseudo variables out of your data variables -- What I do is add a styles, javascript and whatever else variable inside the Parser file, for example something like:

array('styles' => base_url() . '/stylesheets/',
      'scripts' => base_url() . '/javascripts/',
      'something' => base_url() . '/something-awesome/');

Would allow you do put this in your template file:

<link href="{styles}/main.css" rel="stylesheet" type="text/css">
<link href="{styles}/other.css" rel="stylesheet" type="text/css">

<script src="{scripts}/jquery.js"></script>
<script src="{scripts}/amazing.js"></script>

<img src="{something}/something.jpg"/>

Do yourself a favour and checkout Comper Template Parser -- it's a real treat.

Upvotes: 0

Pooshonk
Pooshonk

Reputation: 1324

Try this

<link rel="stylesheet" type="text/css" href="<?php echo base_url('styles/main.css')?>" />

Define your base_url() in your config.php file

Upvotes: 0

Maarty
Maarty

Reputation: 1190

try this

<link href="<?php echo base_url() ?>styles/main.css" rel="stylesheet" type="text/css">

if you are using codeigniter version lower than 2.0 you must specify base_url in config.php

...

or you can specify the base tag

<base href="<?php echo base_url()?>">

and all your relative paths will be prefixed by base_url

it works only in current view (I prefer add it to template view)

...

or check this

Upvotes: 2

PravinS
PravinS

Reputation: 2594

user base_url() instead of site_url()

Upvotes: 0

Related Questions