Use Different CSS When Javascript Is Enabled

Here is a snippet that allows you to have different CSS for when Javascript is turned on or off. You may need different CSS so you can deal with visitors who have Javascript turned off, you may want to display things differently. To change how you display different CSS for when Javascript is on you can add a CSS class to the html tag so you can filter the CSS down to the child tags.


Here is a raw Javascript way to add a CSS class if Javascript is turned on.

document.documentElement.className = "js";

This will change the HTML tag to

<html class="js">


If you use jQuery then you can just use the following which will do the same thing.


Change The CSS

Now you have a CSS class on the HTML tag you can change the child elements.

.slides { display:block; }
.js .slides { display:none; } 

This will change the slides element to hidden if Javascript is turned on which means you can add the functionality of the slideshow with the Javascript.


Reliable uptime monitoring and instant alerts for any website downtime.

  • Uptime Monitoring
  • Performance Monitoring
  • SSL Certificate Alerts
  • Domain Monitoring
  • DNS Checker
  • XML Sitemap Monitoring