Créer une grille à partir de la propriété display: flex
Voici un exemple de grille flex, trouvée sur le CodePen de Ruslan Pivovarov.
Reprenant la logique bootstrap, elle comporte la base d’une disposition en 12 colonnes et permet de structurer sa page très facilement. Pour l’utiliser, il faut se servir du « container » pour englober son contenu. A l’intérieur on glisse autant de row que nécessaire. Et dans celles-ci, on glisse nos colonnes en se servant des classes flex-col-xs, flex-col-sm, flex-col-md, et flex-col-lg. Ce code peut être modifié, amélioré et on peut y ajouter toutes les autres classes utiles de bootstrap pour en faire son propre framework css de base !
.flex-container {
box-sizing: border-box;
margin: 0 auto;
padding: 0 1em;
}
@media (min-width: 540px) {
.flex-container {
max-width: 540px;
}
}
@media (min-width: 768px) {
.flex-container {
max-width: 768px;
}
}
@media (min-width: 992px) {
.flex-container {
max-width: 992px;
}
}
@media (min-width: 1200px) {
.flex-container {
max-width: 1200px;
}
}
.flex-row {
box-sizing: border-box;
display: flex;
flex-wrap: wrap;
margin-bottom: 1em;
}
.flex-row--align-v-center {
align-items: center;
}
.flex-row--align-v-bottom {
align-items: flex-end;
}
.flex-row--align-h-center {
justify-content: center;
}
.flex-row--align-h-right {
justify-content: flex-end;
}
.flex-row--reverse {
flex-direction: row-reverse;
}
.flex-col-xs-1,
.flex-col-sm-1,
.flex-col-md-1,
.flex-col-lg-1 {
box-sizing: border-box;
padding: 1em;
width: 100%;
}
@media (min-width: 540px) {
.flex-col-xs-1 {
max-width: calc(100% * (1 / 12));
width: calc(100% * (1 / 12));
}
}
@media (min-width: 768px) {
.flex-col-sm-1 {
max-width: calc(100% * (1 / 12));
width: calc(100% * (1 / 12));
}
}
@media (min-width: 992px) {
.flex-col-md-1 {
max-width: calc(100% * (1 / 12));
width: calc(100% * (1 / 12));
}
}
@media (min-width: 1200px) {
.flex-col-lg-1 {
max-width: calc(100% * (1 / 12));
width: calc(100% * (1 / 12));
}
}
.flex-col-xs-2,
.flex-col-sm-2,
.flex-col-md-2,
.flex-col-lg-2 {
box-sizing: border-box;
padding: 1em;
width: 100%;
}
@media (min-width: 540px) {
.flex-col-xs-2 {
max-width: calc(100% * (2 / 12));
width: calc(100% * (2 / 12));
}
}
@media (min-width: 768px) {
.flex-col-sm-2 {
max-width: calc(100% * (2 / 12));
width: calc(100% * (2 / 12));
}
}
@media (min-width: 992px) {
.flex-col-md-2 {
max-width: calc(100% * (2 / 12));
width: calc(100% * (2 / 12));
}
}
@media (min-width: 1200px) {
.flex-col-lg-2 {
max-width: calc(100% * (2 / 12));
width: calc(100% * (2 / 12));
}
}
.flex-col-xs-3,
.flex-col-sm-3,
.flex-col-md-3,
.flex-col-lg-3 {
box-sizing: border-box;
padding: 1em;
width: 100%;
}
@media (min-width: 540px) {
.flex-col-xs-3 {
max-width: calc(100% * (3 / 12));
width: calc(100% * (3 / 12));
}
}
@media (min-width: 768px) {
.flex-col-sm-3 {
max-width: calc(100% * (3 / 12));
width: calc(100% * (3 / 12));
}
}
@media (min-width: 992px) {
.flex-col-md-3 {
max-width: calc(100% * (3 / 12));
width: calc(100% * (3 / 12));
}
}
@media (min-width: 1200px) {
.flex-col-lg-3 {
max-width: calc(100% * (3 / 12));
width: calc(100% * (3 / 12));
}
}
.flex-col-xs-4,
.flex-col-sm-4,
.flex-col-md-4,
.flex-col-lg-4 {
box-sizing: border-box;
padding: 1em;
width: 100%;
}
@media (min-width: 540px) {
.flex-col-xs-4 {
max-width: calc(100% * (4 / 12));
width: calc(100% * (4 / 12));
}
}
@media (min-width: 768px) {
.flex-col-sm-4 {
max-width: calc(100% * (4 / 12));
width: calc(100% * (4 / 12));
}
}
@media (min-width: 992px) {
.flex-col-md-4 {
max-width: calc(100% * (4 / 12));
width: calc(100% * (4 / 12));
}
}
@media (min-width: 1200px) {
.flex-col-lg-4 {
max-width: calc(100% * (4 / 12));
width: calc(100% * (4 / 12));
}
}
.flex-col-xs-5,
.flex-col-sm-5,
.flex-col-md-5,
.flex-col-lg-5 {
box-sizing: border-box;
padding: 1em;
width: 100%;
}
@media (min-width: 540px) {
.flex-col-xs-5 {
max-width: calc(100% * (5 / 12));
width: calc(100% * (5 / 12));
}
}
@media (min-width: 768px) {
.flex-col-sm-5 {
max-width: calc(100% * (5 / 12));
width: calc(100% * (5 / 12));
}
}
@media (min-width: 992px) {
.flex-col-md-5 {
max-width: calc(100% * (5 / 12));
width: calc(100% * (5 / 12));
}
}
@media (min-width: 1200px) {
.flex-col-lg-5 {
max-width: calc(100% * (5 / 12));
width: calc(100% * (5 / 12));
}
}
.flex-col-xs-6,
.flex-col-sm-6,
.flex-col-md-6,
.flex-col-lg-6 {
box-sizing: border-box;
padding: 1em;
width: 100%;
}
@media (min-width: 540px) {
.flex-col-xs-6 {
max-width: calc(100% * (6 / 12));
width: calc(100% * (6 / 12));
}
}
@media (min-width: 768px) {
.flex-col-sm-6 {
max-width: calc(100% * (6 / 12));
width: calc(100% * (6 / 12));
}
}
@media (min-width: 992px) {
.flex-col-md-6 {
max-width: calc(100% * (6 / 12));
width: calc(100% * (6 / 12));
}
}
@media (min-width: 1200px) {
.flex-col-lg-6 {
max-width: calc(100% * (6 / 12));
width: calc(100% * (6 / 12));
}
}
.flex-col-xs-7,
.flex-col-sm-7,
.flex-col-md-7,
.flex-col-lg-7 {
box-sizing: border-box;
padding: 1em;
width: 100%;
}
@media (min-width: 540px) {
.flex-col-xs-7 {
max-width: calc(100% * (7 / 12));
width: calc(100% * (7 / 12));
}
}
@media (min-width: 768px) {
.flex-col-sm-7 {
max-width: calc(100% * (7 / 12));
width: calc(100% * (7 / 12));
}
}
@media (min-width: 992px) {
.flex-col-md-7 {
max-width: calc(100% * (7 / 12));
width: calc(100% * (7 / 12));
}
}
@media (min-width: 1200px) {
.flex-col-lg-7 {
max-width: calc(100% * (7 / 12));
width: calc(100% * (7 / 12));
}
}
.flex-col-xs-8,
.flex-col-sm-8,
.flex-col-md-8,
.flex-col-lg-8 {
box-sizing: border-box;
padding: 1em;
width: 100%;
}
@media (min-width: 540px) {
.flex-col-xs-8 {
max-width: calc(100% * (8 / 12));
width: calc(100% * (8 / 12));
}
}
@media (min-width: 768px) {
.flex-col-sm-8 {
max-width: calc(100% * (8 / 12));
width: calc(100% * (8 / 12));
}
}
@media (min-width: 992px) {
.flex-col-md-8 {
max-width: calc(100% * (8 / 12));
width: calc(100% * (8 / 12));
}
}
@media (min-width: 1200px) {
.flex-col-lg-8 {
max-width: calc(100% * (8 / 12));
width: calc(100% * (8 / 12));
}
}
.flex-col-xs-9,
.flex-col-sm-9,
.flex-col-md-9,
.flex-col-lg-9 {
box-sizing: border-box;
padding: 1em;
width: 100%;
}
@media (min-width: 540px) {
.flex-col-xs-9 {
max-width: calc(100% * (9 / 12));
width: calc(100% * (9 / 12));
}
}
@media (min-width: 768px) {
.flex-col-sm-9 {
max-width: calc(100% * (9 / 12));
width: calc(100% * (9 / 12));
}
}
@media (min-width: 992px) {
.flex-col-md-9 {
max-width: calc(100% * (9 / 12));
width: calc(100% * (9 / 12));
}
}
@media (min-width: 1200px) {
.flex-col-lg-9 {
max-width: calc(100% * (9 / 12));
width: calc(100% * (9 / 12));
}
}
.flex-col-xs-10,
.flex-col-sm-10,
.flex-col-md-10,
.flex-col-lg-10 {
box-sizing: border-box;
padding: 1em;
width: 100%;
}
@media (min-width: 540px) {
.flex-col-xs-10 {
max-width: calc(100% * (10 / 12));
width: calc(100% * (10 / 12));
}
}
@media (min-width: 768px) {
.flex-col-sm-10 {
max-width: calc(100% * (10 / 12));
width: calc(100% * (10 / 12));
}
}
@media (min-width: 992px) {
.flex-col-md-10 {
max-width: calc(100% * (10 / 12));
width: calc(100% * (10 / 12));
}
}
@media (min-width: 1200px) {
.flex-col-lg-10 {
max-width: calc(100% * (10 / 12));
width: calc(100% * (10 / 12));
}
}
.flex-col-xs-11,
.flex-col-sm-11,
.flex-col-md-11,
.flex-col-lg-11 {
box-sizing: border-box;
padding: 1em;
width: 100%;
}
@media (min-width: 540px) {
.flex-col-xs-11 {
max-width: calc(100% * (11 / 12));
width: calc(100% * (11 / 12));
}
}
@media (min-width: 768px) {
.flex-col-sm-11 {
max-width: calc(100% * (11 / 12));
width: calc(100% * (11 / 12));
}
}
@media (min-width: 992px) {
.flex-col-md-11 {
max-width: calc(100% * (11 / 12));
width: calc(100% * (11 / 12));
}
}
@media (min-width: 1200px) {
.flex-col-lg-11 {
max-width: calc(100% * (11 / 12));
width: calc(100% * (11 / 12));
}
}
.flex-col-xs-12,
.flex-col-sm-12,
.flex-col-md-12,
.flex-col-lg-12 {
box-sizing: border-box;
padding: 1em;
width: 100%;
}
@media (min-width: 540px) {
.flex-col-xs-12 {
max-width: calc(100% * (12 / 12));
width: calc(100% * (12 / 12));
}
}
@media (min-width: 768px) {
.flex-col-sm-12 {
max-width: calc(100% * (12 / 12));
width: calc(100% * (12 / 12));
}
}
@media (min-width: 992px) {
.flex-col-md-12 {
max-width: calc(100% * (12 / 12));
width: calc(100% * (12 / 12));
}
}
@media (min-width: 1200px) {
.flex-col-lg-12 {
max-width: calc(100% * (12 / 12));
width: calc(100% * (12 / 12));
}
}
l’original est codée en scss
$device-xs: 540px;
$device-sm: 768px;
$device-md: 992px;
$device-lg: 1200px;
.flex-container {
box-sizing: border-box;
margin: 0 auto;
padding: 0 1em;
@media (min-width: $device-xs) {
max-width: $device-xs;
}
@media (min-width: $device-sm) {
max-width: $device-sm;
}
@media (min-width: $device-md) {
max-width: $device-md;
}
@media (min-width: $device-lg) {
max-width: $device-lg;
}
}
.flex-row {
box-sizing: border-box;
display: flex;
flex-wrap: wrap;
margin-bottom: 1em;
}
.flex-row--align-v-center {
align-items: center;
}
.flex-row--align-v-bottom {
align-items: flex-end;
}
.flex-row--align-h-center {
justify-content: center;
}
.flex-row--align-h-right {
justify-content: flex-end;
}
.flex-row--reverse {
flex-direction: row-reverse;
}
@for $i from 1 through 12 {
.flex-col-xs-#{$i},
.flex-col-sm-#{$i},
.flex-col-md-#{$i},
.flex-col-lg-#{$i} {
box-sizing: border-box;
padding: 1em;
width: 100%;
}
.flex-col-xs-#{$i} {
@media (min-width: $device-xs) {
max-width: calc(100% * (#{$i} / 12));
width: calc(100% * (#{$i} / 12));
}
}
.flex-col-sm-#{$i} {
@media (min-width: $device-sm) {
max-width: calc(100% * (#{$i} / 12));
width: calc(100% * (#{$i} / 12));
}
}
.flex-col-md-#{$i} {
@media (min-width: $device-md) {
max-width: calc(100% * (#{$i} / 12));
width: calc(100% * (#{$i} / 12));
}
}
.flex-col-lg-#{$i} {
@media (min-width: $device-lg) {
max-width: calc(100% * (#{$i} / 12));
width: calc(100% * (#{$i} / 12));
}
}
}
Mis à jour le Par Copier coller | Dans CSS