gpt4 book ai didi

html - 绝对容器中的 CSS flex 布局

转载 作者:太空宇宙 更新时间:2023-11-03 22:23:37 24 4
gpt4 key购买 nike

我有以下 HTML/CSS,其中包含一个具有绝对高度的容器 (.full-width.section3)。但是,将内容居中是行不通的。由于我是 flex 模型的新手,有人可以向我解释一下,为什么三个子 div 不是垂直居中的吗?

body {
margin:0;

}
.container {
display:flex;
flex-wrap:wrap;
height:100vh;
background-color: white;
}
.full-width {
width:100%;
}

.full-width.section3{
height: 795px;
}

.full-width > .content > .third-parent{
height: 100%;
display: flex;
}

.full-width > .content > .third-parent > .third{
position: relative;
flex: 1 1 0px;
width: 100%;
border: 1px solid black;
}

.full-width > .content > .third-parent > .third > img{
position: absolute;
width: 100%;
height: auto;
left: 50%;
top:50%;
visibility: visible;
text-align: center;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}

.middle-text{
position: absolute;
width: 100%;
left: 50%;
top:50%;
visibility: visible;
text-align: center;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}


.full-width > .content > .third-parent > .third > .middle-text > .list-div2{
display: flex;
justify-content: center;
align-items: center;
width: 100%;
}

.full-width > .content > .third-parent > .third > .middle-text > .list-div2 li{
position: relative;
display: block;
margin-bottom: 5px;
padding: 10px;
text-align: left;
text-transform: uppercase;
visibility: visible;
}

.list-div2 li::before{
content: '\2022';
margin-right: 10px;
}
.items-list2 {
margin: 0;
padding: 0;
}
<div class="container">
<div class="full-width section3">
<div class="content">
<div class="third-parent">
<div class="third" id="one">
<img src="https://fakeimg.pl/350x200/?text=right">
</div>
<div class="third" id="two">
<div class="middle-text">
<h1>Some headline</h1>
<div class="list-div2">
<ul class="items-list2" id="list">
<li>Entry A</li>
<li>Entry B</li>
<li>Entry C</li>
<li>Entry D</li>
</ul>
</div>
</div>
</div>
<div class="third" id="three">
<img src="https://fakeimg.pl/350x200/?text=left">
</div>
</div>
</div>
</div>
</div>

最佳答案

您将 flex 与绝对定位混合在一起。这会破坏子元素的 flex :

As it is out-of-flow, an absolutely-positioned child of a flex container does not participate in flex layout.

来源:https://www.w3.org/TR/css-flexbox-1/#abspos-items

关于html - 绝对容器中的 CSS flex 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52478657/

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