tags:

views:

39

answers:

1

Hi have noticed a few sites that give the style tag an id such as:

<style id=style-id></style>

Can anyone explain firstly why you would do this and also the benefits of doin so?

+1  A: 

So you can reference it (just like any other element), i.e.

var styles = document.getElementById('style-id');
// do anything you want, like
styles.parentNode.removeChild(styles); // remove these styles
styles.setAttribute('href', 'alternate-styles.css'); // change the style
Dan Beam
so you can use it to create dynamic styles using javascript then?
David
Yes, you can add or delete stylesheets, move them up and down in the page's DOM, or possibly even read their contents? (never tried it) You can additionally style individual elements by change their `.style` properties (i.e. `document.body.style.backgroundColor='red';`), but I highly recommend a library for anything major.
Dan Beam