Why is the window.width smaller than the viewport width set in media queries

This is what worked for me:
CSS media queries and JavaScript window width do not match.

Instead of using $(window).width(); which includes scrollbars get the inner width like this:

function viewport() {
    var e = window, a="inner";
    if (!('innerWidth' in window )) {
        a="client";
        e = document.documentElement || document.body;
    }
    return { width : e[ a+'Width' ] , height : e[ a+'Height' ] };
}

var vpWidth = viewport().width; // This should match your media query

Leave a Comment