tags:

views:

260

answers:

7

I have an external CSS file (I cannot change it at all) which I need to use in my HTML file, but I want the CSS to only affect a section of my HTML. (For example everything in <div id="externally_styled"></div>)

How is this possible, again, without changing the CSS file (and the CSS file contains also general styles that affect body tags etc)

+4  A: 

You'd probably have to use an iframe with a page containing only the HTML you want styled and a reference to the stylesheet. This would mean the general styles wouldn't be applied to the containing page, but it sounds like that's what you want.

roryf
Don't forget that cascading stylesheets are cascading, local style definitions overwrite external ones. - So go easy on it.
BeowulfOF
Thanks, I used your iFrame solution to solve the problem. (Thought I had thought of it but I thought there might be some HTML tag/attribute that would allow to include an external css file in only a certain section of the HTML. )
EdanB
A: 

One way that springs to mind is to have the "to-be styled" portion of your HTML exist in a completely separate file and then pull it in via an iframe that uses the CSS from the external file.

tschaible
A: 

The only thing i can think of is to re-render the content from your DIV to an Iframe.

My Alter Ego
+1  A: 

Any classes or style-declarations attached to a tag will override the declarations in the CSS-file.

Just add your own style-declaration to a tag:

<div style="<your own declarations>">
...
</div>
TFM
Horrible but since the problem in itself already is horrible it would a viable solution
borisCallens
I agree that it's a horrible solution. Every declaration that is omitted in the tag's style attribute will default to the declarations in the css-file.
TFM
The problem stated that it was an external stylesheet
roryf
@Rory: so?
annakata
A: 

Either use classname of the class that you have created for your specific section or use proper parent child relationship css that will render only when it falls under the parent child relationship.

Vinay Pandey
+1  A: 

You can overwrite the general styles that you don't want to be applied to your HTML document. This may be a good idea if the CSS if not that extensive.

The way to overwrite an style is using the keyword important!.

e.g:

original stylesheet:

body {
    color: #000000;
}

your stylesheet:

body {
    color: #CCCCCC !important;
}

You can find more information here.

Freddy
+1  A: 

I'd guess any client side solution is going to be messy.

Can you use a server side solution where you suck in the external CSS file and append a class selector to the start of each rule? I'm sure this would be easy enough with regex.

edeverett
Yeah, that was my first thought. While it may not be possible to modify the original CSS file, no reason why it can't form the basis of a new, dynamically generated one.
Steve Morgan