gpt4 book ai didi

html - flex 容器 CSS : How to center div element?

转载 作者:行者123 更新时间:2023-11-28 03:19:18 25 4
gpt4 key购买 nike

我有以下 HTML/CSS 代码,但我无法将内容置于 flex 容器的中心。结果是:

ko result

在我看来,我想将圆环置于 flex 容器的中心,并将文本(温度)和 CSS 动画(箭头)置于圆环动画内温度之后的中心。

如何做到这一点?

body {
background-color: #0d74ff;
}

.container {
padding: 20px;
}

.flexwrap {
display: flex;
flex-wrap: wrap;
}

.cell {
position: relative;
height: 200px;
width: 200px;
border: 1px solid rgba(0, 0, 0, 0.25);
}

.loader-ring {
width: 150px;
height: 150px;
}

.loader-ring-light {
width: 150px;
height: 150px;
border-radius: 150px;
box-shadow: 0 4px 0 #ffffff inset;
animation: rotate-360 6s linear infinite;
}

.loader-text {
font-weight: bold;
font-size: 2em;
}

.scroll-down {
border: 2px solid #fff;
border-radius: 100px;
width: 30px;
height: 30px;
}

.scroll-down i {
display: block;
border-radius: 100px;
transition: all 0.4s ease;
width: 100%;
height: 100%;
background-size: 0 auto;
animation: pulse 1.5s 0s infinite normal ease forwards;
background-image: url("https://jamesmuspratt.com/codepen/img/arrow-down.svg");
background-repeat: no-repeat;
}

@keyframes rotate-360 {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}

@keyframes pulse {
0% {
opacity: 0;
background-position: center top;
background-size: 0 auto;
}
10% {
opacity: 0;
}
50% {
opacity: 1;
background-size: 75% auto;
}
90% {
opacity: 0;
}
100% {
opacity: 0;
background-position: center bottom;
background-size: 0 auto;
}
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">

<div class="container">
<div class="flexwrap">
<div class="cell">
<div class='loader-ring'>
<div class='loader-ring-light'></div>
<div class='loader-ring-track'>
<div class='loader-text'>26.6&deg;</div>
<div class="scroll-down"><i></i></div>
</div>
</div>
</div>
</div>
</div>

最佳答案

这里不需要那么多方 block 。您需要绝对定位,因为元素需要相互堆叠。同样为了居中绝对定位的元素,你需要这种样式

.loader-ring,
.loader-ring-track {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

演示:

body {
background-color: #0d74ff;
}

.container {
padding: 20px;
}

.cell {
position: relative;
height: 200px;
width: 200px;
border: 1px solid rgba(0, 0, 0, 0.25);
}

/* center absolutely positioned items */
/* both vertically and horizontally */
.loader-ring,
.loader-ring-track {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.loader-ring-track {
display: flex; /* new */
}

.loader-ring {
width: 150px;
height: 150px;
}

.loader-ring-light {
width: 150px;
height: 150px;
border-radius: 150px;
box-shadow: 0 4px 0 #ffffff inset;
animation: rotate-360 6s linear infinite;
}

.loader-text {
font-weight: bold;
font-size: 2em;
}

.scroll-down {
border: 2px solid #fff;
border-radius: 100px;
width: 30px;
height: 30px;
}

.scroll-down i {
display: block;
border-radius: 100px;
transition: all 0.4s ease;
width: 100%;
height: 100%;
background-size: 0 auto;
animation: pulse 1.5s 0s infinite normal ease forwards;
background-image: url("https://jamesmuspratt.com/codepen/img/arrow-down.svg");
background-repeat: no-repeat;
}

@keyframes rotate-360 {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}

@keyframes pulse {
0% {
opacity: 0;
background-position: center top;
background-size: 0 auto;
}
10% {
opacity: 0;
}
50% {
opacity: 1;
background-size: 75% auto;
}
90% {
opacity: 0;
}
100% {
opacity: 0;
background-position: center bottom;
background-size: 0 auto;
}
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">

<div class="cell">
<div class='loader-ring'>
<div class='loader-ring-light'></div>
<div class='loader-ring-track'>
<div class='loader-text'>26.6&deg;</div>
<div class="scroll-down"><i></i></div>
</div>
</div>
</div>

关于html - flex 容器 CSS : How to center div element?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45287478/

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