6

I need to apply a pre-existing style-sheet only to elements within a specific div, is this possible?

Long, drawn-out reason: We have a page fragment with a css file. We now want to include this page fragment inside another page. Doing so is easy enough but its css and the new page's css conflict. Editing either style-sheet heavily would be difficult as both are large and need to maintain compatibility with how they were before. This brings me to the above question.

5
  • 3
    Put the fragment in an iframe? Commented Oct 19, 2011 at 17:53
  • @Šime Vidas: I was thinking about that but that would get very messy very quick with our js and controllers. It would still be easier than messing with the css in our case. I am still fishing for a simple & clean solution. Commented Oct 19, 2011 at 18:22
  • i'd give the wapping div an id or class and add that to every rule in the new css. maybe you can use a replace or regex to add it to every rule at once Commented Oct 19, 2011 at 18:22
  • 1
    You could have the server modify the css on the fly, and add "#fragment " before every rule, using some regex replace. Commented Oct 19, 2011 at 18:30
  • @Šime Vidas: Well it seems like your comment may be the winner. Turn that into an answer if you could. Commented Oct 19, 2011 at 19:32

3 Answers 3

5

Place an IFRAME on the page and load the fragment inside it.

<iframe id="section" src="fragment.html"></iframe> 
Sign up to request clarification or add additional context in comments.

1 Comment

Not the ideal solution but certainly the one that contains the changes the most.
5

do you know http://lesscss.org/ ?
it supports nested rules.
you could nest your entire new css in a new rule (from the class of your div) like that:

div#the-new-div{
   div{

   }
   /*** your entire css ***/
}

less will compile it to div#the-new-div div{...}
i know you probably don't want to implement less into your site but there are standalone less compiler like that: http://digitalpbk.com/less-css/less-css-compiler-windows-lesscexe. then just insert the compiled css

2 Comments

That's neat. That will warrant another look next time a personal project comes along.
Yea I used it in one of my projects already and it works really well. Had a smile on my face all the time :)
2

No you cannot.

You need to change the CSS which should be used in your <div>. For example like this:

Add an id to your <div> e.g. <div id="foo"> and prefix your CSS rules in the stylesheet you want to use for that <div> with #foo.

Disadvantage: you need to add the same id to your other site's html-tag.

1 Comment

That is a possible solution but I am afraid there are simply too many rules for that to look right (and if someone adds something to site A's css and doesn't prefix it then site B has an issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.