I am writing a very simple web-app, which should have a long scrollable list of items, with a fixed header at the top of the viewport offering functionality to filter the items. The header should always be visible, and crucially, its height can be dynamic (because some of its content will be populated dynamically).
Assume I have the following HTML structure
<html>
<body>
<div class="root scrollableContentAncestor">
<div class="app scrollableContentAncestor scrollableContentParent">
<header class="fixedHeader">
<h1>My web app</h1>
...more HTML here...
</header>
<ul class="scrollableContentBelowFixedHeader">
<li>...</li>
<li>...</li>
<li>...</li>
...
</ul>
</div>
</div>
<body>
</html>
I have made it work for a header with static height, by using the position:fixed
+ padding-top
approach as follows.
CSS stylesheet:
.fixedHeader {
position: fixed;
width: 100%;
z-index: 1;
}
.scrollableContentBelowFixedHeader {
/* Offset all the fixed header's vertical distances. */
padding-top: 9.75rem; /* A constant value, or a calc() of constant values. */
}
This works for static height headers, but because you have to specify a static height for padding-top
of the scrollable block, it is not satisfactory for dynamic height headers.
As an added mild annoyance, the vertical scroll bar covers full viewport, i.e. including the fixed header, not just the scrollable block.
How can I make this work for a dynamic height header?