代码之家  ›  专栏  ›  技术社区  ›  clarkk

覆盖flexbox和溢流

  •  0
  • clarkk  · 技术社区  · 2 年前

    在结账页面上,我在网站顶部做了一个覆盖,其中禁用了在主体上滚动,但在覆盖上启用了溢出。

    但是,如果视口较小,则会剪裁覆盖上的内容。

    如何避免这种情况?

    我试着添加 overflow:auto 到覆盖层,但不起作用:

    body {
      overflow: hidden;
    }
    
    #overlay.active {
      position: fixed;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 20px;
      overflow: auto;
      background: yellow;
    }
    <div id="overlay" class="active">
      <div style="height:300px; background:white; padding:20px">
        content
      </div>
    </div>
    1 回复  |  直到 2 年前
        1
  •  2
  •   John Li    2 年前

    更新

    似乎添加 margin: auto 关于孩子 div 内容所在的位置可以解决此问题:

    #overlay > div { margin: auto; }
    

    使用 safe 对于 align-items 这将是一个很好的解决方案,但目前仅适用于Firefox:

    align-items: safe center;
    

    实例

    body {
      overflow: hidden;
    }
    
    /* 👇 Added on the child div */
    #overlay > div {
      margin: auto;
    }
    
    #overlay.active {
      position: fixed;
      /* 👇 Can use shorthand (Optional: not supported by older browsers and IE) */
      inset: 0;
      display: flex;
      justify-content: center;
      /* 👇 Safe is only working on Firefox as of now  */
      align-items: safe center;
      padding: 20px;
      background: #fff;
      overflow: auto;
      background: pink;
    }
    <div id="overlay" class="active">
      <div style="height:300px; background:white; padding:20px">
        content
      </div>
    </div>
        2
  •  0
  •   Jenny    2 年前

    删除 align-items:center; 您在#overlay.active上拥有的,内容将按预期滚动。#overlay.active只有一个子项,因此对齐项(定义弹性项如何沿当前行的横轴排列)实际上没有任何视觉效果