css not loading
hi i am facing problem in loading css file.
<?php //if ( ! defined("BASEPATH")) exit("No direct script access allowed");
class Controller2 extends CI_Controller
{
function index()
{
if ($this->input->post('button'))
$this->test();
else
$this->load->view('2');
}
function test()
{
$this->load->view('2');
}
} ?>
when i first call my controller index function it is loading view "2" with proper html and css but when i press a button inside my "2" view my controller is calling another function TEST but this time test is not loading the same view with css.
开发者_运维百科can anyone solve this?
Using my psychic powers, because you've provided very little, I believe the answer is that you are providing a relative path to your CSS file.
In your view, you need to make sure that the CSS file is referenced absolutely. Examples:
Wrong
<link rel="stylesheet" type="text/css" href="style.css" />
This will only work if the path stays the same. However, your path is changing from /controller2
to /controller2/test
, which means on the second page, the browser is going to look for /controller2/style.css
for the first method, and /controller2/test/style.css
for the second.
Correct
<link rel="stylesheet" type="text/css" href="<?php echo site_url('style.css') ?>" />
Now, the output will be an absolute path to the stylesheet. Now, no matter the page, the browser will look in the same place.
(If this works for you, please make sure you check the checkmark to accept the answer. And then go back and accept the working answers from your previous questions!)
ya, site_url()
working for css as well as javascript linking
Try this:
<link rel="stylesheet" type="text/css" href="<?php echo site_url()?>style.css" />
精彩评论