CHAPTER 27
CSS Align
Learn different methods for aligning text and elements horizontally and vertically.
01. What is CSS Align?
CSS provides different properties and layout systems for alignment.
text-align is commonly used for horizontal text alignment.
Flexbox provides powerful horizontal and vertical alignment using justify-content and align-items.
02. Syntax
text-align: center;
display: flex;
justify-content: center;
align-items: center;
03. Example
CSS / HTML Example
AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>
.container {
height: 300px;
display: flex;
justify-content: center;
align-items: center;
background: #eff6ff;
}
.card {
padding: 30px;
background: #2563eb;
color: white;
}
</style>
</head>
<body>
<div class="container">
<div class="card">
Centered Content
</div>
</div>
</body>
</html>
04. Important Points
✓
text-align controls text alignment.
✓
justify-content controls alignment along the main axis in Flexbox.
✓
align-items controls alignment along the cross axis.
✓
Flexbox is one of the best tools for modern alignment.
05. Common Mistakes
⚠
Trying to vertically center everything using margins only.
⚠
Confusing justify-content with align-items.
⚠
Forgetting that Flexbox alignment depends on flex-direction.
06. Real World Use
Alignment is used in navigation bars, hero sections, cards, buttons, dashboards and responsive layouts.
Ready to Practice? 🚀
Open the AIFA CSS Editor and practice this chapter live.
Try It Yourself →
Practice Exercise
Center a Card
Create a container with height 300px and perfectly center a card horizontally and vertically using Flexbox.