tags:

views:

36

answers:

2

Hello guys, I am first time poster. A question. How do a make a css declaration that only works within one DIV, but, not overwriting the global css? I want to jQuery loading a page into a DIV, however, the page's CSS changed my own site's CSS. I don't want that. Also I can't just take out CSS because I want them looked as intended from the source.

Basically we are going to load an external HTML with its CSS style applied locally ONLY without it changing the style elsewhere. The external HTML is not using inline CSS since we don't have control over it. They are applied to class values or even all element type. We don't want their CSS declaration modifying our own existing CSS outside of the DIV container.

Is this even possible? Thank You?

+2  A: 

If I understand your question correct you would place an id in the div <div id="mystyle"> content </div>. In your CSS you would write #mystyle p { color:red; }. which have no effect on global paragraphs outside the "mystyle" div.

BennySkogberg
why use id's? Id is meant to be used as indetifier. Classes are meant to define styles. One element can have many classes, but just one id. If you have many buttons on one page, and you want to define same style to all of them, then you cannot use id's.
newbie
@Benny: As I understand it, the OP is loading an entire HTML page into a `div`, so he doesn't have control over the stylesheets on that page.
casablanca
@newbie Not really. you can have the same class name on multiple elements in a html-document, but only one specific id to one element in the html-document. Both classes and id's works perfectly and are valid identifiers for cascading style sheets.@casablanca The question is a bit unclear, so I hope I'll get a reply from @user413258 in any minute, so we can contine the discussion on css.
BennySkogberg
A: 

I guess you are asking how to apply an external stylesheet to just one div. There is no way to do this using just CSS. You might be able to emulate this using JavaScript, but it's going to take quite a bit of work. Here's an outline of how you might go about doing this:

  1. Grab the stylesheet filename from the loaded HTML and then get the contents of the CSS file via AJAX.
  2. Somehow parse the CSS and prefix your div ID to each CSS rule, so that it applies only within your div.
  3. Inject the modified stylesheet as inline text into the loaded HTML.

Steps 1 and 3 are relatively simple, step 2 requires a CSS parser written in JavaScript. (There seems to be one available here although there is no documentation.)

casablanca
LOL, this might work, but, rather difficult. Thank you for the idea.