How to make an automatic grid for exact measurements using SCSS?

Asked

Viewed 40 times

1

I have the following example but in percentage:

$grid__cols: 12;
@for $i from 1 through $grid__cols {
  .col-#{$i} {
    flex-basis: (100 / ($grid__cols / $i) ) * 1%;
  }
}

That may be tested here, to generate the CSS automatically.

How could I apply the same rule in pixel only respecting the 12 columns with the exact mediated grid of 76.7px / 12px up to a total width 1272px?

Grid example:

inserir a descrição da imagem aqui

1 answer

4


This formula will make the progression of the columns in PX until 1272px

$grid__cols: 12;
@for $i from 1 through $grid__cols {
  .col-#{$i} {
    flex-basis: (1272 / ($grid__cols / $i) ) * 1px;
  }
}

OUTPUT

.col-1 {
  flex-basis: 106px;
}
.col-2 {
  flex-basis: 212px;
}
.col-3 {
  flex-basis: 318px;
}
.col-4 {
  flex-basis: 424px;
}
.col-5 {
  flex-basis: 530px;
}
.col-6 {
  flex-basis: 636px;
}
.col-7 {
  flex-basis: 742px;
}
.col-8 {
  flex-basis: 848px;
}
.col-9 {
  flex-basis: 954px;
}
.col-10 {
  flex-basis: 1060px;
}
.col-11 {
  flex-basis: 1166px;
}
.col-12 {
  flex-basis: 1272px;
}

And that formula will create 12 colunas of 76.7px, which makes no sense at all, because you would want a class with col-12, but which has the same px width as col-6 The.o

$grid__cols: 12;
@for $i from 1 through $grid__cols {
  .col-#{$i} {
    flex-basis: 76.7px;
  }
}

OUTPUT

.col-1 {
  flex-basis: 76.7px;
}
.col-2 {
  flex-basis: 76.7px;
}
.col-3 {
  flex-basis: 76.7px;
}
.col-4 {
  flex-basis: 76.7px;
}
.col-5 {
  flex-basis: 76.7px;
}
.col-6 {
  flex-basis: 76.7px;
}
.col-7 {
  flex-basis: 76.7px;
}
.col-8 {
  flex-basis: 76.7px;
}
.col-9 {
  flex-basis: 76.7px;
}
.col-10 {
  flex-basis: 76.7px;
}
.col-11 {
  flex-basis: 76.7px;
}
.col-12 {
  flex-basis: 76.7px;
}
  • 1

    Perfect, that’s what I wanted!

  • @Ivanferrer cool that helped, but I’m updating the answer, she had an error in the first formula I used in the answer, before the col-6 of the first formula did not have 1/2 of the total width of the Grid, ie it was not 636px (1272 / 2). But now it’s right!

  • Thank you, excellent!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.