Get Browser Screen Width and Height dynamically using jQuery


In order to get screen width and height of your browser window using jQuery check out the below code. As you resize your browser window the values of the width and height of the frame are displayed.

This code can be quite handy when your testing your responsive design of your website. You can view this page on various devices to get the height and width.

Code Example:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="../js/jquery-1.10.2.min.js" >
</script>
<style>
#display {
        width: auto;
        height:22px;
        background-color: #ccc;
        color: #222;
        margin-left: 5px;
        font-size:20px;
        text-align: center;
    }
</style>
</head>
<body>
<script>
$("#display").html("<b>Width</b> : " + 
       $(window).width() + ",<b>Height : </b>" +
       $(window).height());
$(window).resize(function() {
        $("#display\").html("<b>Width</b> : " +
        $(window).width() + ",<b>Height : </b>" +
        $(window).height());
});
    </script>
<div id="display"></div>
</body>
</html>

You can see the below gif file - you can see that as we resize the browser window, we get the correct width and height size in pixels.

Get Browser width and height using jQuery
Get Browser width and height using jQuery

⛏️ Note: There is an easier way to do this using the Chrome browser Inspect option, Right Click on any web page and click on Inspect, click on "Toggle Device Toolbar" and select the responsive option, drag the width and height to know how your page renders and what the page dimensions are.



Have Questions? Post them here!





Recent Posts:
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, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.