Using CSS scripting: none for Graceful Degradation

The CSS scripting media feature allows developers to query whether scripting languages, primarily JavaScript, are supported and active on the current document. In graceful degradation and progressive enhancement strategies, targeting @media (scripting: none) enables websites to adapt their layout and interface elements natively when JavaScript is disabled, unavailable, or blocked, replacing older class-based fallback techniques.

Understanding the scripting Media Feature

The scripting media feature is defined in the CSS Media Queries Level 5 specification. It accepts three distinct values:

  • none: JavaScript is completely disabled, unsupported, or blocked on the user agent.
  • initial-only: Scripting is enabled only during the initial document load, but not afterwards (commonly seen in server-side pre-rendering or print environments).
  • enabled: JavaScript is supported, enabled, and available for dynamic manipulation during the page lifecycle.

The Traditional Approach vs. scripting: none

Historically, web developers handled JavaScript degradation by applying a .no-js class to the <html> or <body> element directly in the HTML. A small, inline JavaScript snippet would then execute immediately to remove or replace that class with .js.

While effective, this method creates a brief flash of unstyled content, relies on an inline script, and complicates CSS architecture. The @media (scripting: none) media query eliminates these issues by providing a native browser-level check that evaluates without any inline DOM modifications.

Practical Applications in Graceful Degradation

1. Hiding Script-Dependent Elements

Features designed exclusively for dynamic interactions—such as interactive filter bars, print buttons, or rich-text widgets—are unusable without JavaScript. Using scripting: none, you can hide these elements entirely to prevent user frustration.

@media (scripting: none) {
  .js-only-control {
    display: none;
  }
}

2. Displaying Native HTML Fallbacks

When complex widgets like dynamic accordions, modal dialogs, or carousel sliders fail without JavaScript, you can display accessible, static alternatives.

/* Default state for accordion panels */
.accordion-content {
  display: none;
}

/* Fallback: expand all panels if JavaScript is off */
@media (scripting: none) {
  .accordion-content {
    display: block;
  }
}

3. Warning Messages and Static Forms

Certain critical workflows require notice or alternative submission paths. Instead of relying on <noscript> tags inside the HTML body, CSS can style and reveal dedicated fallback notifications.

.no-script-banner {
  display: none;
}

@media (scripting: none) {
  .no-script-banner {
    display: block;
    background-color: #fffae6;
    padding: 1rem;
    border-bottom: 1px solid #e0c860;
  }
}

Advantages for Modern Development

Using @media (scripting: none) offers several benefits:

  • Zero Layout Shift: Styles are computed immediately by the CSS parser without waiting for script execution.
  • Cleaner HTML: Eliminates manual class additions and <noscript> elements.
  • Encapsulation: Keeps presentation logic directly within the styling layer where it belongs.