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

标签 javascript jquery html css

.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/

相关文章:

javascript - 如何在 javascript (Cscript.exe) 中读取二进制文件?

javascript - 在单引号 (') 前添加值

javascript - 关于 Omniture(SiteCatalyst 或 Adob​​e Analytics)的问题

jquery - 如何在前面的div较小时隐藏div

javascript - JQuery- document.ready 函数不工作

html - 流体高度受限图像 HTML

javascript - $.getjson IE 特定错误

jquery - 为封闭的 div 添加单独的点击事件

javascript - 添加一个 css 属性

javascript - 如何判断 Google Analytics 是否已加载