Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load an external CSS for a specific DIV

So basically I have a page that has it's own CSS. On my server I have a folder of different CSS style files, so I want to have a "preview" window on my page for them. Can I somehow, maybe using javascript, apply an external CSS file only to a certain DIV, not to the entire page so I can get a "preview" of the custom CSS file? I'd rather not use iframes.

like image 926
Martin Marinov Avatar asked Sep 11 '11 15:09

Martin Marinov


People also ask

How do you reference an external CSS file?

Using the LINK Element We can use the XHTML element LINK to reference an external CSS file. This method will be recognized by many older browsers. Link the STYLE element; the LINK element needs the TYPE attribute to be "text/css", and the REL attribute to say "stylesheet" just in case!

Does external CSS override internal?

It works kind of counter-intuitively, so just to explain further: inline styles override internal CSS, and internal CSS overrides external CSS files, and external CSS files override browser defaults. One way to think about it is like layers. The “closer” the style is to the element, the higher precedence it has.

How do I load a CSS file asynchronously?

To load CSS Files asynchronously in both Chrome and Firefox, we can use “preload” browser hint and “media='print'” attribute along with onload event feature in a ordered way. you may use the codes below to load CSS Files without render-blocking resources features in Firefox and Chrome.


1 Answers

Like Quentin said, you can use descendent selector to limit the scope. Something like less can help a lot. For example, I would like to have "bootstrap.css" applies only to #MyDiv, in "my.less":

#MyDiv {
  @import "bootstrap.less";
}

"bootstrap.css" should be renamed (or linked) to "bootstrap.less". Run:

lessc my.less my.css

would add #MyDiv to every selectors in the imported file.

like image 178
Arthur Yip Avatar answered Sep 28 '22 10:09

Arthur Yip