Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

html - How to make CSS Sticky work with Flex issue

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...