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.
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>