Question

My current attempt to arrange boxes within a container has brought forth this output.

enter image description here

But what I'm trying to achieve is,

  • fill each row with maximum number of boxes but each box has a fixed padding and margin. I believe I have already achieved this.
  • I need the individual boxes in each row to cover up the remaining spaces with a gap of only 1px.

For eg: Take first row, these should me only 1px gap between each. Or take last row, with just one box it should cover up the whole width of container.

Code here: http://codepen.io/anon/pen/rbkdh

Code placed here for reference:

HTML

<ul class="flex-container">
  <li class="flex-item">1, One One One</li>
  <li class="flex-item">2, Two Two</li>
  <li class="flex-item">3, Three</li>
  <li class="flex-item">4, Four Four Four Four Four</li>
  <li class="flex-item">5, Five Five</li>
  <li class="flex-item">6, Six</li>
</ul>

CSS

.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  border: 1px solid;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;

  flex-direction: row;
  flex-wrap: wrap;
  -webkit-flex-flow: row wrap;
  justify-content: space-between;
  align-items: stretch;
  align-content: stretch;

  max-width:400px; /*only width is fixed*/
}

.flex-item {
  background: green;
  padding-top: 10px;
  padding-bottom: 10px;
  padding-left: 15px;
  padding-right: 15px;
  margin: 2px;

  line-height: 2em;
  color: white;
  font-weight: bold;
  font-size: 1em;
  text-align: center;

  flex-grow:1;
} 
Was it helpful?

Solution

Give .flex-item a flex-grow:1. Remember to apply flex to the child, not the container.

http://codepen.io/anon/pen/jkLCs

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top