How to make CSS Sticky work with Flex issue

前端 未结 1 1273
Happy的楠姐
Happy的楠姐 2020-12-12 06:32

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

相关标签:
1条回答
  • 2020-12-12 06:57

    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>

    0 讨论(0)
提交回复
热议问题