• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Page Details / Viewport Width

Viewport Width

Viewport Width

There are two methods to get the viewport width: window.innerWidth and document.documentElement.clientWidth. The former is more accurate. The latter has better browser support.

To get the best of both worlds, try innerWidth first, and fallback to clientWidth if not supported.

var viewportWidth = window.innerWidth || document.documentElement.clientWidth;

Source

https://vanillajstoolkit.com/reference/viewport/viewport-width/

Page Details

Related Snippets:

  • Top Window
  • Check if a node is in the viewport
  • Reload on Resize
  • Get all URL parameters from a query string

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers