Javascript Reference - Screen width Property








The width property returns the total width of the user's screen in pixels.

Browser Support

width Yes Yes Yes Yes Yes

Syntax

screen.width

Return Value

A Number type value representing the total width of the user's screen in pixels.





More Examples

The following code shows how to all screen properties in one example


<!DOCTYPE html>
<html>
<body>
<div id="demo"></div>
<script>
var txt;
txt = "<p>Total width/height: " + screen.width + "*" + screen.height + "</p>";
txt+= "<p>Available width/height: " + screen.availWidth + "*" + screen.availHeight + "</p>";
txt+= "<p>Color depth: " + screen.colorDepth + "</p>";
txt+= "<p>Color resolution: " + screen.pixelDepth + "</p>";
<!--from ww w.  j  a v  a 2  s . co m-->
document.getElementById("demo").innerHTML = txt;
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the total width of your screen.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!-- w ww  .  ja  v a2s  .  c  om-->
<p id="demo"></p>

<script>
function myFunction() {
    var x = "Total Width: " + screen.width + "px";
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: