0

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>
14
  • check browser developer tools for 404. Commented Oct 16, 2017 at 18:55
  • 1
    Have you ensured that you can access it from the browser? http://localhost/ci/css/styles.css Commented Oct 16, 2017 at 18:57
  • @KeithChason yes! Commented Oct 16, 2017 at 18:59
  • I would next do what @Scriptonomy said, and how do you know it's "not working?" Commented Oct 16, 2017 at 19:00
  • 1
    I think it's a CSS problem instead of CodeIgniter Commented Oct 16, 2017 at 19:24

2 Answers 2

1

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" />
Sign up to request clarification or add additional context in comments.

14 Comments

You need to set the base url in codeigniter 3 > versions as it says in the comment above it.
@wolfgang1983: the leading slash makes it absolutely sure you loading a relative path, if you take it away CI 2.x might break...
I use it like href="<?php echo base_url('assets/css/style.css');?>" because your base url all ready has /
@wolfgang1983 yes that's another correct approach, but you need to type more :)
When tried the above i got : Call to undefined function base_url()
|
0
<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');?>">

1 Comment

"you do not need to have a semicolon terminating the last line of a PHP block". see php manual: php.net/manual/en/…

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.