Flexbox Layout Patterns
Demonstrates CSS Flexbox for one-dimensional layouts including centering, space distribution, wrapping, and responsive navigation patterns.
Code
/* Center content perfectly */
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Responsive navigation bar */
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 1rem;
padding: 1rem;
}
.navbar .menu {
display: flex;
gap: 1.5rem;
flex-wrap: wrap;
}
/* Holy grail layout */
.layout {
display: flex;
min-height: 100vh;
}
.layout .sidebar {
flex: 0 0 200px;
}
.layout .main {
flex: 1;
display: flex;
flex-direction: column;
}
/* Card grid with wrapping */
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card-grid .card {
flex: 1 1 300px;
}
/* Sticky footer */
.page {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.page .content {
flex: 1 0 auto;
}
.page .footer {
flex-shrink: 0;
}
/* Equal height columns */
.columns {
display: flex;
align-items: stretch;
gap: 1rem;
}
.columns > * {
flex: 1;
}Flexbox handles one-dimensional layouts. justify-content controls main-axis alignment, align-items controls cross-axis. flex: 1 1 300px sets grow, shrink, and basis. flex-wrap enables wrapping. gap replaces margins for spacing. Common patterns: centering, navigation, holy grail, card grids, sticky footer, and equal-height columns.
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.CSS Flexbox PlaygroundInteractively test CSS flexbox container and item properties with a live preview. Adjust justify-content, align-items, flex-direction, and flex-grow to see layout changes.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.
Report a problem with this exampleOpens a pre-filled issue on GitHub.