gpt4 book ai didi

html - Flexbox 布局 - 将元素移出文档流并居中显示

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:56 24 4
gpt4 key购买 nike

我正在尝试学习 flexbox。在我的示例中,我想将灰色框(联系人框)从其在文档中的自然位置移动到页面的中心。

前两个元素(蓝色列和绿色列)各占 50% 宽度并拉伸(stretch)以填满屏幕。

见图片: enter image description here

HTML:

<div id='container-row'>

<section class='container-column pic-bkg'>

<nav id='main-nav'>
<ul id='main-nav-list'>
<li>Home</li>
<li>Link</li>
<li>Link</li>
</ul>
</nav>
</section>

<section class='container-column solid-bkg'>

</section>

<section class='container-column contact-box'>
contact box
</section>

</div>
<!-- close id container-row -->
<!-- id container-row is for flexbox 2 column layout -->

这是我第一次使用 Flexbox,我不知道如何只移动一个 flex 元素。

CSS:

body {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: stretch;
height: 100%;
margin: 0;
padding: 0;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 16px;
color: #fff;
}

#container-row {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: stretch;
justify-content: center;
width: 100%;
}

.container-column {
width: 50%;
padding: 4em;
}


#main-nav-list {
display: inline-flex;;
flex-wrap: wrap;
flex-direction: row;
list-style: none;
}

#main-nav-list li {
margin-right: 1em;
}

.solid-bkg {
background-color: #0C3134;
}

.pic-bkg {
background-color: blue;
}

.container-column:nth-child(3){
background-color: grey;
height: 30px;
width: 30%;
}

Codepen here:

最佳答案

请使用如下所示的“.container-column:nth-child(3)”类内容,您将获得结果。

.container-column:nth-child(3) {
background-color: grey;
height: 30px;
width: 30%;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}

关于html - Flexbox 布局 - 将元素移出文档流并居中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49410589/

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