Position sticky for sidebar whick is larger than screen size
Asked Answered
C

1

8

I have a sidebar whose height is larger than the screen size and when using position: sticky, I won't be able to see the bottom of the sidebar until I scroll to the end of the page. How can I make sure that when scrolling, for example, 50% of the page already shows the lower part of the sidebar?

Here code sample: https://jsfiddle.net/zxypmqvg/3/

.main-block {
  display: flex;
}

.sidebar {
  background-color: LightCoral;
  width: 25%;
}

.sidebar-content {
  margin: 5px;
  margin-bottom: 40px;
  background-color: LimeGreen;
  position: sticky;
  top: 0;
}

.main-content {
  width: 75%;
  height: 4000px;
  background-color: LightBlue;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>

<body>
  <div class="main-block">
    <div class="sidebar">
      <div class="sidebar-content">
        <p>sometext for example for stackoverfrom</p>
        <p>sometext for example for stackoverfrom</p>
        <p>sometext for example for stackoverfrom</p>
        <p>sometext for example for stackoverfrom</p>
        <p>sometext for example for stackoverfrom</p>
        <p>sometext for example for stackoverfrom</p>
        <p>last string</p>
      </div>
    </div>
    <div class="main-content">

    </div>
  </div>
</body>

</html>

last string shows only when achieve end of page

last string shows only when achieve end of page

Capsaicin answered 1/10, 2021 at 15:8 Comment(0)
W
3

Maybe, a good practise is add this css to .sidebar-content

overflow-y: auto;
max-height: 100vh;
Weig answered 1/10, 2021 at 18:23 Comment(1)
I would love to see behaviour when initially it scrolls to bottom of the sidebar and then scrolls up when user start scrolling up whole window like on this page: tripconnect.plHopkins

© 2022 - 2024 — McMap. All rights reserved.