【问题标题】:Overlay div won't display on Safari (webkit)覆盖 div 不会在 Safari (webkit) 上显示
【发布时间】:2016-12-15 17:02:21
【问题描述】:

我有一个 div,我想在覆盖屏幕的不透明图像上居中。我已经让它在 Chrome (blink) 中正常工作,但我无法让它在 Safari (webkit) 中显示。我创建了一个simplified JSFiddle version of my issue 并在此处包含了代码 sn-ps。

HTML

<div id="home">
  <div class="wallpaper"></div>
  <div class="info-wrapper">
    <span class="name">John Doe</span>
  </div>
</div>

SCSS

#home {
  position: relative;
  width: 100%;
  height: 100vh;
  min-height: inherit;
  display: flex;
  align-items: center;
  background-color: black;
}

.wallpaper {
  width: 100%;
  height: 100%;
  min-height: inherit;
  opacity: .5;
  background: url(https://images.unsplash.com/photo-1440700265116-fe3f91810d72);
  background-size: cover;
  background-repeat: no-repeat;
}

.info-wrapper {
  position: absolute;
  display: flex;
  justify-content: center;
  width: 100%;
  color: white;
  > .name {
    font-weight: 700;
    font-size: 7em;
  }
}

如果这样可以更轻松地解决此问题,我愿意更改我的 HTML 结构。

【问题讨论】:

    标签: html css safari sass webkit


    【解决方案1】:

    [更新]

    如果元素具有“位置:绝对”,则包含块由最近的“位置”为“绝对”、“相对”或“固定”的祖先建立

    您可以在此link获取更多详细信息

    .container {
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
      position: relative;
    }
    
    .red {
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
      width: 200px;
      height: 200px;
      background-color: red;
    }
    
    .green {
      width: 20px;
      height: 20px;
      background-color: green;
      position: absolute;
    }
    <div class="container">
      <div class="red"></div>
      <div class="green"></div>
    </div>

    在这个示例中,绿色框没有设置left属性,值应该是auto,结果应该是指最近的元素。而且,这在 Safari 和 Firefox 中显示是正确的。 Chrome 不符合标准。


    [老]

    我认为您需要的是让墙纸从文档中流出。在 safari 中运行示例。 sample

    .wallpaper {
        width: 100%;
        height: 100%;
        position: absolute;
        min-height: inherit;
        opacity: .5;
        background: url(https://images.unsplash.com/photo-1440700265116-    fe3f91810d72);
        background-size: cover;
        background-repeat: no-repeat;
    }
    

    【讨论】:

    • 解决了覆盖问题,谢谢!现在来解决垂直居中问题,但如果有必要,那是另一篇文章
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    相关资源
    最近更新 更多