GreatWebGuy Self-proclaimed greatness is a hard thing to prove.

1May/091

Writing JavaScript specific CSS

I wrote an article recently recommending Using jQuery to write JavaScript specific CSS, I've since change my mind on this approach. The approach I've moved to is similar, but doesn't rely on a framework and allows more flexibility like moving all of my JavaScript includes to the bottom of the page for better performance.

In your html template or page add a nojs class to your html node and a script tag in the head section below your title changing the className to js:

<html class="nojs">
<head>
<title>My Title</title>
<script>document.documentElement.className = "js";</script>

Now you can write CSS like this:

.nojs a.myclass {color:red}
.js a.myclass {color:black}
a.myclass2 {color:blue}

In this very simple example all of your non-javascript users would see links with a class of myclass as red and all javascript enabled users would see the links with the color black. Both javascript and non-javascript users would see the links with the class myclass2 as blue.

This very simple example opens hundreds of possibilities for the way you style your site. You could actually display and hide content based on whether javascript is enabled or not, including buttons and links or any element on the page. You could include text for Search Engines and Screen Readers that your regular users might not need to see because the nature of the content could be for SEO or descriptive purposes for non-javascript users. You could also use it to enhance your suckerfish CSS menus with a hoverIntent when javascript is enabled making your menus a little more user friendly.

This is an improvement on the previous method I used because it doesn't depend on a JavaScript framework, it allows you to put off loading all of your JavaScript until the bottom of the page, improving performance, and allows you to utilize CSS to hide, disable, or fade in elements of the page that you don't want to be displayed until the document has been loaded and is ready to perform all that RIA functionality you've added to your app.

Tagged as: , 1 Comment
22Apr/0920

Enable JavaScript specific CSS with one line of jQuery

jQuery is by far my favorite JavaScript framework in terms of simplicity and just pure DOM power. I picked up a little trick at a conference quite a few months back and improved upon it and thought it could benefit anyone that believes in the mantra of progressive enhancement. Here's the line of code that will change the way you develop your applications.

$("html").removeClass("nojs").addClass("js");

Obviously jQuery is a requirement here. The other thing you'll need to do is add a class of nojs to the html node of all of your documents/templates.

<html class="nojs">

Now you're ready to change the way you write your CSS. I'm aware that Yahoo/YSlow recommend that you stick your JavaScript at the bottom of the page, but this little trick won't work as well unless jQuery and the snippet above are in the head of the html document. Now what this buys you is the ability to style your document with separate elements for javascript enabled users, non-javascript users and common elements that both will need. You'll be able to hide/show substitute elements in the page that are dynamic in nature so that you can successfully implement a site using progressive enhancement, meaning that users with javascript disabled will still be able to use your site but without all the bells and whistles. This also prevents the flicker that you see on page load when you modify elements via DOM manipulation using JavaScript.

Now you can write CSS like this:

.nojs a.myclass {color:red}
.js a.myclass {color:black}
a.myclass2 {color:blue}

In this very simple example all of your non-javascript users would see links with a class of myclass as red and all javascript enabled users would see the links with the color black. Both javascript and non-javascript users would see the links with the class myclass2 as blue.

This very simple example opens hundreds of possibilities for the way you style your site. You could actually display and hide content based on whether javascript is enabled or not, including buttons and links or any element on the page. You could include text for Search Engines and Screen Readers that your regular users might not need to see because the nature of the content could be for SEO or descriptive purposes for non-javascript users. You could also use it to enhance your suckerfish CSS menus with a hoverIntent when javascript is enabled making your menus a little more user friendly.

I realize this technique could be implemented with just about any JavaScript framework, jQuery just happens to be my framework of choice and this technique has definitely given us much capability to target javascript/non-javascript users and unobtrusively progressively enhance our website.

Please see my latest article on implementing this without a JavaScript framework.

Tagged as: , , 20 Comments
18Mar/072

15 Great Resources for Up and Coming CSS Designers

Working at a corporation with a bunch of stuffy Java Developers it seems like there's a resistance against using purely CSS for layout, decoration, and design. These Java Developers love their tables and don't want to let them go. Recently we hired two CSS designers, all they think do and breath is CSS, which was a big step in the right direction by our management. Since the Java Developers are now being mandated to embrace CSS, thanks to me sharing CSS Zen Garden with someone in upper management, maybe it's time to study up. The following list of sites come highly recommended from our top CSS designer, enjoy!

Tagged as: Continue reading