Most users have JavaScript enabled these days but there may be a small fraction of your website visitors that have JavaScript turned off. In these circumstances, it’s generally accepted good practice to make sure that your website content is available to those users without JavaScript.
I came across this problem recently when I decided to implement a tabbed content area on the portfolio page of the restyled CVW Web Design site. The jQuery tabs effect works really well in this case because it allows a large amount of content to be effectively filtered and the page’s usability increases as a result.
The tabbed content areas are ‘hidden’ with CSS and jQuery reveals each tab with a nice animated effect. However, my initial testing of the page with JavaScript turned off was not so good because only the first tab’s content is shown. The other tabbed content areas were not visible.
Therefore, I need the CSS selector/rule that ‘hides’ the content to apply only for users with JavaScript enabled. For users without JavaScript, the CSS hide effect should not be apparent and they will be able to see all the tabbed content on the page as a result.
After some Googling, I found this article by Karl Swedberg which showed one way of doing this by dynamically adding a “js” class to your <html> tag using jQuery. Like this:
<script type="text/javascript">
$('html').addClass('js');
....
</script>
Then, use a CSS declaration similar to this in your style sheet. This positions the content off the page but display: none might also be used in other cases.
.js .myclass {
position: absolute;
top: -9999px;
left: -9999px;
}
The .js .myclass selector is applicable for JavaScript enabled users. However, it will not be applied to users without JavaScript and they will simply see all the tabbed content areas but without the jQuery tabs/animation.
There’s at least one other way of doing this but I thought this method was a good one.
Update: Gary Jones pointed out on Twitter that adding a class to the <html> tag is, strictly speaking, only valid in HTML5 (although browsers do support it with HTML4 or XHTML). He also suggested using a .no-js class on <body>, and getting the script to remove it and adding .js instead. This means that you can style explicitly for no JavaScript. That’s a smart idea.
Info: My portfolio page is a modified version of organic tabs. It’s a real nice example.
Stunning CSS3: A Project-Based Guide: Use this book to work through a series of practical yet cutting-edge projects. Each chapter walks you through standalone exercises that you can integrate into projects you're working on, or use as inspiration.
» Buy from Amazon.com · Amazon.co.uk
ThemeForest sell a range of site templates from some fantastic designers. They also sell some great WordPress themes!
Online invoicing made easy with CurdBee!
» Sign up
Join SugarSync for online backup. Sync your files between Mac, PC and mobile phone. Get 5 GB FREE and up to 10 GB bonus space!
HTML5 and CSS3 for the Real World will show you how to create websites using these new methods.
This easy-to-follow guide covers everything you need to know to get started. You’ll master the new semantic markup available in HTML5, as well as how to use CSS3 without sacrificing clean markup or resorting to complex workarounds. Buy the Book! · FREE Chapters
Prodyot 20 February 2012, 00:46 1
Thanks for the article and the referred information.
This is a great override.
Thanks again.