The no-js class is commonly added to the <html> element to provide different styles or functionality depending on whether JavaScript is available in the browser.
- It allows CSS to target pages when JavaScript is disabled.
- JavaScript can replace the no-js class with a js class when scripting is enabled.
- It is commonly used for progressive enhancement and JavaScript-dependent features.
Approach: Using the no-js Class with JavaScript Detection
In this approach, the no-js class is initially added to the <html> element. When JavaScript runs, it replaces no-js with js, allowing CSS to apply different styles based on JavaScript availability.
Example:
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<script>
document.documentElement.className =
document.documentElement.className
.replace(/\bno-js\b/g, "") + " js";
</script>
<title>no-js Class Example</title>
</head>
<body>
<h1>GeeksforGeeks</h1>
</body>
</html>
Output:
