Layouts flexíveis de forma simples e poderosa.
.container {
display: flex;
}
Resultado:
lightbulb
Basta um display: flex no pai e os filhos se alinham automaticamente!
flex-direction: row;
flex-direction: row-reverse;
flex-direction: column;
flex-direction: column-reverse;
flex-start (padrão)
center
flex-end
space-between
space-around
space-evenly
stretch (padrão)
flex-start
center
flex-end
baseline
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
lightbulb Apenas 3 linhas para centralizar qualquer elemento nos dois eixos!
.container {
display: flex;
gap: 16px; /* ambos os eixos */
gap: 16px 24px; /* linha | coluna */
row-gap: 16px; /* só linhas */
column-gap: 24px; /* só colunas */
}
Substitui o uso de margin entre itens. Mais limpo e previsível.
.container {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
Itens quebram para a linha seguinte:
/* Quanto cada item cresce */
.item-a { flex-grow: 0; } /* não cresce */
.item-b { flex-grow: 1; } /* 1 parte */
.item-c { flex-grow: 2; } /* 2 partes */
/* Quanto cada item encolhe */
.item { flex-shrink: 1; } /* encolhe (padrão) */
.fixo { flex-shrink: 0; } /* não encolhe */
/* Tamanho inicial antes de crescer/encolher */
.item { flex-basis: 200px; }
.item { flex-basis: auto; } /* padrão */
/* flex: grow shrink basis */
.item { flex: 1; } /* flex: 1 1 0% */
.item { flex: 0 0 200px; } /* fixo em 200px */
.item { flex: 2 1 auto; } /* cresce 2x */
/* Sobrescreve align-items para um item */
.item-b { align-self: center; }
.item-d { align-self: flex-end; }
/* Reordena visualmente sem mudar o HTML */
.item-1 { order: 3; }
.item-2 { order: 1; }
.item-3 { order: 2; }
nav {
display: flex;
justify-content: space-between;
align-items: center;
}
.cards {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.card {
flex: 1 1 calc(50% - 16px);
}
.sidebar { flex: 0 0 250px; }
.main { flex: 1; }
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.main { flex: 1; }
space-betweenflex-wrap e
gapjustify-content: center e align-items: centerflex: 0 0 250px e flex: 1flex-direction: column e min-height: 100vhCSS Grid: layouts avançados.
display: flex e flex-direction