• 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:

  • Hexadecimal Color Wheel
  • Get and Set Scroll Position of an Element
  • Find the distance from a given element to the top of the document
  • Check if a node is in the viewport

Primary Sidebar

JSON Compare

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
Advertisement