CodeIgniter Adding JS And CSS

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

CodeIgniter - Adding JS & CSS

Adding JavaScript and CSS (Cascading Style Sheet) file in CodeIgniter is very simple. You have to create JS and CSS folder in root directory and copy all the .js files in JS folder and .css files in CSS folder as shown in the figure.

Adding JS and CSS

For example, let us assume, you have created one JavaScript file sample.jsand one CSS file style.css. Now, to add these files into your views, load URL helper in your controller as shown below.


After loading the URL helper in controller, simply add the below given lines in the view file, to load the sample.js and style.css file in the view as shown below.


php echo base_url(); ?>css/style.css">


Create a controller called Test.php and save it in application/controller/Test.php

   class Test extends CI_Controller {
      public function index() { 

Create a view file called test.php and save it at application/views/test.php

 lang = "en"> 
       charset = "utf-8"> 
      CodeIgniter View Example 
      php echo base_url(); ?>css/style.css"> 
       href = 'javascript:test()'>Click Here to execute the javascript function. 

Create a CSS file called style.css and save it at css/style.css

body { 

Create a JS file called sample.js and save it at js/sample.js

function test() { 

Change the routes.php file in application/config/routes.php to add route for the above controller and add the following line at the end of the file.

$route['profiler'] = "Profiler_controller"; 
$route['profiler/disable'] = "Profiler_controller/disable"

Use the following URL in the browser to execute the above example.