我想在css中管理flex

Par*_*ani 1 css css3 flexbox

我需要弹性框像截图我创建演示,但我不需要所有项目

截图: 在此输入图像描述

.flex-container {
  display: flex;
  justify-content: center;
  background-color: DodgerBlue;
}

.flex-container > div {
  background-color: #f1f1f1;
  width: 100px;
  margin: 10px;
  text-align: center;
  line-height: 75px;
  font-size: 30px;
}
Run Code Online (Sandbox Code Playgroud)

https://jsfiddle.net/parthjasani/103t93xd/

LGS*_*Son 5

使用Flexbox,自动边距得到了更新,使它们比块流更有用.

在这种情况下,如果要为第一个和最后一个孩子添加一个自动边距,它们将被推到父母的开头/结尾,同时将两个中间推到中心.

堆栈代码段

.flex-container {
  display: flex;
  /*justify-content: center;        removed, not needed  */
  background-color: DodgerBlue;
}

.flex-container > div {
  background-color: #f1f1f1;
  width: 100px;
  margin: 10px;
  text-align: center;
  line-height: 75px;
  font-size: 30px;
}

.flex-container > div:first-child {
  margin-right: auto;
}

.flex-container > div:last-child {
  margin-left: auto;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>  
  <div>4</div>  
</div>
Run Code Online (Sandbox Code Playgroud)


或者可以在中间的两个上设置自动边距并渲染相同的结果.

.flex-container {
  display: flex;
  /*justify-content: center;        removed, not needed  */
  background-color: DodgerBlue;
}

.flex-container > div {
  background-color: #f1f1f1;
  width: 100px;
  margin: 10px;
  text-align: center;
  line-height: 75px;
  font-size: 30px;
}

.flex-container > div:nth-child(2) {
  margin-left: auto;
}

.flex-container > div:nth-child(3) {
  margin-right: auto;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>  
  <div>4</div>  
</div>
Run Code Online (Sandbox Code Playgroud)


或者在第2和第4(带nth-child(even)).

.flex-container {
  display: flex;
  /*justify-content: center;        removed, not needed  */
  background-color: DodgerBlue;
}

.flex-container > div {
  background-color: #f1f1f1;
  width: 100px;
  margin: 10px;
  text-align: center;
  line-height: 75px;
  font-size: 30px;
}

.flex-container > div:nth-child(even) {
  margin-left: auto;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>  
  <div>4</div>  
</div>
Run Code Online (Sandbox Code Playgroud)