matt
matt

Reputation: 44403

Body styling for a noscript tag?

If no JavaScript is enabled, I want my body to have overflow:hidden (no scrollbars).

How can I solve this? Is it possible to use a noscript tag inside of <head> and set specific styles inside of it?

Upvotes: 2

Views: 2195

Answers (4)

Gideon
Gideon

Reputation: 18501

This is probably a super overkill, but just for general knowledge:

Modernizr uses a mechanism where it suggest you put the no-js class on your html tag. If modernizr runs, it removes the no-js class. Using modernizr would be an overkill, but you could do this yourself. In your css you would have:

html.no-js body {
    overflow:hidden;
}

Upvotes: 1

fcalderan
fcalderan

Reputation:

It's not valid using the <noscript> tag inside the head section (it is allowed only inside <body>), so you could use this workaround instead (an HTML5 example)

 <!doctype html>
 <html lang="en">
     <head>
         <script>document.documentElement.className = 'js';</script>
         <style>
             body { overflow: hidden; }
             html.js body { overflow: auto; }
         </style>
     </head>

Upvotes: 2

JJJ
JJJ

Reputation: 33153

If you mean the <body> of the HTML document, I'd build the HTML as it should be for the users with JS disabled. For example in the CSS file have body { overflow:hidden } and then use JavaScript to set the page as it should be for users with JavaScript enabled (i.e. remove the overflow:hidden with JavaScript).

Upvotes: 5

Ross
Ross

Reputation: 17987

I'm pretty sure

<noscript>
    <style type="text/css">
        body {
            overflow:hidden;
        }
    </style>
</noscript>

in your <head> would do the job.

But as stated in the other post, it may be better to use CSS for this purpose, and enable overflow for browsers with JavaScript.

Upvotes: 3

Related Questions