Link to home
Start Free TrialLog in
Avatar of phparmy
phparmy

asked on

using absolute paths in css files.

i have disorganized directories in my web project.

directory is like
www/myproject
........................./con.php
........................./index.php
........................./conf.php
........................./images/mainpage/1.jpg
........................./images/2.jpg
........................./css/style.css
........................./lib/functions.php
........................./lib/template/mainpage.php
........................./user/index.php
......................../admin/external/tech.php
----------------------------------------------------------------
in my style.css
#box{
      background-image:url(../images/mainpage/1.jpg);
}

#line{
      background-image:url(../images/2.jpg);
}

when i include this css file from /user/index.php it works fine but when i call this css from /admin/external/tech.php

is there any solution to make absolute paths for css files.
i want to make this
#box{
      background-image:url(/myproject/images/mainpage/1.jpg);
}

#line{
      background-image:url(/myproject/images/2.jpg);
}

i know how to do this in php files or in html via javascript. but i can not find any way to do this in css.

thanks.
Avatar of FDMilwaukee
FDMilwaukee
Flag of United States of America image

to use an absolute path you have to define it the following way = http://www.mydomain.com/myproject/images/mainpage/1.jpg

if your site is secure using ssl your path would look like this = https://www.etc, etc, etc

hope that helps

FDM
Avatar of hielo
>>i want to make this
#box{
      background-image:url(/myproject/images/mainpage/1.jpg);
}

#line{
      background-image:url(/myproject/images/2.jpg);
}

Get rid of "/myproject":


i want to make this
#box{
      background-image:url(/images/mainpage/1.jpg);
}

#line{
      background-image:url(/images/2.jpg);
}
Avatar of Member_2_4694817
Member_2_4694817

As the syntax "url()" suggests, the part in bracket is a URL.
You may use relative URLSs ans in your example, i.e. url(foo1.jpg) or url(dir/foo2.jpg) or url(../foo3.jpg)
but also url(/absdir/foo4.jpg) and of course url(ftp://hostname/path/foo5.jpg)

Assuming your CSS file is retrieved as http://yourserver/something/cssdir/cssfile.css, the effect of the above examples is that the client will look for
http://yourserver/something/cssdir/foo1.jpg
http://yourserver/something/cssdir/dir/foo1.jpg
http://yourserver/something/foo3.jpg
http://yourserver/absdir/foo4.jpg
ftp://hostname/path/foo5.jpg

Relative path is always relative to the place where the containing file is, absolute path is from the webroot of the same webserver and with scheme and host you may even reference external resources.
Avatar of phparmy

ASKER

i am asking how can i define this via javascript or etc. because i do not want to make changes in my css when  i setup my web application to new server.
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
to not make changes in your site when you upload it, you don't want to use absolute paths at all!!!  You want to use relative paths!
Hi phparmy,

I was wondering why css in your code is not working properly, even you already have correct directory structure of your project folders, can you please provide the link where your project is live,

so that i can help you out :D

Vinay Rajput



SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phparmy

ASKER

main problem is.
www.phparmy.com/project1/
www.phparmy.com/project2/

you see it is not main root in one css you have to write
/project1/images/image1.jpg
/project2/images/image2.jpg

but i think there is no way to do this automatic. thanks.