gpt4 book ai didi

html - Div 居中时不在一条直线上

转载 作者:太空宇宙 更新时间:2023-11-04 15:48:16 25 4
gpt4 key购买 nike

我正在尝试使用平移方法将 4 个 div 框居中放置在一条垂直直线上,您在将对象居中放置在屏幕中间时使用该方法,this是我使用的代码:

.body-component {
position: relative;
margin: 10px;
color: #000;
display: inline-block;
vertical-align: top;
background-color: #ff6d00;
overflow: auto;
border: 1px solid #D0D3D4;
}

.width-medium {
width: 500px;
}

.height-medium {
height: 400px;
}

.code-snippet {
position: relative;
width: 95%;
height: 95%;
background-color: #000;
}

.snippet-title {
position: absolute;
color: #248b98;
font-family: "Open Sans", sans-serif;
padding: 15px;
text-decoration: underline;
z-index: 1;
}

.center {
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
margin: 0px;
}

.boxes {
position: relative;
width: 100%;
height: 100%;
}

.box1 {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
top: 20%;
left: 50%;
transform: translate(-20%, -50%);
}

.box2 {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
top: 40%;
left: 50%;
transform: translate(-40%, -50%);
}

.box3 {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
top: 60%;
left: 50%;
transform: translate(-60%, -50%);
}

.box4 {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
top: 80%;
left: 50%;
transform: translate(-80%, -50%);
}
<div class="body-component width-medium height-medium">
<span class="snippet-title">Box loading animation</span>
<div class="code-snippet center">
<div class="boxes">
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</div>
</div>
</div>

我尝试了多种方法来解决这个问题,但我不想使用像素将它们居中,因​​为我在响应式网站上使用它。

最佳答案

如果是绝对定位,可以使用left: 50%,负translateX为50%。

.body-component {
position: relative;
margin: 10px;
color: #000;
display: inline-block;
vertical-align: top;
background-color: #ff6d00;
overflow: auto;
border: 1px solid #D0D3D4;
}

.width-medium {
width: 500px;
}

.height-medium {
height: 400px;
}

.code-snippet {
position: relative;
width: 95%;
height: 95%;
background-color: #000;
}

.snippet-title {
position: absolute;
color: #248b98;
font-family: "Open Sans", sans-serif;
padding: 15px;
text-decoration: underline;
z-index: 1;
}

.center {
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
margin: 0px;
}

.boxes {
position: relative;
width: 100%;
height: 100%;
}

.box {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
left: 50%;
transform: translateX(-50%)
}

.box1 {
top: 20%;
}

.box2 {
top: 40%;
}

.box3 {
top: 60%;
}

.box4 {
top: 80%;
}
<div class="body-component width-medium height-medium">
<span class="snippet-title">Box loading animation</span>
<div class="code-snippet center">
<div class="boxes">
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4"></div>
</div>
</div>
</div>

也就是说,您可以使用 flexbox 来实现这种布局,而不必为了垂直间距而知道盒子的数量。

html,
body {
margin: 0;
padding: 0;
color: #248b98;
font-family: "Open Sans", sans-serif;
}

.body-component {
background-color: black;
height: 100vh;
border: 10px solid #ff6d00;
box-sizing: border-box;
padding: 10px;
display: flex;
flex-direction: column;
justify-content: space-betwen;
align-items: center;
min-height: 500px;
}

.snippet-title {
flex: 0 1 auto;
text-decoration: underline;
padding: 10px;
}

.code-snippet {
flex: 1 1 auto;
display: flex;
}

.boxes {
width: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
padding: 15px;
}

.box {
width: 30px;
height: 30px;
background-color: #056ab3;
}
<div class="body-component">
<span class="snippet-title">Box loading animation</span>
<div class="code-snippet">
<div class="boxes">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
</div>

关于html - Div 居中时不在一条直线上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53140313/

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