gpt4 book ai didi

css - 在小屏幕上相互叠加时如何使内部 div 水平居中

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

当屏幕很宽时,我有 4 个 div(每个计数器)彼此相邻。当屏幕变小时,它们会堆叠在一起,但会向右对齐

我希望它们始终居中,当 4 个排成一行、两个排成两行和 1 个排成 4 行时。

我怎样才能做到这一点?数字可能会改变并使 div 的宽度可变

看这里: https://jsfiddle.net/Zivo/gp5wnL9f/1/

body {
margin: 0;
--border: 1px solid #cccccc;

}

.numbers {
font-family: lato;
font-size: 72px;
color: #131128;
}

.odometer {
display: inline-block;
top: -7px;
}

.suffix {
float: right
}

.frame {
transform: translate(-50%);
left: 50%;
position: relative;
border-bottom: var(--border);
height: 94px;
overflow: hidden
}

.suffix p {
margin: 0 5px 0 0;
}

.odometer p {
margin: 0 0 0 5px;
}

.theme {
box-sizing: border-box;
margin: 20px;
float: left;
}

.hline {
height: 40px;
width: 50%;
border-right: var(--border);
/* transform: translateY(-4px);*/
}

.num-title {
text-align: center;
font-weight: 700;
font-size: 16px;
letter-spacing: 0.5px;
padding-top: 6px;
}

HTML

<body>
<div class="numbers">
<!--counter 1 - Sensors-->
<div class="sensors theme">
<div class="frame">
<div class="odometer" odometer-goal="10">
<p>0</p>
</div>
<div class="suffix">
<p>k+</p>
</div>
</div>
<div class="hline"></div>
<div class="num-title">Sensors</div>
</div>
<!--counter 2 - ICO-->
<div class="sensors theme">
<div class="frame">
<div class="odometer" odometer-goal="13">
<p>0</p>
</div>
<!--
<div class="suffix">
<p>k+</p>
</div>
-->
</div>
<div class="hline"></div>
<div class="num-title">IOC Types</div>
</div>
<!--counter 3 - Scanners-->
<div class="sensors theme">
<div class="frame">
<div class="odometer" odometer-goal="550">
<p>0</p>
</div>
<div class="suffix">
<p>k+</p>
</div>
</div>
<div class="hline"></div>
<div class="num-title">Scanners</div>
</div>
<!--counter 4 - Attackers-->
<div class="sensors theme">
<div class="frame">
<div class="odometer" odometer-goal="5">
<p>0</p>
</div>
<div class="suffix">
<p>k+</p>
</div>
</div>
<div class="hline"></div>
<div class="num-title">Attackers</div>
</div>
</div>
<script type="text/javascript">
setTimeout(function() {
let elements = document.getElementsByClassName('odometer');
for (var i = 0; i < elements.length; i++) {
let goal = elements[i].getAttribute("odometer-goal");
elements[i].innerHTML = goal;
}
}, 1000);

</script>
</body>

最佳答案

从类 .theme 中删除 float: left 并将以下 flex 属性添加到 .numbers :

display: flex;
flex-wrap: wrap;
justify-content: center;
  • 第一个属性将激活 flex 布局
  • flex-wrap: wrap 允许元素行在没有足够空间时换行
  • justify-content: center 将使元素居中而不是默认对齐(取决于文本方向)

body {
margin: 0;
--border: 1px solid #cccccc;

}

.numbers {
font-family: lato;
font-size: 72px;
color: #131128;
display: flex;
flex-wrap: wrap;
justify-content: center;
}

.odometer {
display: inline-block;
top: -7px;
}

.suffix {
float: right
}

.frame {
transform: translate(-50%);
left: 50%;
position: relative;
border-bottom: var(--border);
height: 94px;
overflow: hidden
}

.suffix p {
margin: 0 5px 0 0;
}

.odometer p {
margin: 0 0 0 5px;
}

.theme {
box-sizing: border-box;
margin: 20px;
}

.hline {
height: 40px;
width: 50%;
border-right: var(--border);
/* transform: translateY(-4px);*/
}

.num-title {
text-align: center;
font-weight: 700;
font-size: 16px;
letter-spacing: 0.5px;
padding-top: 6px;
}
<body>
<div class="numbers">
<!--counter 1 - Sensors-->
<div class="sensors theme">
<div class="frame">
<div class="odometer" odometer-goal="10">
<p>0</p>
</div>
<div class="suffix">
<p>k+</p>
</div>
</div>
<div class="hline"></div>
<div class="num-title">Sensors</div>
</div>
<!--counter 2 - ICO-->
<div class="sensors theme">
<div class="frame">
<div class="odometer" odometer-goal="13">
<p>0</p>
</div>
<!--
<div class="suffix">
<p>k+</p>
</div>
-->
</div>
<div class="hline"></div>
<div class="num-title">IOC Types</div>
</div>
<!--counter 3 - Scanners-->
<div class="sensors theme">
<div class="frame">
<div class="odometer" odometer-goal="550">
<p>0</p>
</div>
<div class="suffix">
<p>k+</p>
</div>
</div>
<div class="hline"></div>
<div class="num-title">Scanners</div>
</div>
<!--counter 4 - Attackers-->
<div class="sensors theme">
<div class="frame">
<div class="odometer" odometer-goal="5">
<p>0</p>
</div>
<div class="suffix">
<p>k+</p>
</div>
</div>
<div class="hline"></div>
<div class="num-title">Attackers</div>
</div>
</div>
<script type="text/javascript">
setTimeout(function() {
let elements = document.getElementsByClassName('odometer');
for (var i = 0; i < elements.length; i++) {
let goal = elements[i].getAttribute("odometer-goal");
elements[i].innerHTML = goal;
}
}, 1000);

</script>
</body>

关于css - 在小屏幕上相互叠加时如何使内部 div 水平居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55621876/

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