Welcome Guest, Not a member yet? Register   Sign In
Relative link to CSS
#1

[eluser]tokyotech[/eluser]
My directory structure is like this:

codeIgniter
---views
------style
---------master.css
---------base.css
---header.php

In header.php, how do I import master.css using a relative link? When I use "style/master.css", it doesn't get loaded.
#2

[eluser]Colin Williams[/eluser]
You shouldn't use a relative link. It's not relative to your server's file system, it's relative to the URL. It doesn't work like including local files. You are telling the browser where the file is, not your server.
#3

[eluser]John_Betong[/eluser]
 
Try the following and use the relative path from the getcwd();
Code:
echo getcwd();
 
 
 
#4

[eluser]Colin Williams[/eluser]
WHAT!? No! The server's current working directory is still meaningless. It's the current URL that counts! Why does nobody understand this!?
#5

[eluser]Thorpe Obazee[/eluser]
[quote author="Colin Williams" date="1247122985"]WHAT!? No! The server's current working directory is still meaningless. It's the current URL that counts! Why does nobody understand this!?[/quote]

lol. I dont' think John_Betong understood the question.
#6

[eluser]John_Betong[/eluser]
[quote author="bargainph" date="1247124641"][quote author="Colin Williams" date="1247122985"]WHAT!? No! The server's current working directory is still meaningless. It's the current URL that counts! Why does nobody understand this!?[/quote]

lol. I dont' think John_Betong understood the question.[/quote]
 
Maybe I am right and maybe I am wrong, it will not be the first time Smile
 
Anyway I reckon to use getcwd() to establish where the file is located. Once this path is known then create a relative path from the getcwd() path to the JS/CSS file.
 
 
 
#7

[eluser]TheFuzzy0ne[/eluser]
[quote author="tokyotech" date="1247118513"]My directory structure is like this:

codeIgniter
---views
------style
---------master.css
---------base.css
---header.php

In header.php, how do I import master.css using a relative link? When I use "style/master.css", it doesn't get loaded.[/quote]

Try /views/style/master.css. The path should be relative to the Web root.

You could instead do something like this from within your header:

Code:
<link rel="stylesheet" type="text/css" href="<?php echo base_url().'application/views/style/master.css'; ?>" />
#8

[eluser]tokyotech[/eluser]
Sorry, my header.php was actually inside my views folder:

Code:
veetleRefactored
---application
------views
---------style
------------master.css
------------base.css
---------header.php

I prefer not to use the base_url() thing because that just prints out www.example.com. Even if I changed it to localhost, it would be a hassle to change it back and forth when uploading it to my real web host.

I tried using the getcwd() method:

Code:
<link href="<?=getcwd()?>\application\views\style\master.css" rel="stylesheet" type="text/css" />

That does seem to print out the correct path:

Code:
<link href="C:\wamp\www\veetleRefactored\application\views\style\master.css" rel="stylesheet" type="text/css" />

But it still doesn't load my CSS. I'm still looking at plain black and white HTML. When I copy and paste that link into my browser, I get redirected to Yahoo Finance: http://finance.yahoo.com/q?s=c . That's strange!
#9

[eluser]TheFuzzy0ne[/eluser]
[quote author="tokyotech" date="1247178984"]Sorry, my header.php was actually inside my views folder:[/quote]

Irrelevant.

[quote author="tokyotech" date="1247178984"]I prefer not to use the base_url() thing because that just prints out www.example.com. Even if I changed it to localhost, it would be a hassle to change it back and forth when uploading it to my real web host.[/quote]

That's life - Just how difficult can changing a string be? Quite often you'll need a different configuration on your development server than you would on your production server. However, there is a way to get around it as a temporary measure.

You can add an entry to your hosts file (found in C:\Windows\system32\drivers\etc\), like so:

Code:
127.0.0.1    www.yoursite.tld

Obviously you need to replace the address with the address of your site, and that will cause the DNS to resolve the localhost for each request. Obviously, this can interfere with the real site, so I'd suggest just having a separate config.php for each site, and maybe having your development site as dev.yoursite.tld, so you can easily switch between sites.

In any case, it's not too serious if you choose not to use base_url(), although I choose to use it for consistency.

[quote author="tokyotech" date="1247178984"]I tried using the getcwd() method:

Code:
<link href="<?=getcwd()?>\application\views\style\master.css" rel="stylesheet" type="text/css" />

That does seem to print out the correct path:

Code:
<link href="C:\wamp\www\veetleRefactored\application\views\style\master.css" rel="stylesheet" type="text/css" />
[/quote]

As advised by Colin, getcwd() is useless in your situation, since the file is accessed from outside of the server, and therefore, the path must be relative to your Web root.

[quote author="tokyotech" date="1247178984"]But it still doesn't load my CSS. I'm still looking at plain black and white HTML. When I copy and paste that link into my browser, I get redirected to Yahoo Finance: http://finance.yahoo.com/q?s=c . That's strange![/quote]

You've probably agreed somewhere whilst installing software to have your default search provider changed to yahoo. That's one of the pitfalls of selecting "Next" on every dialogue of the installer, without reading anything it says. Wink

Please read my previous post again - I believe the instructions are clear enough to resolve your problem.

EDIT: This would be my solution:
Code:
<link href="/application/views/style/master.css" rel="stylesheet" type="text/css" />;

However, I'd like to point out that a lot of people consider it bad practice to allow direct access to files within your system/application directory via the URL. Ideally, those directories should only be accessible by script on the server.
#10

[eluser]John_Betong[/eluser]
 
Using getcwd()
It seems that using getcwd() is considered useless. I just think that my previous post was not clear.
 
So to re-iterate:
 
I echo getcwd() once to find out where the file is located and then delete getcwd().
 
I create a relative path to the CSS file. The CSS file is located in the views folder.
 
This works fine for:
1. LOCALHOST
2. On line
3. For over a dozen LOCALHOST CI applications that share a common CI system folder.
 
 
edit:
I just noticed that the path to my CSS file is not relative and has a leading slash "/". Changing to a relative path fails to find the CSS file. I am now confused Smile
 
 
 




Theme © iAndrew 2016 - Forum software by © MyBB