Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS 100% width but avoid scrollbar

Tags:

css

This is probably a variation on something that has been solved dozens of times but CSS really makes me feel like a fool.

I am trying to build a widget that can be positioned and sized in a variety of ways. It's a pretty simple layout - fixed-height header, fixed-height footer, and a body that takes up the remaining space. The overall width and height varies. The content of the body needs to scroll. I have the overall container, header, footer, and body sizing ok.

But what I want is for the body to scroll when it needs to WITHOUT shrinking content to the left when the scrollbar appears. That is, I want the body to be as wide as it can MINUS the scrollbar that would be there iF it needed to scroll, so that when it DOES need to scroll there is no shrink. In effect, I want this:

| - - - unknown width - - -| +--------------------------+ | content                |*| | might                  |*| | scroll                 |*| +--------------------------+ 

I want the content that might scroll to be as wide as it can MINUS the potential scrollbar width (|*| region).

What I have now is something like this:

<div id="content" style="position: absolute; overflow-y: auto; width: 100%">     <div id="scrollContent" style="???">     </div> </div> 

I have tried margins, padding, even %-widths for the inner-most div and all 'do the shift' thing. I also need this to work in FF3, IE7/8 and (fantasy?) IE6.

like image 867
n8wrl Avatar asked Aug 22 '10 14:08

n8wrl


People also ask

Does 100vw 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.

Does width include scrollbar?

The window. innerWidth is the actual width of the window, including scrollbars, but not including the window border.

How do I reduce the scrollbar width in CSS?

scrollbar-width accepts the following values: auto is the default value and will render the standard scrollbars for the user agent. thin will tell the user agent to use thinner scrollbars, when applicable. none will hide the scrollbar completely, without affecting the element's scrollability.

What does width 100% do in CSS?

It seems like this should be one of the easiest things to understand in CSS. If you want a block-level element to fill any remaining space inside of its parent, then it's simple — just add width: 100% in your CSS declaration for that element, and your problem is solved.


1 Answers

Use overflow: scroll instead of overflow: auto - that'll force a scrollbar to always appear.

like image 108
casablanca Avatar answered Oct 05 '22 06:10

casablanca