我想在css中管理flex

时间:2018-01-31 11:10:56

标签: css css3 flexbox

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

屏幕截图:enter image description here

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

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

2 个答案:

答案 0 :(得分:5)

Flexbox, auto margins得到了更新,使它们比块流更有用。

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

Stack snippet

sbt assembly all -> (output) 3 jars.
sbt assembly project-A -> (output) 1 project-A jar.
sbt assembly project-B -> (output) 1 project-B jar.
sbt assembly project-C -> (output) 1 project-C jar.
.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;
}

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

<div class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>  
  <div>4</div>  
</div>
.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;
}

或者在第2和第4位(使用<div class="flex-container"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> </div>)。

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

答案 1 :(得分:4)

您可以在第一个和最后一个内部div元素上使用保证金。

.flex-container {
  background-color: DodgerBlue;
  display: flex;
}
.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;
}
<div class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>  
  <div>4</div>  
</div>