Quite a hefty snippet but this is the basic layout for a site I'm working on and the issue is actually really simple - I'm using a width container, same width on the navbar and the main elements - everything aligns great except when I use overflow-y: auto
to create a scroll bar on the main
element.
Because of the scrollbar, the centre of the main body is now a few pixels short.
Is there a way to stop the browser from auto-adjusting the center of the view when a scroll bar is present?
I haven't yet tested on other browsers but I'm seeing this on firefox, I'm just wondering if there's a simple fix for all browsers or if it's just something we have to live with.
Here it is with the overflow added:
And here it is without:
You can see the centered white portion in the navbar is misaligned with the body's centered light grey portion.
html {
height: 100%;
font-size: calc(1em + 1vw);
}
body {
height: 100%;
overflow: hidden;
margin: 0;
padding: 0;
font-family: Arial, Helvetica, sans-serif;
display: flex;
flex-direction: column;
background-color: #ccc;
box-sizing: border-box;
}
header {
display: flex;
justify-content: center;
box-shadow: rgba(0, 0, 0, 0.4) 0px 2px 2px;
z-index: 1000;
}
main {
display: flex;
overflow-y: auto;
justify-content: center;
flex: 1 0 0px;
}
.navTop-widthContainer {
max-width: 536px;
flex: 1 0 0px;
display: flex;
background-color: #fff;
}
.navTop {
display: inline-flex;
flex-direction: row;
flex: 1 0 auto;
flex-shrink: 0;
}
.navTop div {
display: flex;
}
.floatRight {
margin-left: auto;
order: 2;
}
.f-d {
display: flex;
flex-direction: column;
}
.f-d div {
display: flex;
justify-content: center;
}
.widthContainer {
max-width: 536px;
background-color: #eee;
border-left: solid 1px #ddd;
border-right: solid 1px #ddd;
color: #242424;
}
<body>
<header>
<div class="navTop-widthContainer">
<nav id="navTop" class="navTop" role="navigation">
<div>
<a href="/Services">Services</a>
<a href="/Customer">Shopping</a>
</div>
<div class="floatRight">
<a id="register" href="/Identity/Account/Register">Register</a>
<a id="login" href="/Identity/Account/Login">Login</a>
</div>
</nav>
</div>
</header>
<main>
<div class="widthContainer f-d">
<div>
<p>Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill
text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text
Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill
text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text Fill text
Fill text Fill text Fill text Fill text Fill text Fill text</p>
</div>
</div>
</main>
</body>