gpt4 book ai didi

javascript - 如何将所有左浮动元素对齐到其父元素的中心?

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

.container{width:50%;padding:10px;box-sizing:border-box;background-color:#00FF00;}
.container::after {content: ''; display: table; clear: both;}
.child{width:100px;height:30px;line-height:30px;text-align:center;border-radius:2px;background-color:#FFFF00;float:left;margin:2px;}
<div class="container">
<div class="child">First</div>
<div class="child">Second</div>
<div class="child">Third</div>
<div class="child">Fourth</div>
</div>

我想对齐.container的内容在中心而不使用属性 display: inline-block;.child .

最佳答案

请看这里: jsfiddle

已编辑:在 .sub-containerdisplay:inline- 上使用 display:flexjustify-content:center block .child

或者您可以在 .child 上使用 float:left ,它仍然有效。但我建议你在想要居中时不要使用 float

代码:

.child
{
width: 50px;
height: 30px;
text-align: center;
border-radius: 10px;
background-color: #FFFF00;
display:inline-block;
}

.sub-container
{
width: 50%;
height: auto;
padding: 10px;
box-sizing: border-box;
margin: 0px auto;
background-color: #00FF00;
float: left;
text-align: center;
position:relative;
display: flex;
justify-content: center;
}

让我知道这是否是您要找的东西

关于javascript - 如何将所有左浮动元素对齐到其父元素的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38391791/

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