Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making CSS url() relative to document

Tags:

css

url

When it comes to CSS the following rule applies:

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

But here's my problem :

I have different websites that use the same CSS file. While they use the same layout, the actual images the CSS references are different for each one of them.

Exemple:

#header {
width: 960px;
height: 200px;
background: url(/images/header.png);
}

Each domain has its own "images" folder and its own "header.png" that I would like the CSS to reference. Currently it behaves as it's supposed to and tries to find the png file on the domain where the CSS is hosted. What I want is for it to get the png file from the domain where the CSS file was called.

I use "link" for the stylesheets because "@import" breaks progressive rendering in IE.

Any suggestion or workarounds?

like image 252
Enkay Avatar asked Nov 14 '09 16:11

Enkay


People also ask

How do you give a relative path of a CSS file in HTML?

Relative: If you want to reference something you know is in the same path on the url - that is, if it is in the same folder, for example http://mysite.com/myUrlPath/index.html and http://mysite.com/myUrlPath/css/style.css , and you know that it will always be this way, you can go against convention and specify a ...

How do I create a relative URL in HTML?

To link pages using relative URL in HTML, use the <a> tag with href attribute. Relative URL is used to add a link to a page on the website. For example, /contact, /about_team, etc.

How do I give a path to a previous directory?

Starting with / returns to the root directory and starts there. Starting with ../ moves one directory backward and starts there. Starting with ../../ moves two directories backward and starts there (and so on...) To move forward, just start with the first sub directory and keep moving forward.

What is absolute path in CSS?

“ - [Instructor] Another way to link to web resources is to use an absolute path. These resources are located on a server so the entire URL, including the HTTP and full domain name must be referenced. This technique is often used for linking to pages outside of your website.


2 Answers

What I want is for it to get the png file from the domain where the CSS file was called.

You're going to have to have separate URLs for each domain referenced. So www.example1.com references its stylesheet as /style/sheet.css and thus grabs it from http://www.example1.com/style/sheet.css whilst www.example2.com gets it from http://www.example2.com/style/sheet.css.

However just because they look different from the client end that doesn't mean they have to be different files on the server side (with all the maintenance that would imply). You could just point an Alias on each domain to the real, shared CSS file.

The only other workaround I can think of would be to split out the URL-referencing rules like background-image and put them in a domain-specific stylesheet or an internal stylesheet.

like image 157
bobince Avatar answered Oct 05 '22 13:10

bobince


You can duplicate your CSS file on each website using a server-side script.

example1.com/global.css is your CSS file

example2.com/global.css.php is a PHP script that will actually return the global.css file of example1.com

The script can be as simple as

<?php
readfile('http://example1.com/global.css');
?>

But you would need more code if you want to handle caching better.

like image 20
Vincent Robert Avatar answered Oct 05 '22 12:10

Vincent Robert