CSS 29 💻 align-content and align-self properties
These two properties complete the Flexbox alignment toolkit. align-content controls how multiple lines are distributed along the cross axis, while align-self lets you override the container’s align-items for individual items.
Overview of Properties
| Property | Applies To | Description |
|---|---|---|
align-content | Container | Aligns multiple lines along the cross axis |
align-self | Item | Overrides align-items for a single item |
1. align-content
The align-content property controls the alignment of flex lines along the cross axis. It only works when:
- The container has
flex-wrap: wrap(orwrap-reverse) - There are multiple lines of items
- There is extra space in the container
.container {
display: flex;
flex-wrap: wrap;
height: 400px;
align-content: center;
}
Values
| Value | Description | Visual |
|---|---|---|
stretch | Lines stretch to fill container (default) | Lines fill height |
flex-start | Lines packed at start | [1][2][3][4] |
flex-end | Lines packed at end | [1][2][3][4] |
center | Lines centered | [1][2][3][4] |
space-between | First at start, last at end | [1][2][3][4] |
space-around | Equal space around each line | [1][2][3][4] |
space-evenly | Equal space everywhere | [1][2][3][4] |
Important: align-content has no effect if:
flex-wrap: nowrap(single line)- There’s only one line of items
- There’s no extra space in the container
2. align-self
The align-self property allows an individual flex item to override the container’s align-items value.
.item2 {
align-self: flex-start; /* This item aligns differently */
}
Values
Same as align-items:
| Value | Description |
|---|---|
auto | Inherits from parent’s align-items (default) |
stretch | Stretches to fill container |
flex-start | Aligns to the start |
flex-end | Aligns to the end |
center | Centers the item |
baseline | Aligns to text baseline |
align-content vs align-items
| Aspect | align-content | align-items |
|---|---|---|
| Applies to | Multiple lines | Items within a line |
| When it works | flex-wrap: wrap + multiple lines | Always (single or multi-line) |
| Axis | Cross axis | Cross axis |
| Controls | Distribution of lines | Alignment of items |
| Number of lines | Only multi-line | Single or multi-line |
Visual difference:
align-items: center align-content: center
┌─────────────────┐ ┌─────────────────┐
│ │ │ │
│ [1] [2] [3] │ │ [1] [2] [3] │
│ │ │ [4] [5] [6] │
│ │ │ │
└─────────────────┘ └─────────────────┘
Items centered Lines centered
within each line as a group
Complete Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>align-content and align-self Properties</title>
<style>
*, *::before, *::after {
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background: #f8f9fa;
line-height: 1.6;
}
h1 {
color: #007bff;
border-bottom: 3px solid #007bff;
padding-bottom: 10px;
}
h2 {
color: #28a745;
border-left: 4px solid #28a745;
padding-left: 15px;
margin-top: 30px;
}
h3 {
color: #333;
margin-top: 20px;
}
section {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
margin: 20px 0;
}
.code-block {
background: #1e1e1e;
color: #d4d4d4;
padding: 15px;
border-radius: 8px;
overflow-x: auto;
font-family: 'Courier New', monospace;
font-size: 0.9rem;
line-height: 1.8;
margin: 10px 0;
}
.reference-table {
width: 100%;
border-collapse: collapse;
margin: 15px 0;
}
.reference-table th,
.reference-table td {
padding: 10px;
border: 1px solid #ddd;
text-align: left;
}
.reference-table th {
background: #007bff;
color: white;
}
.reference-table tr:nth-child(even) {
background: #f8f9fa;
}
.note {
font-size: 0.9rem;
color: #6c757d;
margin-top: 5px;
}
.highlight {
background: #ffc107;
color: #333;
padding: 2px 6px;
border-radius: 4px;
font-weight: bold;
}
/* ====== FLEX CONTAINERS ====== */
.container {
display: flex;
flex-wrap: wrap;
height: 300px;
border: 2px solid #333;
margin: 10px 0;
padding: 5px;
background: #e9ecef;
border-radius: 8px;
gap: 5px;
}
.item {
background: #007bff;
color: white;
padding: 15px;
margin: 3px;
text-align: center;
width: 200px;
border-radius: 6px;
font-weight: bold;
display: flex;
align-items: center;
justify-content: center;
}
.item:nth-child(even) {
background: #28a745;
}
.item:nth-child(3n) {
background: #6c5ce7;
}
/* align-content demos */
.ac-stretch {
align-content: stretch;
}
.ac-start {
align-content: flex-start;
}
.ac-end {
align-content: flex-end;
}
.ac-center {
align-content: center;
}
.ac-between {
align-content: space-between;
}
.ac-around {
align-content: space-around;
}
.ac-evenly {
align-content: space-evenly;
}
/* align-self demo */
.container-self {
display: flex;
height: 300px;
border: 2px solid #333;
margin: 10px 0;
padding: 5px;
background: #e9ecef;
border-radius: 8px;
gap: 5px;
}
.container-self .item {
width: auto;
flex: 1;
min-width: 80px;
}
.container-self .item1 {
align-self: stretch;
background: #007bff;
}
.container-self .item2 {
align-self: flex-start;
background: #28a745;
}
.container-self .item3 {
align-self: flex-end;
background: #dc3545;
}
.container-self .item4 {
align-self: center;
background: #ffc107;
color: #333;
}
.container-self .item5 {
align-self: baseline;
background: #17a2b8;
}
/* Comparison grid */
.comparison-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
}
.comparison-item {
text-align: center;
}
.comparison-item h4 {
margin-bottom: 5px;
color: #007bff;
}
.comparison-item .container {
margin: 5px 0;
height: 200px;
}
.comparison-item .item {
width: auto;
flex: 1;
min-width: 60px;
font-size: 0.8rem;
padding: 10px 5px;
}
/* Visual explanation */
.visual-explanation {
display: flex;
flex-direction: column;
align-items: center;
gap: 15px;
margin: 20px 0;
}
.visual-row {
display: flex;
align-items: center;
gap: 10px;
width: 100%;
max-width: 700px;
}
.visual-label {
font-weight: bold;
color: #007bff;
min-width: 160px;
text-align: right;
font-size: 0.85rem;
}
.visual-bar {
flex: 1;
height: 120px;
background: #e9ecef;
border-radius: 6px;
position: relative;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 10px;
border: 1px solid #ddd;
gap: 5px;
}
.visual-bar .line {
display: flex;
gap: 5px;
padding: 5px 10px;
background: #007bff;
border-radius: 4px;
color: white;
font-size: 0.7rem;
font-weight: bold;
}
.visual-bar .line:nth-child(even) {
background: #28a745;
}
</style>
</head>
<body>
<h1>align-content and align-self Properties</h1>
<!-- ====== 1. ALIGN-CONTENT ====== -->
<section>
<h2>1. align-content</h2>
<p>Aligns <strong>multiple lines</strong> along the cross axis. Only works with <code>flex-wrap: wrap</code> and multiple lines.</p>
<h3>align-content: stretch (default)</h3>
<div class="container ac-stretch">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
<p class="note">Lines stretch to fill the container's height.</p>
<h3>align-content: flex-start</h3>
<div class="container ac-start">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
<h3>align-content: flex-end</h3>
<div class="container ac-end">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
<h3>align-content: center</h3>
<div class="container ac-center">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
<h3>align-content: space-between</h3>
<div class="container ac-between">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
<h3>align-content: space-around</h3>
<div class="container ac-around">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
<h3>align-content: space-evenly</h3>
<div class="container ac-evenly">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
<div class="code-block">
.container {
display: flex;
flex-wrap: wrap;
height: 300px;
align-content: stretch; /* Default — fills height */
align-content: flex-start; /* Packed at start */
align-content: flex-end; /* Packed at end */
align-content: center; /* Centered */
align-content: space-between;/* First at start, last at end */
align-content: space-around; /* Equal space around lines */
align-content: space-evenly; /* Equal space everywhere */
}
</div>
</section>
<!-- ====== 2. ALIGN-SELF ====== -->
<section>
<h2>2. align-self</h2>
<p>Overrides <code>align-items</code> for an <strong>individual item</strong>.</p>
<div class="container-self">
<div class="item item1">align-self: stretch</div>
<div class="item item2">align-self: flex-start</div>
<div class="item item3">align-self: flex-end</div>
<div class="item item4">align-self: center</div>
<div class="item item5">align-self: baseline</div>
</div>
<p class="note">Each item has a different <code>align-self</code> value, overriding the container's default alignment.</p>
<div class="code-block">
.item1 { align-self: stretch; } /* Stretches to fill */
.item2 { align-self: flex-start; } /* Top */
.item3 { align-self: flex-end; } /* Bottom */
.item4 { align-self: center; } /* Center */
.item5 { align-self: baseline; } /* Text baseline */
</div>
</section>
<!-- ====== 3. SIDE-BY-SIDE COMPARISON ====== -->
<section>
<h2>3. Side-by-Side Comparison</h2>
<h3>align-content (multi-line distribution)</h3>
<div class="comparison-grid">
<div class="comparison-item">
<h4>stretch (default)</h4>
<div class="container ac-stretch">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</div>
<div class="comparison-item">
<h4>flex-start</h4>
<div class="container ac-start">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</div>
<div class="comparison-item">
<h4>center</h4>
<div class="container ac-center">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</div>
<div class="comparison-item">
<h4>space-between</h4>
<div class="container ac-between">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</div>
<div class="comparison-item">
<h4>space-around</h4>
<div class="container ac-around">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</div>
<div class="comparison-item">
<h4>space-evenly</h4>
<div class="container ac-evenly">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</div>
</div>
</section>
<!-- ====== 4. VISUAL EXPLANATION ====== -->
<section>
<h2>4. Visual Explanation</h2>
<p>See how <code>align-content</code> distributes multiple lines.</p>
<div class="visual-explanation">
<div class="visual-row">
<div class="visual-label">flex-start:</div>
<div class="visual-bar" style="justify-content: flex-start;">
<div class="line">Line 1</div>
<div class="line">Line 2</div>
<div class="line">Line 3</div>
</div>
</div>
<div class="visual-row">
<div class="visual-label">center:</div>
<div class="visual-bar" style="justify-content: center;">
<div class="line">Line 1</div>
<div class="line">Line 2</div>
<div class="line">Line 3</div>
</div>
</div>
<div class="visual-row">
<div class="visual-label">flex-end:</div>
<div class="visual-bar" style="justify-content: flex-end;">
<div class="line">Line 1</div>
<div class="line">Line 2</div>
<div class="line">Line 3</div>
</div>
</div>
<div class="visual-row">
<div class="visual-label">space-between:</div>
<div class="visual-bar" style="justify-content: space-between;">
<div class="line">Line 1</div>
<div class="line">Line 2</div>
<div class="line">Line 3</div>
</div>
</div>
<div class="visual-row">
<div class="visual-label">space-around:</div>
<div class="visual-bar" style="justify-content: space-around;">
<div class="line">Line 1</div>
<div class="line">Line 2</div>
<div class="line">Line 3</div>
</div>
</div>
<div class="visual-row">
<div class="visual-label">space-evenly:</div>
<div class="visual-bar" style="justify-content: space-evenly;">
<div class="line">Line 1</div>
<div class="line">Line 2</div>
<div class="line">Line 3</div>
</div>
</div>
</div>
</section>
<!-- ====== 5. REFERENCE TABLES ====== -->
<section>
<h2>5. Reference Tables</h2>
<h3>align-content Values</h3>
<table class="reference-table">
<tr>
<th>Value</th>
<th>Description</th>
<th>Space at Edges</th>
</tr>
<tr>
<td><code>stretch</code></td>
<td>Lines stretch to fill container (default)</td>
<td>—</td>
</tr>
<tr>
<td><code>flex-start</code></td>
<td>Lines packed at start</td>
<td>No</td>
</tr>
<tr>
<td><code>flex-end</code></td>
<td>Lines packed at end</td>
<td>No</td>
</tr>
<tr>
<td><code>center</code></td>
<td>Lines centered</td>
<td>Equal at both ends</td>
</tr>
<tr>
<td><code>space-between</code></td>
<td>Equal space between lines</td>
<td>No</td>
</tr>
<tr>
<td><code>space-around</code></td>
<td>Equal space around lines</td>
<td>Half space</td>
</tr>
<tr>
<td><code>space-evenly</code></td>
<td>Equal space everywhere</td>
<td>Full space</td>
</tr>
</table>
<h3>align-self Values</h3>
<table class="reference-table">
<tr>
<th>Value</th>
<th>Description</th>
</tr>
<tr>
<td><code>auto</code></td>
<td>Inherits from parent's align-items (default)</td>
</tr>
<tr>
<td><code>stretch</code></td>
<td>Stretches to fill container</td>
</tr>
<tr>
<td><code>flex-start</code></td>
<td>Aligns to the start</td>
</tr>
<tr>
<td><code>flex-end</code></td>
<td>Aligns to the end</td>
</tr>
<tr>
<td><code>center</code></td>
<td>Centers the item</td>
</tr>
<tr>
<td><code>baseline</code></td>
<td>Aligns to text baseline</td>
</tr>
</table>
<h3>align-content vs align-items</h3>
<table class="reference-table">
<tr>
<th>Aspect</th>
<th>align-content</th>
<th>align-items</th>
</tr>
<tr>
<td><strong>Applies to</strong></td>
<td>Multiple lines</td>
<td>Items within a line</td>
</tr>
<tr>
<td><strong>When it works</strong></td>
<td><code>flex-wrap: wrap</code> + multiple lines</td>
<td>Always</td>
</tr>
<tr>
<td><strong>Controls</strong></td>
<td>Distribution of lines</td>
<td>Alignment of items</td>
</tr>
<tr>
<td><strong>Number of lines</strong></td>
<td>Multi-line only</td>
<td>Single or multi-line</td>
</tr>
</table>
</section>
<!-- ====== 6. BEST PRACTICES ====== -->
<section>
<h2>6. Best Practices</h2>
<div style="background: #d4edda; padding: 20px; border-radius: 8px; border-left: 4px solid #28a745;">
<h3 style="margin-top: 0; color: #155724;">✅ Do This:</h3>
<ul>
<li>Use <code>align-content</code> only with <code>flex-wrap: wrap</code></li>
<li>Use <code>align-self</code> to override alignment for specific items</li>
<li>Use <code>align-content: center</code> to center multiple lines vertically</li>
<li>Use <code>align-self: flex-start</code> for items that need to stick to the top</li>
<li>Remember that <code>align-content</code> has no effect with a single line</li>
</ul>
</div>
<div style="background: #f8d7da; padding: 20px; border-radius: 8px; border-left: 4px solid #dc3545; margin-top: 15px;">
<h3 style="margin-top: 0; color: #721c24;">❌ Don't Do This:</h3>
<ul>
<li>Don't use <code>align-content</code> without <code>flex-wrap: wrap</code></li>
<li>Don't confuse <code>align-content</code> (lines) with <code>align-items</code> (items)</li>
<li>Don't forget that <code>align-self</code> overrides <code>align-items</code> for one item</li>
<li>Don't use <code>align-content</code> on single-line containers</li>
</ul>
</div>
</section>
</body>
</html>
Quick Reference
| Property | Applies To | Description |
|---|---|---|
align-content | Container | Aligns multiple lines along the cross axis |
align-self | Item | Overrides align-items for one item |
align-content Values
| Value | Description |
|---|---|
stretch | Lines stretch to fill container (default) |
flex-start | Lines packed at start |
flex-end | Lines packed at end |
center | Lines centered |
space-between | First at start, last at end |
space-around | Equal space around lines |
space-evenly | Equal space everywhere |
align-content vs align-items
| Aspect | align-content | align-items |
|---|---|---|
| Applies to | Multiple lines | Items within a line |
| When it works | flex-wrap: wrap + multiple lines | Always |
| Controls | Distribution of lines | Alignment of items |
| Single line | No effect | Works |
Best Practices
✅ Do This:
/* Center multiple lines vertically */
.container {
display: flex;
flex-wrap: wrap;
height: 400px;
align-content: center; /* Centers lines as a group */
}
/* Override alignment for one item */
.special-item {
align-self: flex-start; /* This item aligns to the top */
}
/* Distribute lines evenly */
.container {
display: flex;
flex-wrap: wrap;
align-content: space-between;
}
❌ Don’t Do This:
/* Don't use align-content without wrap */
.container {
display: flex;
flex-wrap: nowrap; /* align-content has NO effect */
align-content: center;
}
/* Don't confuse align-content and align-items */
.container {
align-content: center; /* Centers LINES, not items */
align-items: center; /* Centers ITEMS within lines */
}
Pro Tip: align-content is often confused with align-items. The key difference: align-items aligns items within each line, while align-content aligns the lines themselves as a group. Remember: align-content only works when you have multiple lines (i.e., flex-wrap: wrap and enough items to wrap). And align-self is your escape hatch — it lets a single item break away from the container’s align-items rule and align itself independently!
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!