Forum Moderators: not2easy

Message Too Old, No Replies

Is it possible to link to an embedded stylesheet?

         

mmmwowmmm

7:26 am on Sep 26, 2006 (gmt 0)

10+ Year Member



Let's say on one page I have embedded style information, as in:

<head>
<style type="text/css">
body {
color:red;
background-color:black;
}
</style>
</head>

and then let's say that I have another webpage that I want to also use this style information, but I don't want to write it out again... can I somehow link to, or import, the embedded style information from the first webpage?

Thanks,
Phil

DrDoc

7:45 am on Sep 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not possible.

mmmwowmmm

8:02 am on Sep 26, 2006 (gmt 0)

10+ Year Member



Actually, I just figured it out, and it works. I did it like this:

<link rel='stylesheet' href='http://example.com/somepage.html' type='text/css' />

Now, to be acurate, this isn't exactly how I did it, since my site runs on php, but this is what the html output amounts to. Anyway, I'll check it later in straight html, just for my own curiousity, but this is working for me now.

rocknbil

9:10 am on Sep 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're saying that works? ODD! I will have to test that . . . but I have to ask. Why wouldn't you just put the CSS in an external file and link to it from both pages that need it?

mmmwowmmm

9:26 am on Sep 26, 2006 (gmt 0)

10+ Year Member



"Why wouldn't you just put the CSS in an external file and link to it from both pages that need it?"

Kind of a long story: I have a site that allows users to create their own sub-sites within the main site. Each user has a page named "style", which contains their embedded style information, which is automatically applied to all the pages in their sub-site. Each user also has a rich-text editor, which they can use to create pages on their sub-site. So I wanted the rich-text editor to use the style from their own site, so they would see what their page would actually look like as they were creating it - and since the style information is embedded into one of their other pages, and not an external css file, I needed the rich-text editor to link to the embedded style.

It is surprizing that that works, it was such a simple solution that I almost didn't even try it.

Lexur

9:40 am on Sep 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Did you test in other browsers?

mmmwowmmm

9:47 am on Sep 26, 2006 (gmt 0)

10+ Year Member



"did you test it in other browsers?"

It's working in IE, FF and NS6. Haven't tried any others yet...

leadegroot

11:59 am on Sep 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd be guessing that it doesn't validate? :)
I think this is working through a bug in the browsers - because they are so tolerant of sloppy code (this is why pages that don't validate will often look fine - the browser developers have done incredibly amounts of extra work to figure out what sloppy markup means) you can put a lot of gunk in and the browsers will still extract the intended css (or in other cases html) from the file.

I wouldn't call this a technique to learn from....

SuzyUK

5:05 pm on Sep 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



my guess would be that the source shows a duplication and only the valid bits of each are being rendered?

Anyway interestingly enough I'm doing something similar to this on a site of my own (where I require embedded CSS per article, which sometimes extends to a multi page article, or gets displayed in a list along with other articles) - I did it through the CMS PHP code based on the article ID number

- in theory (and remember my PHP is not the best ;)) I created a unique variable based on the unique article number then used that variable to pull in the required CSS, via an if/else statement in the CMS code, I'm guessing this theory could be used to create a unique variable by user ID to do the same thing?

This is not really a css question (though I'm interested to see how/why your solution works) more a PHP one perhaps, your users can already store their own CSS you just need to find the cleanest way to store/retrieve the info based on your site programming

Suzy