Home » Php » CodeIgniter PHP stylesheet link. HOW?

CodeIgniter PHP stylesheet link. HOW?

Posted by: admin July 12, 2020 Leave a comment


I’m using xampp for my php. And I have download a code igniter and save it on my htdocs. I already made a databasing and a sample page. My only problem is how can I link my css. Where should I save my style.css? How can I call my style.css?

<link rel="stylesheet" href="<? base_url(); ?>stylesheet/style.css" type="text/css" media="screen"/>

I have this but still have a problem. Is there a step by step on how to link a css?

Thank You.

How to&Answers:

Put your CSS wherever you’d like, then target that url.

Try putting it in the folder ‘/css/test.css’ for instance ‘/’ being the root of your site, not your CI install.

From CI, you can then call it with

<?= link_tag(base_url().'css/test.css'); ?>

That should generate the following code

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

If you’re having trouble targetin the CSS, the easiest way to find out where your script is trying to call it, is to look in your web pages source output. You’ll see exactly where CI thinks it’s located.


You can put your stylesheet anywhere really – it’s just a matter of getting your directory to it correctly. The code you posted is going to look in your main directory (the folder with the license, system, user_guide, etc. It’s going to a look for a folder called ‘stylesheet’, and then for style.css.

Make sure that you have your stylesheet in there.


Using the Firefox plugin FireBug will help a lot with that. View the source of the HTML that was output, and find out where the browser is looking for that stylesheet, and make sure it’s there.


This is Syed Haroon from Mysore, Karnataka, India.

Root Folder Name: ci_test
CSS file name : mystyles.css”;
Controller file name: Start.php
view file name: test_view.php


Suggestion for WAMP (hope it will be easy to fix the same in xampp):

Save it in root/system/application/css

Create a new CSS directory in application folder (just to Manage files and folder in the correct way)

How to connect back to CSS file?

in system/application/config/config.php
Edit “$config[‘base_url’] = http://localhost/” to

$config['base_url'] = "http://localhost/ci_test";

Crate a new line

$config['css'] = "system/application/css/mystyles.css";

Create a Controller in “system/application/controllers/start.php” insert the below sample code:

class Start extends Controller {
 var $base;
 var $css;

 function Start(){
  $this->base = $this->config->item('base_url');
  $this->css = $this->config->item('css');

 function hello(){
  $data['css'] = $this->css;
  $data['base'] = $this->base;
  $data['mytitle'] = 'Welcome to this site';
  $data['mytext'] = "Hello, now this is how CSS work!";
  $this->load->view('test_view', $data);

Crate a view file in “system/application/views/test_view.php” insert the below sample code:

<title>Web test Site</title>
<base href= <?php echo "$base"; ?> >
<link rel="stylesheet" type="text/css" href="<?php echo "$base/$css";?>">
<h1><?php echo $mytitle; ?> </h1>
<p class='test'> <?php echo $mytext; ?> </p>

now enter this in ur address bar of the browser “http://localhost/ci_test/index.php/start/hello/


The simplest way of doing this is to place the css file in the same directory with the index.php file and reference it using <link rel="stylesheet" type="text/css" href="style.css" />, or if you have more than one css file, you can create a subdirectory into which you can place them.


I think the better way is, you create a public folder. Put the css, and the js, and images folder inside the public folder.

Make a .htaccess file in the root folder. (where is the index.php)

Put this into .htaccess file:

RewriteEngine On

RewriteCond $1 !^(index\.php|images|js|css)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

RewriteCond $1 ^(images|js|css)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./public/$1 [L,QSA]

After you can refer the files like this:

<img src="/images/blablabla.jpg">

Or /css/blabla.css


My file my file path is
<link href="<?php echo base_url();?>assets/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>& not working.

At last I have used

RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

on my .htaccess file & it’s working now. You can rename your file instead “assets” my style folder name.