CodeIgniter Forums
cant link stylesheet, not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: cant link stylesheet, not working (/showthread.php?tid=37411)

Pages: 1 2


cant link stylesheet, not working - El Forum - 01-09-2011

[eluser]jp612[/eluser]
Hi,

Im completely new to Code Igniter so just bear with me here... Smile

Im trying to link a css file to a html page in my views. I used the following code:

Code:
<link rel="stylesheet" type="text/css" href="<?php print $this->config->item('base_url'); ?>/css/style.css" />

and it outputs this: <link href="localhost/codei01/css/style.css" type="text/css" rel="stylesheet">

I've checked my config.php and I have put in the correct address link.

My css folder is under my views folder. Is this the correct thing to do?

The CSS sheet just isn't linking...

Thanks!


cant link stylesheet, not working - El Forum - 01-09-2011

[eluser]Nick_MyShuitings[/eluser]
You can use the simpler function call base_url() instead of the config item, its a shortcut.

Personally I use an assetts folder on the same level as views. so my links look like this:

Code:
<?=base_url()?>asstes/css/nameoffile.css



cant link stylesheet, not working - El Forum - 01-09-2011

[eluser]jp612[/eluser]
I tried that and I also activated the url helper.

it outputs this in the html file <link href="localhost/codei01/assets/css/style.css " type="text/css" rel="stylesheet">

however it also gives this error message within the <link href="localhost/codei01/assets/css/style.css " type="text/css" rel="stylesheet">

Code:
<p>The requested URL /codei01/localhost/codei01/assets/css/style.css was not found on this server.</p>

the URL is all mucked up in the error message since there is another codei01 infront of the localhost.
How do i fix this?


cant link stylesheet, not working - El Forum - 01-09-2011

[eluser]Nick_MyShuitings[/eluser]
your base URL should be 'http://localhost' , just in case. without that or without an auto detecting base url, it looks like your setup is doing a relative url. ie, its looking for a folder called localhost inside of the site folder.


cant link stylesheet, not working - El Forum - 01-09-2011

[eluser]jp612[/eluser]
whoops i made it
Code:
localhost/codei01

I changed it to
Code:
http://localhost
I tried it with and without a trailing slash.

it gives this error now
Code:
<p>The requested URL /assets/css/style.css was not found on this server.</p>

how does it know to look in system/views for the assets folder?

Do i have to unconfigure relative url in my php.ini?


cant link stylesheet, not working - El Forum - 01-09-2011

[eluser]Dizza[/eluser]
[quote author="jp612" date="1294623143"]whoops i made it
Code:
localhost/codei01

I changed it to
Code:
http://localhost
I tried it with and without a trailing slash.

it gives this error now
Code:
<p>The requested URL /assets/css/style.css was not found on this server.</p>

how does it know to look in system/views for the assets folder?

Do i have to unconfigure relative url in my php.ini?[/quote]

Dunno if my solution works, but better try to help someone then not helping Tongue

This is how i have it
Code:
&lt;link rel="stylesheet" href="&lt;?php echo base_url() . 'css/cms.css'; ?&gt;  " type="text/css" /&gt;

I made a map css @ the root folder of codeigniter and called it css. In the css map there is my cms.css. Maybe this works for you Big Grin


cant link stylesheet, not working - El Forum - 01-09-2011

[eluser]jp612[/eluser]
i tried it but no change.

I think its more of a URL error rather than syntax.

Thanks anyway though!! Smile


cant link stylesheet, not working - El Forum - 01-09-2011

[eluser]Nick_MyShuitings[/eluser]
Quote:how does it know to look in system/views for the assets folder?

It doesn't. It has no idea where to look... this isn't magic.

if your localhost looks like this
|
\codei01
|
\index.php (codeigniters index)
\assets
|
\css
|
\style.css

Then the link would be Http://localhost/codei01/assets/css/styles.css

The entire point of using base_url is to NOT use relative Urls. If you want to go relative urls, then you need to think of the relation between the file and your index.php. The system will not in any way attempt to guess that the css is in the views folder.


cant link stylesheet, not working - El Forum - 01-09-2011

[eluser]jp612[/eluser]
Ah that was my problem i didnt put codei01 into the URL so now its:

Code:
&lt;?php echo base_url() . 'codei01/assets/css/style.css'; ?&gt;

Thanks a lot!!


cant link stylesheet, not working - El Forum - 01-09-2011

[eluser]Nick_MyShuitings[/eluser]
np. There is a patch you can put into your constants.php or config.php files in the config folder. That makes your base_url() automatically detect what the url is.

It makes it a lot easier when you don't have to worry about changing that when you push the site live.

But remember, the entire point of using base_url() is to make it so you are NOT writing relative links.

Code:
/*
|--------------------------------------------------------------------------
| Docment root folders
|--------------------------------------------------------------------------
|
| These constants use existing location information to work out web root, etc.
|
*/

// Base URL (keeps this crazy sh*t out of the config.php
if(isset($_SERVER['HTTP_HOST']))
{
    $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
    $base_url .= '://'. $_SERVER['HTTP_HOST'];
    $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
    
    // Base URI (It's different to base URL!)
    $base_uri = parse_url($base_url, PHP_URL_PATH);
    if(substr($base_uri, 0, 1) != '/') $base_uri = '/'.$base_uri;
    if(substr($base_uri, -1, 1) != '/') $base_uri .= '/';
}
else
{
    $base_url = 'http://localhost/';
    $base_uri = '/';
}
// Define these values to be used later on
define('BASE_URL', $base_url);
define('BASE_URI', $base_uri);
define('APPPATH_URI', BASE_URI.APPPATH);

// We dont need these variables any more
unset($base_uri, $base_url);