【问题标题】:How to make CSS Sticky work with Flex issue如何使 CSS Sticky 与 Flex 问题一起工作
【发布时间】:2020-06-02 05:54:02
【问题描述】:

我有一个 HTML 结构,我似乎无法让 CSS 位置保持粘性。

我认为是因为它位于 aside 容器内。如果我让aside 坚持它就可以了。

我希望.product-info div 具有粘性,当它碰到 div .content-other 时它会松开。

除非使用 flex,否则我可以从 aside 中移出 .personal-info.product-info 并让它们彼此靠右放置?喜欢

content | Personal info | Product info

那就不用麻烦把包装放在一边吗?不知道如何用 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>

干杯

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    只需删除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>

    【讨论】:

      猜你喜欢
      • 2021-10-29
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 2010-11-09
      • 1970-01-01
      • 2016-05-05
      相关资源
      最近更新 更多