【问题标题】:Can't vertically center content with Flexbox on IE11无法在 IE11 上使用 Flexbox 垂直居中内容
【发布时间】:2018-06-22 04:01:30
【问题描述】:

所以,我尝试使用 flexbox 将 2 个 div 垂直居中,它以所有主流浏览器为中心,但在 IE11 上无法正确居中。

对此进行搜索,我发现以最小高度居中 div 存在问题,但我没有使用它,所以我不知道有什么问题或缺失。

JSFiddle

.container {
  position: relative;
  display: flex;
  align-items: center;
}

img {
  max-width: 100%;
}

.prev {
  width: 40px;
  height: 40px;
  background: rgba(0,0,0,.8);
  position: absolute;
  left: 30px;
}

.next {
  width: 40px;
  height: 40px;
  background: rgba(0,0,0,.8);
  position: absolute;
  right: 30px;
}
 <div class="container">
  <img src="https://www.w3schools.com/css/trolltunga.jpg">
  <div class="prev"></div>
  <div class="next"></div>
</div>

【问题讨论】:

标签: html css internet-explorer flexbox


【解决方案1】:

您可以删除 flexbox 规则,而是使用 transform 垂直居中您的 div...

fiddle

.container {
  position: relative;
}

img {
  max-width: 100%;
  display: block;
  /* added */
}

.prev,
.next {
  top: 50%;
  transform: translateY(-50%);
  width: 40px;
  height: 40px;
  background: rgba(0, 0, 0, .8);
  position: absolute;
}

.prev {
  left: 30px;
}

.next {
  right: 30px;
}
<div class="container">
  <img src="https://www.w3schools.com/css/trolltunga.jpg">
  <div class="prev"></div>
  <div class="next"></div>
</div>

或者,如果你想保留flexbox规则,你可以添加topbottommargin属性来满足IE。

fiddle

.container {
  position: relative;
  display: flex;
  align-items: center;
}

img {
  max-width: 100%;
  display: block; /* added */
}

.prev,
.next {
  top: 0;
  bottom: 0;
  margin: auto;
  width: 40px;
  height: 40px;
  background: rgba(0, 0, 0, .8);
  position: absolute;
}

.prev {
  left: 30px;
}

.next {
  right: 30px;
}
<div class="container">
  <img src="https://www.w3schools.com/css/trolltunga.jpg">
  <div class="prev"></div>
  <div class="next"></div>
</div>

【讨论】:

    猜你喜欢
    • 2016-09-10
    • 2017-12-12
    • 2021-11-06
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 2019-02-19
    • 2016-12-04
    相关资源
    最近更新 更多