How to detect and apply window size to a div using jQuery

In some case we need to apply the window width and height to a div or something like that. Here is a solution for that.

You can detect and insert the screen width to a variable using the following jQuery code,

var screen_width = $(window).width();

Also you can detect and insert the screen height to a variable using the following jQuery code,

var screen_height = $(window).height();

If you want to apply the width or to a div, you can use the following code,

//do something with the width or height value here!
    $("div").width(screen_width);
    $("div").height(screen_height);

If you want to customize your value (example: you want to reduce 10pixels from the screen width), you can use the following code,

var screen_width = $(window).width();
var screen_height = $(window).height();
var reduced_width = parseInt(current_width)- parseInt(10);
var reduced_height = parseInt(current_height)- parseInt(10);
     
//do something with the width or height value here!
    $("div").width(reduced_width);
    $("div").height(reduced_height);

If you liked the script, leave a comment here. 🙂
Enjoy…