Make an element sticky until a specific amount of the remaining container height is reached

How can an element be defined to behave like a sticky element only when the remaining height of the element is greater than a specific size (that I will call sticky container margin)?

For example, in the below image:

You can see that the light blue container has a sticky red element, and the sticky container margin is showed as a dark blue line. When the container element is leaving the screen, the red element stays sticky until the sticky container margin reaches the end of the container.

Here is an initial code to try out some solutions. I tried to make the blue line a container, but that messes up the initial position of the rest of the content.

section {
  height: 300lvh;
}

.content {
  background: #eee;
  height: 80lvh;
}

.box {
  height: 120lvh;
  background: #9f9;
  display: flex;
  flex-direction: column;
}

.sticky {
  position: sticky;
  top: 0px;
  height: 20lvh;
  background: #99f;
}

.end {
  color: red;
  padding-top: 50px;
}

.flex-end {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}
<section>
  <div class="content">Prev content</div>
  <div class="box">
    <div class="sticky">
      Sticky element
    </div>
    <div>
      Content
    </div>
    <div class="flex-end">
      Content
    </div>
  </div>
  <div class="content end">
    When this is visible, the sticky element should go up
  </div>
</section>

3

I would play with some margin

section {
  height: 300lvh;
}

.content {
  background: #eee;
  height: 80lvh;
}

.box {
  height: 120lvh;
  background: #9f9;
  display: flex;
  flex-direction: column;
}

.sticky {
  position: sticky;
  top: 0px;
  height: 20lvh;
  margin-bottom: 80lvh; /* here */
  background: #99f;
}

.sticky + * {
  margin-top: -80lvh; /* and here */
}

.end {
  color: red;
  padding-top: 50px;
}

.flex-end {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}
<section>
  <div class="content">Prev content</div>
  <div class="box">
    <div class="sticky">
      Sticky element
    </div>
    <div>
      Content
    </div>
    <div class="flex-end">
      Content
    </div>
  </div>
  <div class="content end">
    When this is visible, the sticky element should go up
  </div>
</section>

One solution is to use a wrapper for the sticky content. The concept is to have a smaller container to start being scrolled out of view when content end comes in.

You don’t need Javascript if the height of the sticky element is hard-coded (20lvh in your case), or else you will need to use Javascript to get the height of the sticky element.

.box {
  --sticky-height: 20lvh;
  height: 120lvh;
  background: #9f9;
  position: relative;
}

.sticky-wrapper {
  position: absolute;
  width: 100%;
  height: calc(100% - 100lvh + var(--sticky-height));
  z-index: 1;
}

.content-container {
  position: relative;
  top: var(--sticky-height);
  height: calc(100% - var(--sticky-height));
  /*The css for laying out the contents originally in box needs to be moved here*/
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

As shown in below example:

section {
  height: 300lvh;
}

.content {
  background: #eee;
  height: 80lvh;
}

.box {
  --sticky-height: 20lvh;

  height: 120lvh;
  background: #9f9;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
}

.sticky {
  position: sticky;
  top: 0px;
  height: var(--sticky-height);
  background: #99f;
}

.end {
  color: red;
  padding-top: 50px;
}

.flex-end {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.sticky-wrapper {
  position: absolute;
  width: 100%;
  height: calc(100% - 100lvh + var(--sticky-height));
  z-index: 1;
}

.content-container {
  position: relative;
  top: var(--sticky-height);
  height: calc(100% - var(--sticky-height));
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
<section>
  <div class="content">Prev content</div>
  <div class="box">
    <div class="sticky-wrapper">
      <div class="sticky">
        Sticky element
      </div>
    </div>
    <div class="content-container">
      content
      <div class="flex-end">
        Content
      </div>
    </div>
  </div>
  <div class="content end">
    When this is visible, the sticky element should go up
  </div>
</section>

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật