gpt4 book ai didi

html - 水平居中 flex 元素

转载 作者:太空宇宙 更新时间:2023-11-04 13:06:03 26 4
gpt4 key购买 nike

是否可以水平居中 .div2 而不是像这张图片那样 absolute/fixed 的位置?

enter image description here

div3可以改变宽度

这是我的代码:

body {
margin:0;
}

.test {
background:green;
color:#fff;
display:flex;
}

.test .div1 {

}

.test .div2 {
margin:0 auto;
background:red;
}

.test .div3 {
margin:0 0 0 auto;
}
<div class="test">
<div class="div1">Left options</div>
<div class="div2">Center div</div>
<div class="div3">Username: test (string can change)</div>
</div>

最佳答案

你可以给所有的div 33.3333%的宽度

.test {
background:green;
color:#fff;
display:flex;
}

.test .div1 {
width:33.3333%;
}

.test .div2 {
margin:0 auto;
background:red;
width:33.3333%;
}

.test .div3 {
margin:0 0 0 auto;
width:33.3333%;
}
<div class="test">
<div class="div1">Left options</div>
<div class="div2">Center div</div>
<div class="div3">Username: test (string can change)</div>
</div>

关于html - 水平居中 flex 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41540678/

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