【问题标题】:Css positioning button at center of Overlay with image in background覆盖中心的 Css 定位按钮,背景为图像
【发布时间】:2017-06-09 12:47:20
【问题描述】:

.msg-ex {
  display: flex;
  align-items: center;
  justify-content: center;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}

.msg-ex .msg-ex-overlay {
  position: relative;
  background-color: rgba(0, 0, 0, 0.4);
  width: 100%;
  height: 100%;
  opacity: 0;
  z-index: 2;
}

.msg-ex .msg-ex-overlay .msg-ex-cta {
  position: absolute;
  color: red;
  background-color: #DCE0E3;
  font-size: 1.1em;
  font-weight: 700;
  border-radius: 18px;
  height: 38px;
  width: 100px;
  cursor: pointer;
  top: 50%;
  left: 50%;
  transform: translateX(-48%) translateY(-47%);
}

.msg-ex .msg-ex-overlay:hover {
  opacity: 1;
}

.msg-ex-box {
  position: relative;
  background-color: $voiceColor;
}

.msg-ex-box::after {
  content: '';
  bottom: 0;
  right: 0;
  width: 32px;
  height: 32px;
  background-image: url(../pics/data/img1.png);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: absolute;
  z-index: 1;
}


}
.msg-ex-voice:hover {
  background-color: rgba(0, 0, 0, 0.4);
}
<div class="msg-ex msg-ex-box">
  <div class="msg-ex-overlay">
    <button class="msg-ex-cta">Show</button>
  </div>
</div>

我使用transform: translateX(-48%) translateY(-47%); 将我的“显示”按钮定位在中心,但理想情况下它应该是transform: translateX(50%) translateY(50%);,它位于中心 - 无法弄清楚我做错了什么。请帮忙。

【问题讨论】:

    标签: html css flexbox css-position


    【解决方案1】:

    使用下面的 css 作为你想要居中的按钮

    position:absolute;
    top:50%;
    left:50%;
    transform: translate(-50%,-50%);
    -webkit-transform: translate(-50%,-50%);
    -ms-transform: translate(-50%,-50%);
    -moz-transform: translate(-50%,-50%);
    

    display:flex;
    align-items:center;
    justify-content:center;
    

    【讨论】:

      【解决方案2】:

      那是因为.msg-ex-cta 的父元素没有高度。当您将height: 100%; 用于.msg-ex-overlay 时,该百分比高度需要一个值来继承,因此没有为其父级定义高度,因此0100%0

      虽然将width: 100vw;height: 100vh; 添加到.msg-ex-overlay 可能会得到您想要的,但触发模式/叠加层的方式可能需要更改。虽然我不知道你的确切目标,所以我会保持原样,直到另行通知。

      body {
        margin: 0;
      }
      
      .msg-ex {
        display: flex;
        align-items: center;
        justify-content: center;
        background-position: center;
        background-size: cover;
        background-repeat: no-repeat;
      }
      
      .msg-ex .msg-ex-overlay {
        position: relative;
        background-color: rgba(0, 0, 0, 0.4);
        width: 100vw;
        height: 100vh;
        opacity: 0;
        z-index: 2;
      }
      
      .msg-ex .msg-ex-overlay .msg-ex-cta {
        position: absolute;
        color: red;
        background-color: #DCE0E3;
        font-size: 1.1em;
        font-weight: 700;
        border-radius: 18px;
        height: 38px;
        width: 100px;
        cursor: pointer;
        top: 50%;
        left: 50%;
        transform: translateX(-48%) translateY(-47%);
      }
      
      .msg-ex .msg-ex-overlay:hover {
        opacity: 1;
      }
      
      .msg-ex-box {
        position: relative;
        background-color: $voiceColor;
      }
      
      .msg-ex-box::after {
        content: '';
        bottom: 0;
        right: 0;
        width: 32px;
        height: 32px;
        background-image: url(../pics/data/img1.png);
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;
        position: absolute;
        z-index: 1;
      }
      
      
      }
      .msg-ex-voice:hover {
        background-color: rgba(0, 0, 0, 0.4);
      }
      <div class="msg-ex msg-ex-box">
        <div class="msg-ex-overlay">
          <button class="msg-ex-cta">Show</button>
        </div>
      </div>

      【讨论】:

        【解决方案3】:

        如果您想居中,请将其放在父级中:

        display: flex;
        justify-content: center;
        

        并将其添加到您要居中的孩子:

        align-self: center;
        

        希望这会有所帮助!

        【讨论】:

        • 你实际测试过这个吗?这与 OP 的解决方案有何不同?在这种情况下,父级上的align-items: center; 与子级上的align-self: center; 相同。只有一个元素 align-items: center; 可以从父元素中应用,因此您不必担心某些不应该居中的项目。
        猜你喜欢
        • 2016-06-23
        • 1970-01-01
        • 2015-10-07
        • 2013-08-07
        • 1970-01-01
        • 2015-07-04
        • 1970-01-01
        • 2014-10-03
        • 1970-01-01
        相关资源
        最近更新 更多