Loading CSS file in CodeIgnitor

Loading CSS file in CodeIgnitor


I was wondering hours and hours how to load css files in CodeIgnitor. For the beginners this is  musch embarrassing. So dear beginners here is how to do it.

Lets have a sample view and a controller.

View- application/views/myView.php
Controller- application/controllers/myController.php

And its a good practice to have your in a separate directory. Here I keep my css files in a directory under application folder.

CSS file- application/styles/myStyle.css

myController.php
<?php

    class MyController extends CI_Controller{
       
        functionMyController{
            parent::__construct();
        }
       
        function load_home_view(){
            $this->load->helper('url');
            $this->load->view("myView");
        }
       
    }
?>


myView.php
<html>

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

    <body>
        <div id="container">
                 Hello world test
        </div>
    </body>
</html>


The most important two lines of codes are bellow


  1. <link rel="stylesheet" type="text/css" href="<?php echo base_url();?>/styles/myStyle.css">
            Here <?php echo base_url();?> returns your site's base url. After that you need to add the    directory where your css file is located.

     2.  $this->load->helper('url');
           This is the other line of code. If you call the base_url() method you need to add this line of code in your method where you load your view.


After these steps you are done. 
Thanks guys. I hope this will some one's life.
:D

Comments

Popular posts from this blog

Enabling Custom SSL Profiles in WSO2 ESB 4.8.1

Using WSO2 ESB HTTP Endpoints to define Restful Endpoints

Integrating WSO2 ESB Connectors in real world integration Scenarios