CSS Designer and DOM scripter, sitting in a tree
Hello, I have a question for the CSS folk out there.
Currently I am writing a book for APress on Practical JavaScript and I am right now at the chapter about JavaScript and CSS working together.
Dynamic classes needed for styling JavaScript effects
The idea is to separate CSS and JavaScript completely – as explained in my “Unobtrusive JavaScript Course”:http://www.onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html. The JavaScript will dynamically add and remove classes to style effects instead of keeping the look and feel in JavaScript.
The question I was asking myself now is a maintenance one. I tend to separate out the class names as properties – in case that further down the line you need to use other class names as the initial ones. I also tend to comment the classes. Something like this:
scriptstuff={
// CSS classes
hideClass:'hide', // hide elements
currentClass:'current', // current navigation element
hoverClass:'over', // hover state
[... lots of dark magic, voodoo and other code...]
}
Note: If you are confused by the syntax, check the “show love to the object literal”:http://www.wait-till-i.com/index.php?p=239 post.
This does the trick quite nicely for a few CSS classes, but I wondered about sites with several different scripts and sections.
The first set of questions are:
- Do you feel comfortable with this arrangement?
- Would you know how to change the CSS class name if necessary?
Now, if there were a lot of dynamic classes to be defined in a site, would you feel more comfortable if all of them were in an own document in “JSON”:http://www.json.org/, for example named cssclasses.js:
css={
'hide':'hide', // hide elements
'current':'current', // current navigation element
'hover':'over', // hover state
[... and so on for a lot of them...]
}
As the DOM scripter, this would make it easier to spot what is CSS related, as you’d use css.hide,css.hover and so on.
Another more complex approach would be to use AJAX and parse a CSS file exclusively for dynamic classes (f.e. dynamicstyles.css) – thus leaving the commenting and the syntax to you. The CSS could be something like
/* hide */ .hide{
[... CSS settings ...]
}
/* current */ .current{
[... CSS settings ...]
}
/* warning */ .warning{
[... CSS settings ...]
}
All the DOM scripter would have to provide you with are the comment names.
Defining the scripting enabled and scripting disabled look and feel
I normally tend to apply a single class to the body when JavaScript and the DOM is available. something like:
if(!document.getElementById || !document.createElement){return;}
cssjs('add',document.body,'jsenabled');
This allows you to define two states in the CSS for every element:
#nav li{
[...settings...]
}
body.jsenabled #nav li{
[...settings...]
}
Would it be more useful to have two totally separate style sheets, like lowstyles.css and highstyles.css
It would be easy to remove the original styles and replace them with the dynamic ones via the DOM.
Or is all of this overkill? :-)


February 21st, 2006 at 8:42 am
I currently use a similar approach. I have a basic global css (Master.css).
And I attach advanced css properties, which only make sense when js is enabled, via DOM-scripting.
Users with js-disabled see my Master.css only and do not download the utility css as it does not makes sense for a non-js environment.
Regarding the use of object literal, I have adopted a few sets of rules:
For each page, I have a common.js and page-specific js file.
common.js
———
function init(){
if(typeof PageName != “undefined”)
{
PageName.init();
}
};
EventHandler.addEventListener(window,”load”,init);
PageName.js
———
var PageName={
init:function{
… page initialization code goes here …
}
};
So in order to add the css object literal, I would have used something like:
common.js
————
var Master={
css:{
/* global css properties */
hide:’hide’,
current:’current’,
hover:’over’
}
}
and in PageName.js
PageName.js
———–
var PageName={
init:function{
… page initialization code goes here …
},
css:{
/* page-specific css properties */
}
};
Being organized is not overkill and it shows its fruits on the long run imho.
February 21st, 2006 at 8:50 am
Hi Volkan,
this is very obvious to someone who knows JS a lot. However, it seems that you scatter the classes over a lot of documents. Have you used this is in a big project with lots of non-js-savvy designers?
February 21st, 2006 at 11:11 am
For ease of use, I think it would be best if they were in their own file like cssclasses.js slight overkill but that way even junior developers could make changes and not ruin the rest of the js file. I’m not sure what the JSON link is though – object not found.
I don’t think that’s neccessary – unless we are talking about big css files. Its helpful to compare the differences in CSS when they are next to each other in the same file and you can just add the extra CSS to the enabled version.
ben
February 21st, 2006 at 11:26 am
Yes, I used the approach in a big project.
But my advantage was that the team happened to know advanced enough js to handle things.
I am currently using the approach in a project of my own.
I suppose some level of expertise is required nonetheless.
However, if you clearly explain the folder and object hierarchy of the project to the (non-js savy) design team, it will not be a big trouble anyway.
In any team where skillsets are not evenly distrubuted, a careful lead is a must anyway.
July 2nd, 2007 at 3:44 pm
Hi there
I’m having some difficuties styling anchor text in an element with the DOM, because my anchor styling uses a class instead (the number of anchors is unlimited in this element) Basically, I am able to change dynamically the color of my text, because it has a single ID, but not to several anchors within this element (works with the first link only, but not the following. Ideally, I would like to have something that works like a document.getElementByClassName.
Possiblle
document.getElementByID(‘thetext’).color =’#'+ document.getElementByID(‘i(‘textHEXcolor’).innerHTML;
July 2nd, 2007 at 4:45 pm
in my last post, it shoudl read instead
document.getElementByID(‘thetext’).color =’#’+ document.getElementByID(‘textHEXcolor’).innerHTML;