Grow your CSS skills. Land your dream job.

Last updated on:

JavaScript Required Content with Fallback Content

JavaScript-required area is hidden with inline CSS, and then shown after it loads with a small snippet of JavaScript. Below that, noscript tags are used for fallback content. So only one or the other of these bits of content will be required depending on if JavaScript is enabled or not.

<!-- JavaScript-Required Content -->
<div id="photoslider" style="display:none">Javascript photoslider</div>
<script>document.getElementById('photoslider').style.display='';</script>
<!-- END JavaScript-Required Content -->

<!-- Fallback Content -->
<noscript><div id="photo">Alternative static photo as content when javascript is turned off</div></noscript>
<!-- END Fallback Content -->

Comments

  1. This code makes me happy! So simple, but so effective!

  2. stine
    Permalink to comment#

    Nice tip! thanks

  3. Chris,

    You and this site are always coming to my rescue! FYI – I just tried this with a WordPress slideshow. I added the ‘noscript’ tags with a standard image under the slideshow shortcode in the standard editing page and it worked like a charm.
    Thanks!

  4. Miles
    Permalink to comment#

    Hi Chris
    I was wanting to use a simple slideshow on a homepage but was concerned about the ‘no javascript available’ scenario. This simple piece of code leaves me with a great first choice image even if the slideshow doesn’t run.

    many thanks for this

Leave a Comment

Posting Code

  • Use Markdown, and it will escape the code for you, like `<div class="cool">`.
  • Use triple-backticks for blocks of code.
    ``` 
    <div>
      <h1>multi-line block of code</h1>
      <span>be cool yo.</span>
    </div>
    ```
  • Otherwise, escape your code, like <code>&lt;div class="cool"&gt;</code>. Markdown is just easier though.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".