How to get the width of device screen in JavaScript ?
Given an HTML document which is running on a device. The task is to find the width of the working screen device using JavaScript.
Example 1: This example uses window.innerWidth to get the width of device screen. The innerWidth property is used to return the width of the device.
<!DOCTYPE HTML> <html> <head> <title> How to get the device screen width in JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <!-- Script to display the device screen width --> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "Click on the button to get the" + " width of the device's screen"; function GFG_Fun() { var width = window.innerWidth; el_down.innerHTML = width + " pixels"; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example uses document.documentElement.clientWidth method to get the width of device screen.
<!DOCTYPE HTML> <html> <head> <title> How to get the device screen width in JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <!-- Script to display the device screen width --> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "Click on the button to get the" + " width of the device's screen"; function GFG_Fun() { el_down.innerHTML = document.documentElement.clientWidth + " pixels"; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to get the height of device screen in JavaScript ?
- How to detect touch screen device using JavaScript?
- How to detect the device is an Android device using JavaScript ?
- HTML | Screen width Property
- Detect a device is iOS or not using JavaScript
- JavaScript BOM | Window Screen
- How to get the width of scroll bar using JavaScript ?
- How to find the width of a div using vanilla JavaScript?
- Calculate the width of the text in JavaScript
- How to Dim entire screen except a fixed area using JavaScript ?
- How to get the image size (height & width) using JavaScript ?
- What is the difference between “screen” and “only screen” in media queries?
- How to detect a mobile device in jQuery?
- How to detect the user's device using jQuery ?
- CSS | max-width Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



