Although it's 2023, many developers still rely on the jQuery library for its ease of use in JavaScript. If you need to obtain the screen width and height of your browser window using jQuery, look no further than the following code. As you resize your browser window, the width and height values of the frame are automatically displayed.
There could be various reasons one may want to get the width and height of a web browser,
- In order to create a responsive website that adapts to the different screen sizes (mobile/laptops), you need to know the screen width and height so that you can adjust the layout accordingly.
- Knowing the screen size of the user could help you customize the user experience for different devices. For example, you might want to display a mobile-friendly version of your site on small screens or show a larger version of images on larger screens.
- This may also help to track the screen size of your visitors to help you analyze their behavior and preferences. For example, you might find that visitors using smaller screens tend to click on certain areas of your site more frequently than those using larger screens.
- If you display ads on your website, it could be so that you need to know specific screen sizes for displaying ads, so by knowing the screen size, you can ensure that the appropriate ad is displayed to the user.
This code can be quite handy when testing the 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 demo that as we resize the browser window, we get the correct width and height size in pixels.
Frequently Asked Questions (FAQ):
let screenWidth = $(window).width();
let screenHeight = $(window).height();
if ($(window).width() < 768) {
// Adjust content for smaller screens
} else {
// Content for larger screens
}
$(window).resize(function() {
// Your code to execute when the screen size changes
});
Bootstrap or Foundation for responsive design.
if ($(window).width() < 768) {
// your code when width less than 768px
}
let newImageWidth = $(window).width() * 0.8;
$('#myImage').css('width', newImageWidth + 'px');
if ($(window).width() < 600) {
$('.mobile-only').show();
$('.desktop-only').hide();
} else {
$('.mobile-only').hide();
$('.desktop-only').show();
}
if ($(window).width() < 768) {
$.getScript('mobile.js');
} else {
$.getScript('desktop.js');
}
$('meta[name="viewport"]').attr('content', 'width=device-width, initial-scale=1.0');
Example: Toggle navigation on small screens
$('.menu-icon').click(function() {
$('nav').slideToggle();
});
if ($(window).width() < 1400) {
$('body').css('background-image', 'url(background_large.jpg)');
} else {
$('body').css('background-image', 'url(background_large.jpg)');
}
$(window).resize(function() {
// your code
});
if ($(window).width() < 600) {
$('.responsive-text').css('font-size', '16px');
} else {
$('.responsive-text').css('font-size', '20px');
}
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- jQuery: Check if an element exists or not
- [jQuery] Uncaught ReferenceError: $ is not defined at index.html:5
- Make div element draggable using jQuery
- jQuery : Move to top of the page
- Dynamically Obtaining Browser Screen Width and Height with jQuery [Updated 2023]
- How to check if an element is hidden using jQuery code?
- Redirect page using jQuery
- Disable jQuery button after being click
- Convert Java Array to ArrayList Code Example - Java
- How to connect to SSH port other than default 22 - Linux
- Get cURL command from Chrome Inspect Network HTTP URL - cURL
- Stop Automatic Opening of Music App on Mac when I connect Earphones - MacOS
- Error when deleting SharePoint Online folder or file - SharePoint
- How to Upgrade Pandas Package - PIP
- How to change macOS Safari default language - MacOS
- BSNL Broadband upgrades speed to minimum 2MBps for all users 512Kbps 1Mbps - HowTos