Goal: create scrolling content overlaid by an equally wide navbar. Example html could look like:
<div class="scrollable">
<div class="content"></div>
</div>
<div class="navbar"></div>
Criteria:
.content
fills a percent width of.scrollable
, and is horizontally centered.scrollable
should have scrollbars.navbar
is exactly as wide as.content
, centered exactly atop.content
- Scrollbar width should be treated as unknown
- Avoid javascript running via scroll,
requestAnimationFrame
,setInterval
, etc. Ideally no js.
The most intuitive approach fails since the scrollbar causes a discrepancy in dimensions:
.container {
position: absolute;
width: 80%; height: 80%; left: 10%; top: 10%;
box-shadow: 0 0 10px 1px rgba(0, 0, 0, 0.3);
}
.scrollable {
position: absolute;
width: 100%; height: 100%; left: 0; top: 0;
overflow: hidden visible;
}
.scrollable > .content {
width: 80%; margin: 60px auto;
background-color: rgba(0, 0, 0, 0.2); text-align: center;
}
.navbar {
position: absolute;
left: 10%; bottom: 0; width: 80%; height: 50px;
background-color: rgba(255, 0, 0, 0.3); color: rgba(255, 255, 255, 1);
}
<div class="container">
<div class="scrollable">
<div class="content">abcd<br/><br/><br/>abcd<br/><br/><br/>abcd<br/><br/><br/>abcd<br/><br/><br/>abcd<br/><br/><br/>abcd<br/><br/><br/>abcd<br/><br/><br/>abcd<br/><br/><br/>abcd<br/><br/><br/>abcd<br/><br/><br/></div>
</div>
<div class="navbar">I am navbar</div>
</div>
The issue here is that .content
takes up a percentage width of .scrollable
's width minus its scrollbar width, whereas .navbar
is set to a percentage width of .scrollable
including its scrollbar width.
What is the best way to get around this discrepancy?