How to make CSS Sticky work with Flex issue

久未见 提交于 2020-06-06 08:31:19

问题


I have an HTML structure where I can't seem to get the CSS position sticky working.

I think it because it's within the aside container. If I make aside stick it works.

I want the .product-info div to be sticky and when it hits the div .content-other it unsticks.

Unless with flex I could move out .personal-info and .product-info from within the aside and have them sit to the right on top of each other? Like

content | Personal info | Product info

Then not bother having the wrapping aside? Not sure how to stack these like this though with flex.

body {
  padding: 20px;
}

.container {
  margin-left: auto;
  margin-right: auto;
  position: relative;
  padding-bottom: 16px;
  padding-top: 16px;
  width: 100%;
  display: flex;
}

.content {
  position: relative;
  max-width: 100%;
  flex-basis: 74%;
  border: 1px solid black;
  width: 300px;
  margin-right: 20px;
  height: 540px;
}

.right-side {
  align-self: flex-start;
  background-color: #ffffff;
  border: 2px solid #e8e8e3;
  border-radius: 0 4px 4px 4px;
  flex: 1 1;
  flex-basis: 40%;
  min-width: 338px;
  padding: 16px 16px 0;
  display: block;
  width: 400px;
}

.personal-info {
  height: 250px;
}

.product-info {
  position: sticky;
  position: -webkit-sticky;
  top: 24px;
  border: 1px solid red;
}

.content-other {
  width: 100%;
  background: #f5f5f5;
  height: 400px;
}
<div class="container">
  <div class="content">content area here</div>
  <aside class="right-side">
    <div class="personal-info">some info</div>
    <div class="product-info">sticky info</div>
  </aside>
</div>

<div class="content-other">.product-info unsticks when it hits here</div>

Cheers


回答1:


Simply remove align-self: flex-start;

body {
  padding: 20px;
}

.container {
  margin-left: auto;
  margin-right: auto;
  position: relative;
  padding-bottom: 16px;
  padding-top: 16px;
  width: 100%;
  display: flex;
}

.content {
  position: relative;
  max-width: 100%;
  flex-basis: 74%;
  border: 1px solid black;
  width: 300px;
  margin-right: 20px;
  height: 540px;
}

.right-side {
  /*align-self: flex-start;*/
  background-color: #ffffff;
  border: 2px solid #e8e8e3;
  border-radius: 0 4px 4px 4px;
  flex: 1 1;
  flex-basis: 40%;
  min-width: 338px;
  padding: 16px 16px 0;
  display: block;
  width: 400px;
}

.personal-info {
  height: 250px;
}

.product-info {
  position: sticky;
  position: -webkit-sticky;
  top: 24px;
  border: 1px solid red;
}

.content-other {
  width: 100%;
  background: #f5f5f5;
  height: 400px;
}
<div class="container">
  <div class="content">content area here</div>
  <aside class="right-side">
    <div class="personal-info">some info</div>
    <div class="product-info">sticky info</div>
  </aside>
</div>

<div class="content-other">.product-info unsticks when it hits here</div>


来源:https://stackoverflow.com/questions/62145711/how-to-make-css-sticky-work-with-flex-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!