Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scrollbar on browser and div margin 0 auto jumping

Tags:

html

css

i am using div#wrapper margin: 0 auto to center the div, there is scroll bar on this page however when it transition to second page where there is no scroll bar, it appears as jumpy because there is no scroll bar i guess.

<body>
<div id="wrap">
<div id="wrapper">
....

#wrapper { width: 970px; margin: 0 auto; } 

what would be the best solution for this not to make it jumpy?

like image 566
cicakman Avatar asked Jun 15 '11 12:06

cicakman


People also ask

How do I stop the scroll bar from moving in CSS?

The easy fix is to use width: 100% instead. Percentages don't include the width of the scrollbar, so will automatically fit. If you can't do that, or you're setting the width on another element, add overflow-x: hidden or overflow: hidden to the surrounding element to prevent the scrollbar.

How do I make Div scroll automatically?

Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.


2 Answers

Set html width to be equal viewport and turn off horizontal scroll to avoid appearing of horizontal scrollbar when vertical one will expand html.

html {
  width: 100vw;
  overflow-x: hidden;
}
like image 136
Andrey Shaforostov Avatar answered Oct 02 '22 16:10

Andrey Shaforostov


I've run into this a few times, the best thing I've found is to force a Y scroll-bar on every page, even if it isn't needed using in your style sheet:

html { overflow-y: scroll; }

This will mean there is always a right scroll bar on the page, but it will be enabled/disabled as needed, and prevent the jump.

like image 42
Matt.C Avatar answered Oct 02 '22 18:10

Matt.C