Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating width of scrollbar and using result in calc() css

I want to calculate the width of the scrollbar so that I use the result in a CSS calc() declaration.

At the moment, I assume that the width of the scrollbar is always 17px, like this:

body { 
    width:calc(100vw - 17px); 
}
.container { 
    max-width:calc(100vw - 17px); 
}

The problem with this is when you choose a different browser zoom %, the width of the scrollbar changes. So I want to use the result of the calculation to do something along these lines:

body { 
    width:calc(100vw - CALCULATED SCROLL-BAR WIDTH); 
}
.container { 
    max-width:calc(100vw - CALCULATED SCROLL-BAR WIDTH); 
}

EDIT: I've now solved the problem with the help of this question

The JavaScript used to calculate the scrollbar width (though, I have found you require an interval to get it to autoupdate):

function getScrollbarWidth() {
  var outer = document.createElement("div");
  outer.style.visibility = "hidden";
  outer.style.width = "100px";
  outer.style.msOverflowStyle = "scrollbar"; // needed for WinJS apps

  document.body.appendChild(outer);

  var widthNoScroll = outer.offsetWidth;
  // force scrollbars
  outer.style.overflow = "scroll";

  // add innerdiv
  var inner = document.createElement("div");
  inner.style.width = "100%";
  outer.appendChild(inner);        

  var widthWithScroll = inner.offsetWidth;

  // remove divs
  outer.parentNode.removeChild(outer);

  return widthNoScroll - widthWithScroll;
}

My code (which is used to embed the result of the function into a CSS calc() declaration).

$('body').css({ 
  'width':'calc(100vw - ' + getScrollbarWidth() + 'px)' 
});

$('.container').css({ 
  'max-width':'calc(100vw - ' + getScrollbarWidth() + 'px)' 
});
like image 637
Bailey280899 Avatar asked Sep 08 '16 13:09

Bailey280899


People also ask

How do I get the scrollbar width in CSS?

To get the width of the scrollbar, you use the offsetWidth and clientWidth of the Element : The offsetWidth returns the width of the Element in pixels including the scrollbar. The clientWidth returns the with of the Element in pixels without the scrollbar.

How does the CALC () function work on values in CSS?

CSS calc() is a function used for simple calculations to determine CSS property values right in CSS. The calc() function allows mathematical expressions with addition (+), subtraction (-), multiplication (*), and division (/) to be used as component values.

Does view width include scrollbar?

Many assume that width: 100vw is the same as width: 100% . This is true on a page that doesn't scroll vertically, but what you might not realize is that the viewport actually includes the scrollbars, too.


2 Answers

Actually, you can get the scrollbar width just with css and custom properties (and completely without javascript):

body {
    --scrollbar-width: calc(100vw - 100%);
}

Then you can use this variable in a child element like this:

.container { 
    max-width: calc(100vw - var(--scrollbar-width)); 
}

This is because 100vw is always the inner width of the view, but the 100% of the body does not include the scrollbar.

like image 101
jonas_jonas Avatar answered Sep 28 '22 04:09

jonas_jonas


Expanding jonas_jonas's answer, it can work but if .container must have the same width as the body.

If that's not the case, even so you can make it work with vanilla JS, defining a CSS property like this

document.body.style.setProperty(
    "--scrollbar-width",
    `${window.innerWidth - document.body.clientWidth}px`
);

And then you can use it in CSS

.container { 
    max-width: calc(100vw - var(--scrollbar-width)); 
}
like image 22
Juan Barba Avatar answered Sep 28 '22 03:09

Juan Barba