To get the height and width of the Display Screen we must use screen.height and screen.width javascript properties respectively. The screen property is available in the Window Object, we don't need to prefix it with the screen though.

<!DOCTYPE html>
<html>
<title>Get Device width and Height using Javscript</title>
<script type="text/javascript">
function getScreenDimentions() {
document.getElementById("dimenstions").innerHTML =
"Display Width : " + screen.width +"<br/> Display Height : " + screen.height;
}
</script>
<h2>You Monitor display pixels</h2>
<body onload="getScreenDimentions()">
<div id="dimenstions"></div>
</body>
</html>
To demonstrate we create an javaScript function in the head section getScreenDimentions() and set the screen width and height to the HTML div element with class name dimensions on body load.
Note that the screen property returns the dimensions in the unit pixel.
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!