【问题标题】:IE 11 flex align-Items: center with an absolute positioned div aligns the top of the div not the middle [duplicate]IE 11 flex align-Items:中心与绝对定位的div对齐div的顶部而不是中间[重复]
【发布时间】:2018-09-19 15:00:37
【问题描述】:

如果父 div 是相对位置并显示 flex,则 align-items: center,绝对定位的子组件没有按预期居中,而是该组件的顶部居中

有人知道如何解决这个怪癖吗?

https://codepen.io/anon/pen/aYXJZe

.crumb {
  width: 100%;
  margin-top: 100px;
  height: 100px;
  background-color: grey;
}

.arrowWrapper {
  height: 100px;
  width: 100px;
  background-color: red;
  display: flex;
  align-items: center;
  position: relative;
}

.arrow {
  height: 150px;
  width: 80px;
  background-color: blue;
  position: absolute;
}
<div class="crumb">
  <div class="arrowWrapper">
    <span class="arrow"></span>
  </div>
</div>

【问题讨论】:

  • 可以在chrome中打开code pen查看想要的效果

标签: css internet-explorer flexbox internet-explorer-11


【解决方案1】:

在css下面添加并检查

  .arrow {
  height: 150px;
  width: 80px;
  background-color: blue;
  position: absolute;
top:-25px;
}

【讨论】:

  • 这适用于这个具体的例子,但实际的技术问题有多个高度的多个 div,所以我希望有一个更通用的解决方案 >
【解决方案2】:

添加CSS .arrowWrapper {margin: 0 auto;} 和 .arrow {left: 0; right: 0; margin: 0 auto; transform: translateY(-50%); top: 50%}

.crumb {
    width: 100%;
    height: 100px;
    background-color: grey;
}

.arrowWrapper {
    height: 100px;
    width: 100px;
    background-color: red;
    display: flex;
    align-items: center;
    position: relative;
    justify-content: center;
    margin: 0 auto;
}

.arrow {
    height: 60px;
    width: 80px;
    background-color: blue;
    position: absolute;
    right: 0;
    left: 0;
    top: 50%;
    bottom: 0;
    margin: 0 auto;
    transform: translateY(-50%);
}
<div class="crumb">
    <div class="arrowWrapper">
        <span class="arrow"></span>
    </div>
</div>

【讨论】:

  • 它水平居中而不是垂直居中,如果您在 chrome 中打开代码笔,您将看到所需的效果
  • 我已经编辑了我的答案@MichaelNorrie
猜你喜欢
  • 1970-01-01
  • 2018-09-09
  • 2022-09-23
  • 2017-05-14
  • 2019-09-04
  • 2018-11-20
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
相关资源
最近更新 更多