CSS:将网格重构为具有不同子元素的弹性布局

时间:2019-02-21 12:38:00

标签: html css css3 flexbox

我目前正在尝试构建一个应该有3个可能的孩子的弹性布局:

enter image description here

  • 如果包装器只有一个按钮,则它应该具有包装器的整个宽度。
  • 如果包装器有两个孩子,则最后一个孩子的最大宽度应为75px,第一个孩子必须填充可用空间。
  • 如果包装器有三个孩子,则第一行应与前面的示例相同,但最后一个孩子应位于下一行,宽度为100%。

我已经构建了这个网格布局,但是由于某些浏览器的兼容性,我需要将其更改为flex。我怎样才能做到这一点?我已经尝试了很多,但结果总是很糟糕。

.wrapper {
    width: 60%;
    display: grid;
    display: -ms-grid;
    grid-template-columns: 1fr 75px;
    text-align: center;
    position: relative;
    margin-bottom: 20px;
}

.wrapper div+div {
    margin-left: 6px;
}

.wrapper div {
    background-color: yellow;
    padding: .6em 1em;
    text-align: center;
}

.wrapper div.show.single-button {
  grid-column: 1/span 2;
}

.wrapper div.cancel {
    grid-column: 1/span 2;
    -ms-grid-row: 2;
    -ms-grid-column-span: 2;
    margin-top: 6px;
    margin-left: 0;
}
<div class="wrapper">
  <div class="show single-button">Show</div>
</div>

<div class="wrapper">
  <div class="show">Show</div>
  <div class="invoice">Invoice</div>
</div>

<div class="wrapper">
  <div class="show">Show</div>
  <div class="invoice">Invoice</div>
  <div class="cancel">Cancel</div>
</div>

3 个答案:

答案 0 :(得分:2)

明智地使用flex:1flex-wrap似乎有用。

* {
  box-sizing: border-box;
}

.wrapper {
  width: 60%;
  margin: auto;
  display: flex;
  flex-wrap: wrap;
  margin-bottom: 20px;
  justify-content: space-between;
  border: 1px solid green;
}

.wrapper div {
  background-color: lightblue;
  padding: .6em 1em;
  text-align: center;
  margin: 6px;
}

.wrapper div.show {
  flex: 1;
}

.wrapper div.invoice {
  flex: 1;
  max-width: 75px;
}

.wrapper div.cancel {
  flex: 1 1 100%;
}
<div class="wrapper">
  <div class="show single-button">Show</div>
</div>

<div class="wrapper">
  <div class="show">Show</div>
  <div class="invoice">Invoice</div>
</div>

<div class="wrapper">
  <div class="show">Show</div>
  <div class="invoice">Invoice</div>
  <div class="cancel">Cancel</div>
</div>

答案 1 :(得分:2)

Flexbox提供了为此所需的一切。无需混用flexbox和box-model或width属性:

.wrapper {
  height: 100px;
  display: flex;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.wrapper > div {
  flex: 1;
  border: 1px dotted #999;
  background-color: aliceblue;
}

.wrapper > div:nth-child(2) {
  flex-shrink: 0;
  flex-grow: 0;
  flex-basis: 75px;
  -webkit-flex-basis: 75px;
}

.wrapper > div:nth-child(3) {
  flex-shrink: 0;
  flex-grow: 0;
  flex-basis: 100%;
  -webkit-flex-basis: 100%;
}
<div class="wrapper">
  <div>1</div>
</div>

<div class="wrapper">
  <div>1</div>
  <div>2</div>
</div>

<div class="wrapper">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

答案 2 :(得分:1)

您可以使用flex-basis来告知元素的初始宽度,然后使用flex-grow来告知它们是否应该占用更多空间,然后使用flex-wrap使其不会强制它们适合一行,例如: / p>

.wrapper {
  display: flex;
  flex-wrap: wrap; // make it multiline
  margin-bottom: 1rem;
}

.wrapper div {
  background: yellow;
  margin: .2rem;
  padding: .2rem;
  text-align: center;
}

.invoice {
  flex-basis: 75px; // make it have 75px
  flex-grow: 0; // can't grow
  flex-shrink: 0; // can't shrink
}

.show {
  flex-grow: 1; // no basis width, just grow and take the space left from .invoice
}

.cancel {
  flex-basis: 100%; // takes the whole width so it must go on a single row
}

https://codepen.io/anon/pen/KJLqVj