Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically load stylesheets

Tags:

html

import

css

i know that you can have style-sheets in the head of a page, but i like to have them in a separate file. Now i'm working with a single page application.

Well in an SPA the content is dynamic, right? so i didn't want to import all the style-sheets in the head section with the link tag. Can i somehow import style-sheets as-and-when i need them?

I mean, can i have a link in the body, such that whenever my SPA loads some dynamic content, a style sheet also gets loaded? Such that i dont have to load all the stylesheets even when the dynamic content is not loaded..

I stress again: Whenever the content loads, the styles load.

I know i can do it with the help of an inline style like this:

~PSEUDO CODE 
<tagname style="somestyle"></tagname>

but can i have some dynamic file imports too? Can i have the link tag in the body too? Even if it works, is it standard?

like image 948
Anshu Dwibhashi Avatar asked Aug 29 '13 11:08

Anshu Dwibhashi


People also ask

How do I dynamically load CSS files?

Load CSS and JS files dynamically: We create a script element for the JS file and link element for the CSS file as required using DOM, assign the appropriate attributes to them, and then add the element to the desired location within the document tree using the element. append() method.

Can you import multiple stylesheets?

Yes, It is possible to include one CSS file in another and it can be done multiple times. Also, import multiple CSS files in the main HTML file or in the main CSS file.

Can we change CSS dynamically?

color = "red"; you can apply the style change dynamically. Below is a function that turns an element's colour to red when you pass it the element's id . You could also use setAttribute(key, value) to set a style on an element. For example, you could set the colour of an element to red by calling element.

What are dynamic styles?

Dynamic stylesThe object model provides programmatic access to styles. This means you can change inline styles on individual elements and change style rules using simple JavaScript programming. Inline styles are CSS style assignments that have been applied to an element using the style attribute.


2 Answers

You should look into asychronously loading assets, such as the famous google-analytics code. You can load external stylesheets using Javascript.

JavaScript

(function(){
  var styles = document.createElement('link');
  styles.rel = 'stylesheet';
  styles.type = 'text/css';
  styles.media = 'screen';
  styles.href = 'path/to/css/file';
  document.getElementsByTagName('head')[0].appendChild(styles);
})();

Lines 1 and 7 create a new scope for variables such that local variables do not collide or override with globally scoped variables. It isn't necessary just a best practice. This solution also assumes you have a <head> tag in your html.

like image 79
cjross Avatar answered Sep 22 '22 06:09

cjross


You can add/remove/edit link tags in your head area with java script to add/remove stylesheet files.

Code example:

Add a stylesheet to the head:

var newstyle = document.createElement("link"); // Create a new link Tag
// Set some attributes:
newstyle.setAttribute("rel", "stylesheet");
newstyle.setAttribute("type", "text/css");
newstyle.setAttribute("href", "filename.css"); // Your .css File
document.getElementsByTagName("head")[0].appendChild(newstyle);

To remove or edit a stylesheet you can give every stylesheet an id attribute and access it with this:

document.getElementById('styleid')

Or you can loop through all link tags in the head area and find the correct one but I suggest the solution with the ID ;)

Now you can change the href attribute:

document.getElementById('styleid').setAttribute("href", "newfilename.css");

Or you can remove the complete tag:

var styletorem = document.getElementById('styleid');
styletorem.parentNode.removeChild(styletorem)
like image 36
virtualmarc Avatar answered Sep 23 '22 06:09

virtualmarc