Sticky header doesn't stick anymore when scrolled beyond a certain point

yotube
0

Issue

Given a container, I'd like to have a sticky header on the left and a sticky sidebar on the right. The scrollbar should appear on the right of the container, top to bottom (including the sticky header).

enter image description here

Unfortunately, when the content is scrolled, the sticky header doesn't stick anymore to the top. I assume it's because the container has a defined height and we scroll past that height.

What's the easiest way to achieve this layout?



.container {
outline: 1px solid black;
margin: 60px;
height: 300px;
width: 400px;
display: flex;
gap: 20px;
overflow-y: auto;
}

.sticky-header {
position: sticky;
top: 0;
padding: 8px;
text-align: center;
background-color: #ddd;
}

.sticky-sidebar {
position: sticky;
top: 0;
padding: 8px;
background-color: #eee;
}

<div class="container">
<div>
<div class="sticky-header">Sticky header</div>
<div>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line
in section 1.10.32. There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use
a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the
Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or
non-characteristic words etc.
</div>
</div>
<div class="sticky-sidebar">Sidebar</div>
</div>




Solution

It's related to the stretch default alignment of flex box. Disable it using align-self: start



.container {
outline: 1px solid black;
margin: 60px;
height: 300px;
width: 400px;
display: flex;
gap: 20px;
overflow-y: auto;
}
.container > div:first-child {
align-self: start; /* added */
}

.sticky-header {
position: sticky;
top: 0;
padding: 8px;
text-align: center;
background-color: #ddd;
}

.sticky-sidebar {
position: sticky;
top: 0;
padding: 8px;
background-color: #eee;
}

<div class="container">
<div>
<div class="sticky-header">Sticky header</div>
<div>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line
in section 1.10.32. There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use
a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the
Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or
non-characteristic words etc.
</div>
</div>
<div class="sticky-sidebar">Sidebar</div>
</div>





Answered By - Temani Afif
Tags

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top