Robert Ross
Robert Ross

Reputation: 1189

Correct way to load CSS in a codeigniter view

My base url in config is : $config['base_url'] = 'http://localhost/ci/';

My CSS folder is on root level(so ci).

In the view I tried:

<head>
    <style> @import url('<?=base_url()?>/css/styles.css'); </style>
</head>

and

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

both fail. what is the correct way to import a CSS file using the Codeigniter framework

Here is how my .htaccess file looks like :

    <IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>

Upvotes: 0

Views: 545

Answers (2)

Uday Kumar
Uday Kumar

Reputation: 131

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

you are missing a semicolon at the end of base_url

This code is ok ensure that you are giving correct path

you can also use

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

Upvotes: 0

Vickel
Vickel

Reputation: 7997

I use this approach:

folder structure:

example.com
|_ application
|_ assets
|_ system

CI 2.x

I've set $config['base_url'] = '';, this will work for local server and production server

CI 3.x

WARNING: You MUST set this ($config['base_url']) value! so I use below code to differentiate between local server and production server :

if ($_SERVER["HTTP_HOST"]=='ex.ample')
    $config['base_url'] =  'http://ex.ample/';
else
    $config['base_url'] =  'http://example.com/';

( I'm using at the local server a virtual host called ex.ample, change this to your needs)

in my folder assets I store assets/css/style.css

and call it in my view with:

<link rel="stylesheet" type="text/css" href="/assets/css/style.css" />

Upvotes: 1

Related Questions