gpt4 book ai didi

css - 在 Safari 中使用 flexbox 进行绝对定位

转载 作者:行者123 更新时间:2023-12-02 08:13:47 24 4
gpt4 key购买 nike

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/

最佳答案

因为position: absolute; break 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>


或者给内部元素一个left/top值

    .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>

关于css - 在 Safari 中使用 flexbox 进行绝对定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43779733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com