左,右和中心在父Div容器下对齐子div

Jee*_*eet 4 html css

我想根据我的父级Div将子Div与左,右或中心对齐.

我做了以下,

.container{
  border:1px solid;
  padding:1px;
  width:100%;
  margin:1px;
}

.left-item{
  float:left;
  padding:auto;
  margin:1px;
}

.center-item{  
   padding:auto;
  margin:1px;
 }

.right-item{
 float:right;
 padding:auto;
  margin:1px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">        
    <button class="left-item">Left</button> <button class="center-item">Center 2</button> <button class="right-item">Right</button> <button class="left-item">Left</button> <button class="right-item">Right</button> <button class="center-item">Center 1</button> <button class="center-item">Center 3</button>        
</div>
Run Code Online (Sandbox Code Playgroud)

我无法与父母对齐 .谁能帮我这个?child DivsDivs

Dyl*_*ark 5

将框划分为左、中和右的一种更简单的方法是使用 CSS flexbox。通过将 left、center 和 right 包装在一个新div的容器中并将您的容器变成一个 flexbox,display:flex您可以用更少的代码来模拟这种行为。

.container {
  border: 1px solid;
  padding: 1px;
  width: 100%;
  margin: 1px;
  display:flex;
  justify-content:space-between;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div>
    <button>Left</button>
    <button>Left</button>
  </div>
  <div>
    <button>Center 2</button>
    <button>Center 1</button>
    <button>Center 3</button>
  </div>
  <div>
    <button>Right</button>
    <button>Right</button>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


Ayu*_*rma 5

只需将text-align:center属性赋予容器div即可.JS小提琴 - https://jsfiddle.net/72aqsq83/1/

.container{
  border:1px solid;
  padding:1px;
  width:100%;
  margin:1px;
  text-align:center
}
Run Code Online (Sandbox Code Playgroud)