CSS

Dynamically Add a CSS Class using Javascript

Posted in CSS on August 10, 2006

0


I was writing a module for a webapp today and was thinking how cool it would be if I could dynamically add CSS classes using Javascript.  At that particular moment, I had all the classes for all the modules grouped together in one big file, but since sometimes I didn't load all the modules, it was inefficient.

It took me a while to find some examples on doing this, so figured I would post the function I wrote in case anyone else has this same problem.
function addCSSclass(className, classRule) {
 if (document.all) {
  document.styleSheets[0].addRule("." + className, classRule)
  } else if (document.getElementById) {
  document.styleSheets[0].insertRule("." + className + " { " + classRule + " }", 0);
  }
 }
Enjoy!

CSS Media Types

Posted in CSS on June 18, 2006

0


One thing that really frustrates me is when I try to print something off a website, and my printer prints three pages of headers, graphics, and menus, and ONE page of what I actually wanted to print.  Some designers have thought ahead and have a link to open their pages in a new window, unformatted, but this doesn't always work, especially if the page is, for example, dynamically generated by submitting a form, or the user has a pop-up blocker.  Fortunately, there's a really cool CSS technique for making this easier.  It lets you use one stylesheet for what your users see on the screen, and a completely different stylesheet for what you print!  And it works like a charm!

Read more...