如何将所有左浮动元素对齐在父元素的中心?

时间:2016-07-15 08:48:20

标签: javascript jquery html5 css3



val schema = df.schema.fields.map(x => (x.name, x.dataType))
  val rdd = df.map{x =>
    schema.map{y =>
      x.getAs[y._2](s"""${y._1}""")}
  }

.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;的内容与中心对齐。

3 个答案:

答案 0 :(得分:1)

请参阅此处: jsfiddle

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

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

代码:

.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;
 }

让我知道这是否是您正在寻找的

答案 1 :(得分:0)

添加到width:100%;并移除{{1}}

答案 2 :(得分:0)

也许这可以帮到你: 添加边距50% - 容器宽度的一半。只是免责声明:某些浏览器不支持此功能。

.child
{
width: 100px;
height: 30px;
margin-left: calc(50% - 50px);
text-align: center;
border-radius: 10px;
background-color: #FFFF00;
float: left;
}