【问题标题】:Absolutely positioning with flexbox in Safari在 Safari 中使用 flexbox 进行绝对定位
【发布时间】:2017-10-02 10:44:06
【问题描述】:

Safari has full support for FlexBox according to caniuse.

我只是想使用 flexbox 将一些不同大小的 div 堆叠在一起。我真的很好奇为什么这在 Chrome/Firefox 中有效,但在 Safari 中无效:

<div class="container">
  <div class="inner-one"></div>
  <div class="inner-two"></div>
</div>
.container {
  width: 15rem;
  height: 15rem;
  border-radius: 50%;
  background-color: blue;

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

.container div {
  position: absolute;
}

.inner-one {
  width: 13rem;
  height: 13rem;
  border-radius: 50%;
  background-color: green;
}

.inner-two {
  width: 11rem;
  height: 11rem;
  border-radius: 50%;
  background-color: purple;
}

在此处查看 JSFiddle:https://jsfiddle.net/19n95exf/3/

【问题讨论】:

  • 你用-webkit前缀试过了吗?

标签: css safari flexbox


【解决方案1】:

因为position: absolute; 打破display: flex,所以改用transform: translate

    .container {
      position: relative;
      width: 15rem;
      height: 15rem;
      border-radius: 50%;
      background-color: blue;
    }

    .container div {
      border-radius: 50%;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%);
    }

    .inner-one {
      width: 13rem;
      height: 13rem;
      background-color: green;
    }

    .inner-two {
      width: 11rem;
      height: 11rem;
      background-color: purple;
    }
    <div class="container">
      <div class="inner-one"></div>
      <div class="inner-two"></div>
    </div>

或者给内部元素一个左/上值

    .container {
      width: 15rem;
      height: 15rem;
      border-radius: 50%;
      background-color: blue;

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

    .container div {
      border-radius: 50%;
      position: absolute;
    }

    .inner-one {
      left: 1.5rem;
      top: 1.5rem;
      width: 13rem;
      height: 13rem;
      background-color: green;
    }

    .inner-two {
      left: 2.5rem;
      top: 2.5rem;
      width: 11rem;
      height: 11rem;
      background-color: purple;
    }
<div class="container">
      <div class="inner-one"></div>
      <div class="inner-two"></div>
</div>

【讨论】:

  • 传奇。我仍然觉得有趣的是,绝对在 Safari 中与 flex 的处理方式不同。无论哪种方式,这都是一个可行的解决方法。谢谢。
猜你喜欢
  • 2018-09-12
  • 1970-01-01
  • 2017-01-09
  • 2016-08-11
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
相关资源
最近更新 更多