.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-container
和 display:inline- 上使用
到display:flex
和 justify-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/