Placing of css in codeigniter views

23,010

Solution 1

It is not a good idea to put everything in your view file, as those folders are supposed to clean things up, putting them in view makes them messy again.

Assume this is your directories:

Localhost

  • Application
    • Controllers
    • Views
    • Etc..
  • System
  • Assets
    • Javascripts
    • CSS

To include any of javascripts, the path would be:

localhost/Assets/Javascripts/your_script.js

and obviously CSS would be

localhost/Assets/CSS/your_stylesheet.css

In order to make sure your implementation work everywhere,

<script src="<?=base_url('path/to/your/js/foo.js')>" type="text/javascript"></script>

would be the safest way

Solution 2

You need to put the files in the base directory e.g. the one containing the application and system folders.

Try using a folder called assests.

Also make sure your are using a leading slash so e.g.

href="/assets/css/style.css"

Note this will load from base URI

e.g. example.local/assets/css/style.css

Solution 3

I would suggest you to not keep your js/images/css files in the view folder.

Please refer to this past question. Where do I put image files, css, js, etc. in Codeigniter?

As to answer your question, it is like Dale said.

First you will need to check your .htaccess, and note where is your base directory.

You'll then have to provide a path link that is appropriate to your js/css folder. If you kept them in your views folder, it seems like you'll have to do this url

http://localhost/codeigniter/index.php/application/views/css/text.css

If I am not wrong, you are currently at

"http://localhost/codeigniter/index.php/admin"

trying to load the css files. If you use href="something", it will merely add on to your current URL. Which will provide "http://localhost/codeigniter/index.php/admin/something" which IMO is not what you're looking for.

Share:
23,010
bmrinal
Author by

bmrinal

Updated on July 28, 2022

Comments

  • bmrinal
    bmrinal almost 2 years

    I am new to codeigniter. I made a simple controller to load the three views: header, body and footer. My header contains links to several javascripts and CSS which have a relative paths. I kept all the css and js relative to file in views folder.

    The problem is that the controller loads everything properly but images, js and css are not getting loaded.

    This is the hierarchy of my view:

    CSS(sub directory in views) JS (sub directory in views)

    header (header view file) body (body view file) footer (footer view file)

    ny idea?

  • bmrinal
    bmrinal about 12 years
    Can you please elaborate a standard and safe practice for this. i am new to codeignitor. Thanks.
  • He Hui
    He Hui about 12 years
    I am not a 'pro' either. What I normally do is this: I keep my files in /Extras/img, /Extras/css, /Extras/js. Hence, if I ever need to access any image files, I will do this: base_url('Extras/img/foo.jpg')
  • bmrinal
    bmrinal about 12 years
    thanks :) i am following this: stackoverflow.com/questions/6630770/…
  • He Hui
    He Hui about 12 years
    Hope that helped. Good luck :)
  • Broncha
    Broncha about 12 years
    Yes you should place all your static files outside application folder say assets and refer to the in your view files using base_url()
  • Dale
    Dale about 12 years
    I'm going to +1 this because you have the best name ever!!