css - Centered HTML content gets an extra indent when it overflows off the page - Stack Overflow

I have an HTMLwhich is centered horizontally and contains a bunch of <p> tags. When the centere

I have an HTML which is centered horizontally and contains a bunch of <p> tags. When the centered div height exceeds the height of the page, it indents the div. How can I stop this from happening?

.container {
  display: flex;
  justify-content: center;
}

.center {
  width: 600px;
  max-width: 600px;
  border-left: 1px;
  border-right: 1px;
  border-top: 1px;
  border-bottom: 1px;
  border-color: rgb(0, 0, 0);
  border-style: solid;
}
<div class="container">
  <div class="center">
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
  </div>
</div>

I have an HTML which is centered horizontally and contains a bunch of <p> tags. When the centered div height exceeds the height of the page, it indents the div. How can I stop this from happening?

.container {
  display: flex;
  justify-content: center;
}

.center {
  width: 600px;
  max-width: 600px;
  border-left: 1px;
  border-right: 1px;
  border-top: 1px;
  border-bottom: 1px;
  border-color: rgb(0, 0, 0);
  border-style: solid;
}
<div class="container">
  <div class="center">
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
  </div>
</div>

I have tried setting overflow to 'auto' in the center div. This has not worked.

Share Improve this question edited Mar 5 at 23:20 Ori Drori 194k32 gold badges238 silver badges229 bronze badges asked Mar 5 at 23:15 Max FogdallMax Fogdall 112 bronze badges 1
  • 1 You mean, the browser scrollbar comes showing up causing the actual viewport to be a little bit more narrow and thus forcing the center of the page to be a little more left than if the scrollbar wasn't visible? – prettyInPink Commented Mar 5 at 23:29
Add a comment  | 

1 Answer 1

Reset to default 1

To prevent the centered div from jumping when the scrollbar appears due to height changes, you can use the new scrollbar-gutter property. Use the property on the scroll container (<html> in this case):

html {
  scrollbar-gutter: stable both-edges;
}

Demo - run the demo, click "Full page" and reduce the height using the dev tools:

html {
  scrollbar-gutter: stable both-edges;
}

.container {
  display: flex;
  justify-content: center;
}

.center {
  width: 600px;
  max-width: 600px;
  border-left: 1px;
  border-right: 1px;
  border-top: 1px;
  border-bottom: 1px;
  border-color: rgb(0, 0, 0);
  border-style: solid;
}
<div class="container">
  <div class="center">
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
  </div>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745002962a4605609.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信