Окончательный код будет зависеть от типа сетки, которую вы хотите, но, возможно, это поможет вам:
.grid {
margin-bottom: 20px;
background: #8181ac;
}
.grid:after {
content: "";
display: table;
clear: both;
}
/* Let's assume that the gap = 20px */
[class*='col-'] {
float: left;
background: #cfcfcf;
text-align: center;
margin-right: 20px;
}
.grid [class*=col-]:last-of-type { margin-right: -20px; }
.col-1-2 { width: calc(100% * 1 / 2 - 20px / (2 / 1)); }
.col-1-3 { width: calc(100% * 1 / 3 - 20px / (3 / 2)); }
.col-2-3 { width: calc(100% * 2 / 3 - 20px / (3 / 1)); }
.col-1-4 { width: calc(100% * 1 / 4 - 20px / (4 / 3)); }
.col-2-4 { width: calc(100% * 2 / 4 - 20px / (4 / 2)); }
<div class="grid">
<div class="col-1-2"><h3>1/2</h3></div>
<div class="col-1-2"><h3>1/2</h3></div>
</div>
<div class="grid">
<div class="col-1-3"><h3>1/3</h3></div>
<div class="col-1-3"><h3>1/3</h3></div>
<div class="col-1-3"><h3>1/3</h3></div>
</div>
<div class="grid">
<div class="col-1-3"><h3>1/3</h3></div>
<div class="col-2-3"><h3>2/3</h3></div>
</div>
<div class="grid">
<div class="col-1-4"><h3>1/4</h3></div>
<div class="col-1-4"><h3>1/4</h3></div>
<div class="col-1-4"><h3>1/4</h3></div>
<div class="col-1-4"><h3>1/4</h3></div>
</div>
<div class="grid">
<div class="col-1-4"><h3>1/4</h3></div>
<div class="col-2-4"><h3>2/4</h3></div>
<div class="col-1-4"><h3>1/4</h3></div>
</div>
С синтаксисом SCSS в приведенном выше примере col width
рассчитывается следующим образом:
calc(100% * #{$colSize} / #{$gridSize} - #{$gap} / (#{$gridSize} / (#{$gridSize} - #{$colSize})));
И разрывустановить с помощью margin-right
на [class*='col-']
.
Вы, конечно, можете написать миксин для динамического создания селекторов:
$gap: 20px;
$maxGridCol: 5;
@for $i from 1 through $maxGridCol {
@for $y from 1 through $i {
[class*="col-#{$y}-#{$i}"] {
width: calc(100% * #{$y} / #{$i} - (#{$gap} / (#{$i} - #{$y})));
}
}
}