Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a different CSS stylesheet if a user has JavaScript disabled in their browser?

I am developing a website for someone, and the CSS styles I use require JavaScript (for the buttons that are used for a dropdown navigation bar on small screens). How can I use one stylesheet if the user has JavaScript enabled or use another one if JavaScript is disabled.

like image 356
scarecrow850 Avatar asked Feb 10 '23 01:02

scarecrow850


2 Answers

Two ways to do it:

  1. Append the JavaScript-only stylesheets with JavaScript:

    function appendStyle(url) {
      var sheet = document.createElement("link");
      sheet.setAttribute("href", url);
      sheet.setAttribute("rel", "stylesheet");
      sheet.setAttribute("type", "text/css");
      document.head.appendChild(sheet);
    }
    
  2. If you don't mind loading the CSS for the JS and you just want to override your site's default appearance you can use a noscript tag instead:

    <noscript>
      <link href="your/no-js/stylesheet.here.css" type="text/css" rel="stylesheet">
    </noscript>
    
like image 105
Sean Vieira Avatar answered Feb 11 '23 14:02

Sean Vieira


You can use like this...

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <link rel="stylesheet" href="general css file" />
        <noscript>
          <link rel="stylesheet" href="CSS file for JS dissable" />
        </noscript>

This works for me

like image 36
Marmik Bhatt Avatar answered Feb 11 '23 15:02

Marmik Bhatt