Get Device Screen Width and Height using javaScript


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.

screen dimentions output
screen dimentions output
getScreenDimentions.html
<!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.

Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.