I am using Twitter Bootstrap on a project. As well as the default bootstrap styles I have also added some of my own
//My styles
@media (max-width: 767px)
{
//CSS here
}
I am also using jQuery to change the order of certain elements on the page when the width of the viewport is less that 767px.
$(document).load($(window).bind("resize", checkPosition));
function checkPosition()
{
if($(window).width() < 767)
{
$("#body-container .main-content").remove().insertBefore($("#body-container .left-sidebar"));
} else {
$("#body-container .main-content").remove().insertAfter($("#body-container .left-sidebar"));
}
}
The problem I am having is that the width calculated by $(window).width()
and the width calculated by the CSS doesn't seem to be the same. When $(window).width()
returns 767 the css calculates it the viewport width as 751 so there seems to be a 16px different.
Does anyone know what is causing this and how I could solve the problem? People have suggested that the width of the scrollbar isn't being taken into considering and using $(window).innerWidth() < 751
is the way to go. However ideally I want to find a solution that calculates the width of the scrollbar and that is consistent with my media query (e.g where both conditions are checking against the value 767). Because surely not all browsers will have a scrollbar width of 16px?
yes, that's due to scrollbar. Right answer source: enter link description here
Try this
For More Reference click here
I was facing the same problem recently - also with Bootstrap 3.
Neither $.width() nor $.innerWidth() will work for you.
The best solution I came up with - and is specifically tailored to BS3 -
is to check the width of a
.container
element.As you probably know how the
.container
element works,it's the only element that will give you the current width set by BS css rules.
So it goes something like