2

I have two flex rows. First row has 6 equal columns with flex: 1. Second row has column with flex: 4 and column with flex:2. Columns have margin-right: 10px set, except for last child in a row.

http://jsbin.com/gufihoyaha/edit?html,css,output

.row {
  display: flex;
}

.row .col {
  margin-right: 10px;
  background-color: coral;
}

.row .col:last-child {
  margin-right: 0;
}

.flex-1 {
  flex: 1;
}

.flex-4 {
  flex: 4;
}

.flex-2 {
  flex: 2;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div class="row">
  <div class="col flex-1">1</div>
  <div class="col flex-1">1</div>
  <div class="col flex-1">1</div>
  <div class="col flex-1">1</div>
  <div class="col flex-1">1</div>
  <div class="col flex-1">1</div>
</div>
<div class="row">
  <div class="col flex-4">4</div>
  <div class="col flex-2">2</div>
</div>
</body>
</html>

But the result differs from what I expected:

enter image description here

What I want is something like this:

enter image description here

Question: Why does it happen and how to fix that?

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Serhii Holinei
  • 5,758
  • 2
  • 32
  • 46

1 Answers1

1

Only display:grid would actually be able to do this correctly:

unfortunately, at this time, it is quiet new and not implemented every where. See http://caniuse.com/#search=grid

To know more about it : https://css-tricks.com/snippets/css/complete-guide-grid/

.row {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
}

.row .col {
  background-color: coral;
  margin-right: 10px;
}

.row .col:last-child {
  margin-right: 0;
}

.flex-1 {}

.flex-4 {
  grid-column: auto / span 4;
}

.flex-2 {
  grid-column: auto / span 2;
}
<div class="row">
  <div class="col flex-1">1</div>
  <div class="col flex-1">1</div>
  <div class="col flex-1">1</div>
  <div class="col flex-1">1</div>
  <div class="col flex-1">1</div>
  <div class="col flex-1">1</div>
</div>
<div class="row">
  <div class="col flex-4">4</div>
  <div class="col flex-2">2</div>
</div>
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129