I want to define .exampleclass img {height:250px}
if javascript is not enabled. Is their anyway to undo this in javascript / jquery?

- 55,053
- 85
- 237
- 424
-
https://developer.mozilla.org/en-US/docs/Web/CSS/@media/scripting – mplungjan Jun 03 '20 at 11:08
8 Answers
You could probably use HTML's noscript
tag.
<noscript>
<style type="text/css">
.exampleclass img {height:250px}
</style>
</noscript>
EDIT: I'm going to actually side with Stephen's answer as being the best. While the above might work, it might not be valid/following best practices.
Stephen answered:
put a "no-js" class on your body element, and then remove it with JS at load, and use .no-js in the selector for your CSS
The class could also be placed on your html
element and then all styling that should appear when JavaScript is not available can be prefixed with html.no-js
before what the selector would be otherwise. This is what HTML5 Boiler Plate does, for example.

- 2,822
- 2
- 19
- 24
-
I believe you can place it within the head tags. It would probably be best to define the style in the head. Edit: If it is overriding the class definition, make sure that this is after the other class is defined. – Kyle Jan 12 '11 at 06:00
-
put a "no-js" class on your body element, and then remove it with JS at load, and use .no-js in the selector for your CSS

- 18,597
- 4
- 32
- 33
One day we may have this
Not currently active in Chrome 105 OSX
https://developer.mozilla.org/en-US/docs/Web/CSS/@media/scripting
p {
color: lightgray;
}
@media (scripting: none) {
.script-none {
color: red;
}
}
@media (scripting: initial-only) {
.script-initial-only {
color: red;
}
}
@media (scripting: enabled) {
.script-enabled {
color: red;
}
}
<p class="script-none">You do not have scripting available. :-(</p>
<p class="script-initial-only">Your scripting is only enabled during the initial page load. Weird.</p>
<p class="script-enabled">You have scripting enabled! :-)</p>

- 169,008
- 28
- 173
- 236
I use something like this...
JavaScript
document.body.className += ' javascript';
jQuery
$('body').addClass('javascript');
CSS
.exampleclass img {height: 250px}
.javascript .exampleclass img {height: 500px}

- 479,566
- 201
- 878
- 984
-
1[According to MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript), `style` within `noscript` within `head` is valid. – Josh Kelley Sep 05 '14 at 21:11
-
@JoshKelley Good to know, I don't know where I got that piece of information from. I'll remove it. – alex Sep 07 '14 at 23:58
The most elegant way is to have in your CSS something like:
.exampleclass img {height:250px}
.js .exampleclass img {height:auto;}
and in your JS:
var dd = document.documentElement;
dd.className += ((dd.className == "") ? "js" : " js");
so that the second CSS rule will override the first one in case JS is enabled

- 17,949
- 7
- 35
- 40
In the future (if and when browsers start supporting it), it will be possible to use the scripting
CSS media feature:
@media (scripting: none) {
// Styles to apply if JS is disabled
}

- 6,173
- 1
- 17
- 37