CSS Alignment
1. Introduction to CSS Alignment
CSS provides several properties for aligning elements. You can align text, images, divs, buttons, and other components both horizontally and vertically.
Common Alignment Methods
- text-align
- margin: auto
- vertical-align
- Flexbox
- Grid
.container{
text-align:center;
}
2. Horizontal Text Alignment (text-align)
The text-align property is used to align inline content such as text and inline elements.
h1{
text-align:center;
}
p{
text-align:justify;
}
| Value | Description |
|---|---|
| left | Aligns text to the left. |
| center | Centers the text. |
| right | Aligns text to the right. |
| justify | Stretches text evenly. |
3. Center Aligning Block Elements (margin: auto)
To horizontally center a block-level element, specify a width and use margin: auto;.
.box{
width:300px;
margin:auto;
}
| Property | Purpose |
|---|---|
| width | Defines the element's width. |
| margin:auto | Centers the element horizontally. |
.card{
width:400px;
margin:auto;
}
4. Vertical Alignment (vertical-align)
The vertical-align property aligns inline, inline-block, and table-cell elements vertically.
img{
vertical-align:middle;
}
| Value | Description |
|---|---|
| top | Aligns to the top. |
| middle | Centers vertically. |
| bottom | Aligns to the bottom. |
| baseline | Aligns with text baseline. |
<div>.
5. Aligning with Flexbox
Flexbox provides one of the easiest ways to align items horizontally and vertically.
.container{
display:flex;
justify-content:center;
align-items:center;
height:300px;
}
| Property | Purpose |
|---|---|
| display:flex | Creates a flex container. |
| justify-content:center | Centers items horizontally. |
| align-items:center | Centers items vertically. |
.container{
display:flex;
justify-content:center;
align-items:center;
}
6. Aligning with CSS Grid
CSS Grid provides another powerful way to align elements. Using place-items, justify-items, and align-items, you can easily position content inside a grid container.
.container{
display:grid;
place-items:center;
height:300px;
}
| Property | Purpose |
|---|---|
| display:grid | Creates a grid container. |
| place-items:center | Centers items horizontally and vertically. |
| justify-items | Horizontal alignment. |
| align-items | Vertical alignment. |
7. Centering an Element Horizontally & Vertically
Flexbox makes it very easy to center an element both horizontally and vertically.
.container{
display:flex;
justify-content:center;
align-items:center;
height:100vh;
}
| Property | Function |
|---|---|
| justify-content:center | Horizontal centering. |
| align-items:center | Vertical centering. |
| height:100vh | Uses full screen height. |
8. Image Alignment
Images can be aligned using different CSS techniques, depending on your layout requirements.
img{
display:block;
margin:auto;
}
| Method | Use |
|---|---|
| margin:auto | Center block images. |
| text-align:center | Center inline images. |
| Flexbox | Responsive image alignment. |
.image-box{
text-align:center;
}
9. Practical Alignment Examples
Example 1: Center a Button
.button-box{
text-align:center;
}
Example 2: Center a Card
.card{
width:350px;
margin:auto;
}
Example 3: Center Content
.container{
display:flex;
justify-content:center;
align-items:center;
}
10. CSS Alignment Comparison Table
| Method | Best Used For |
|---|---|
| text-align | Text and inline elements. |
| margin:auto | Center block elements. |
| vertical-align | Inline and table-cell elements. |
| Flexbox | Horizontal & vertical centering. |
| Grid | Complex layouts and centering. |
.container{
display:flex;
justify-content:center;
align-items:center;
}
11. CSS Alignment Best Practices
Following proper alignment techniques makes webpages clean, responsive, and easy to read.
- Use
text-alignfor aligning text. - Use
margin:auto;to center block elements. - Use Flexbox for horizontal and vertical centering.
- Use CSS Grid for complex layouts.
- Keep spacing consistent throughout the webpage.
- Avoid mixing multiple alignment techniques unnecessarily.
- Test alignment on different screen sizes.
- Write clean and readable CSS code.
12. Common Alignment Mistakes
| Mistake | Correct Practice |
|---|---|
| Using margin:auto without width. | Specify the element width first. |
| Using vertical-align on block elements. | Use Flexbox or Grid instead. |
| Forgetting display:flex. | Apply display:flex; before using Flexbox properties. |
| Using text-align for block elements. | Use margin:auto or Flexbox. |
| Not checking responsiveness. | Test layouts on mobile and desktop. |
❌ Incorrect
.box{
justify-content:center;
}
✔ Correct
.box{
display:flex;
justify-content:center;
}
13. Browser Support
CSS alignment properties such as text-align, margin:auto, Flexbox, and Grid are supported by all modern browsers.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
14. CSS Alignment Interview Questions
- What is CSS alignment?
- What is the purpose of
text-align? - How do you center a block element?
- When should you use
margin:auto? - What is the difference between Flexbox and Grid?
- Which property centers items horizontally in Flexbox?
- Which property centers items vertically in Flexbox?
- What does
vertical-aligndo? - Can
vertical-alignbe used on block elements? - Which alignment method is recommended for modern websites?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Alignment.
- ✔ text-align property.
- ✔ margin:auto.
- ✔ vertical-align.
- ✔ Flexbox alignment.
- ✔ CSS Grid alignment.
- ✔ Horizontal and vertical centering.
- ✔ Image alignment.
- ✔ Practical examples.
- ✔ Best practices and common mistakes.
Quick Quiz
Which Flexbox property centers items horizontally?