Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use relative/absolute paths in css URLs?

I have a production and development server. The problem is the directory structure.

Development:

  • http://dev.com/subdir/images/image.jpg
  • http://dev.com/subdir/resources/css/style.css

Production:

  • http://live.com/images/image.jpg
  • http://live.com/resources/css/style.css

How can I have a style.css in css folder that uses on both servers the same path for the background: url property? Is there a trick I can use with relative paths?

like image 375
johnlemon Avatar asked Apr 28 '11 07:04

johnlemon


People also ask

Should I use relative or absolute URLs?

An absolute URL contains more information than a relative URL does. Relative URLs are more convenient because they are shorter and often more portable. However, you can use them only to reference links on the same server as the page that contains them.

How do you use absolute and relative paths?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).

What is absolute relative URL?

An absolute URL contains all the information necessary to locate a resource. A relative URL locates a resource using an absolute URL as a starting point. In effect, the "complete URL" of the target is specified by concatenating the absolute and relative URLs.

How do you use an absolute URL in HTML?

An absolute URL is the full URL, including protocol ( http / https ), the optional subdomain (e.g. www ), domain ( example.com ), and path (which includes the directory and slug). Absolute URLs provide all the available information to find the location of a page.


2 Answers

The URL is relative to the location of the CSS file, so this should work for you:

url('../../images/image.jpg') 

The relative URL goes two folders back, and then to the images folder - it should work for both cases, as long as the structure is the same.

From https://www.w3.org/TR/CSS1/#url:

Partial URLs are interpreted relative to the source of the style sheet, not relative to the document

like image 153
Kobi Avatar answered Oct 16 '22 11:10

Kobi


Personally, I would fix this in the .htaccess file. You should have access to that.

Define your CSS URL as such:

url(/image_dir/image.png); 

In your .htacess file, put:

Options +FollowSymLinks RewriteEngine On RewriteRule ^image_dir/(.*) subdir/images/$1 

or

RewriteRule ^image_dir/(.*) images/$1 

depending on the site.

like image 34
Garison Piatt Avatar answered Oct 16 '22 12:10

Garison Piatt