Alignment in CSS means controlling where content sits inside its container. It includes horizontal alignment, vertical alignment, text alignment, item alignment, spacing between items, and alignment inside flexbox or grid layouts. Good alignment makes a page feel organized and readable.
CSS has several alignment tools because different layout modes behave differently. The correct property depends on whether you are aligning text, a block element, inline content, flex items, grid items, or the grid tracks themselves. Many alignment problems happen because the right property is used in the wrong layout context.
Text Alignment
The text-align property aligns inline content inside a block. It is mainly used for text, links, inline images, and inline-level elements.
.hero-title {
text-align: center;
}
This centers the text inside the element. It does not center the block element itself. That distinction is important. Text alignment controls content inside the box, not the position of the box in the page.
Centering a Block with margin auto
A block element with a defined width can be centered horizontally using left and right auto margins.
.content-wrapper {
max-width: 1100px;
margin-left: auto;
margin-right: auto;
}
This is a common pattern for page containers. The wrapper stays within a maximum width and remains centered in the viewport.
Inline Alignment with vertical-align
The vertical-align property works with inline-level and table-cell elements. It is often misunderstood because it does not vertically center normal block elements.
.icon {
vertical-align: middle;
}
This can align an inline icon with nearby text. For modern layout centering, flexbox or grid is usually better than relying on vertical-align.
Flexbox Alignment
Flexbox provides some of the most useful alignment tools in CSS. In a row layout, justify-content controls horizontal alignment along the main axis, and align-items controls vertical alignment along the cross axis.
.button-row {
display: flex;
justify-content: center;
align-items: center;
gap: 1rem;
}
This centers the row content and keeps consistent spacing between buttons. If the flex direction changes to column, the axes change too, so the same properties affect different directions.
Grid Alignment
CSS Grid has alignment properties for both items and the overall grid. justify-items aligns items horizontally inside their cells. align-items aligns items vertically inside their cells. place-items is shorthand for both.
.empty-state {
display: grid;
place-items: center;
min-height: 300px;
}
This centers the content both horizontally and vertically. It is a clean pattern for empty states, loading screens, cards, badges, and simple centered components.
justify-content vs justify-items
In grid layouts, justify-content aligns the grid tracks inside the container when there is extra space. justify-items aligns each item inside its own grid cell. These sound similar, but they affect different levels.
| Property | What It Aligns |
|---|---|
| justify-content | the whole grid or flex line along the main axis |
| align-content | multiple rows or tracks along the cross axis |
| justify-items | items inside grid cells horizontally |
| align-items | items inside cells or flex items on cross axis |
| place-items | shorthand for align-items and justify-items |
If an alignment property seems to do nothing, check whether there is extra space to distribute and whether the layout mode supports that property.
align-self and justify-self
Sometimes one item needs different alignment from the rest. The align-self property overrides cross-axis alignment for a single flex or grid item. In grid, justify-self can also align one item horizontally inside its cell.
.special-card {
align-self: start;
justify-self: end;
}
This is useful when one card, icon, badge, or callout needs a different position without changing every item in the container.
Centering Horizontally and Vertically
The most common alignment question is how to center something both horizontally and vertically. Flexbox and grid both solve this cleanly.
.center-with-flex {
display: flex;
justify-content: center;
align-items: center;
}
.center-with-grid {
display: grid;
place-items: center;
}
Flexbox is useful when there are multiple items in a row or column. Grid is very simple when there is one main child that should sit in the center.
Baseline Alignment
Baseline alignment lines up text baselines instead of box edges. This is useful when buttons, icons, labels, or form controls have different heights but their text should visually line up.
.form-row {
display: flex;
align-items: baseline;
gap: 0.75rem;
}
Baseline alignment is less common than center alignment, but it can make mixed text and controls look more polished.
Gap and Alignment
Alignment is not only about centering. Spacing also matters. The gap property works in flexbox and grid and creates consistent space between items.
.stats {
display: flex;
align-items: center;
gap: 1rem;
}
Using gap keeps spacing controlled by the parent. It is usually cleaner than adding margins to each child and then removing margin from the first or last item.
Alignment in Responsive Design
Alignment often changes between desktop and mobile. A desktop row may have items spread apart, while the mobile version may stack items and center them.
.cta {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}
@media (max-width: 700px) {
.cta {
flex-direction: column;
text-align: center;
}
}
This keeps the desktop layout wide and balanced while making the mobile layout readable and centered.
Choosing the Right Alignment Tool
| Goal | CSS Tool |
|---|---|
| Center text | text-align: center |
| Center a page wrapper | margin-inline: auto with max-width |
| Align row items | flexbox align-items |
| Distribute row space | flexbox justify-content |
| Center one child both ways | grid place-items |
| Align item inside grid cell | justify-self or align-self |
The best alignment tool is the one that matches the layout context. Do not use text-align to center block layout. Do not use vertical-align to center normal page sections. Do not use random margins when flexbox or grid can describe the alignment directly.
Logical Alignment Values
Modern CSS includes logical alignment values such as start and end. These can be better than physical values such as left and right because they respect writing direction.
.card-footer {
display: flex;
justify-content: end;
}
In a left-to-right language, end usually means the right side. In a right-to-left language, it can mean the left side. Logical alignment is helpful for websites that may support multiple languages.
Safe and Unsafe Alignment
Some alignment values can use safe or unsafe. Safe alignment avoids placing content in a way that could cause data loss or make content unreachable when space is limited.
.modal-body {
display: flex;
align-items: safe center;
}
This is not needed in every layout, but it is useful to know when dealing with scrollable panels, modals, and components that must remain usable on small screens.
Alignment and Writing Mode
CSS alignment is affected by writing mode and direction. A layout in English, Hindi, or Arabic may not behave exactly the same if physical left and right values are hardcoded everywhere. Logical properties and logical alignment values make layouts more adaptable.
.article-header {
text-align: start;
}
This aligns text to the start side of the writing direction. It behaves like left in left-to-right text and right in right-to-left text.
Debugging Alignment Problems
When alignment does not work, inspect the layout mode first. A flex property will not work unless the parent is a flex container. A grid item property may not work if the element is not a direct grid item. Also check whether the container actually has extra space available.
- Check display on the parent element.
- Check whether the property belongs on the parent or child.
- Check the main axis and cross axis.
- Check width, height, and available free space.
- Check whether text alignment is being confused with box alignment.
- Check responsive rules that override alignment later.
Practical Alignment Patterns
Many real interfaces repeat the same alignment patterns. A header often uses flexbox with justify-content: space-between. A centered empty state often uses grid with place-items: center. A readable article wrapper often uses margin-inline: auto with max-width.
.article {
max-width: 760px;
margin-inline: auto;
}
This centers the article block while keeping line length readable. Alignment is not only about visual beauty. It also affects scanning, readability, and how professional the layout feels.
Alignment in Forms
Forms depend heavily on alignment. Labels, inputs, error messages, and buttons should line up in a way that helps the user scan quickly. CSS Grid is useful when labels and fields need columns, while flexbox is useful for inline controls such as checkboxes and button rows.
.form-row {
display: grid;
grid-template-columns: 160px minmax(0, 1fr);
gap: 0.75rem 1rem;
align-items: center;
}
This creates a label column and an input column. The input column can shrink safely because of minmax(0, 1fr). On mobile, the same row can become a single column so labels sit above inputs.
@media (max-width: 600px) {
.form-row {
grid-template-columns: 1fr;
}
}
Good form alignment reduces confusion. Users should not have to guess which label belongs to which field or where the next action button is located.
When debugging alignment, temporarily add background colors or outlines to the parent and child boxes. This quickly shows whether the problem is text alignment, box size, available space, or the wrong alignment context.
This visual debugging step is simple, but it exposes most alignment mistakes faster than changing random properties one by one.
After the boxes are visible, the correct alignment property is usually obvious.
Then remove the temporary outlines.
Common Alignment Mistakes
- Using text-align to center a block element.
- Expecting vertical-align to center normal block layout.
- Confusing justify-content with align-items after changing flex-direction.
- Using margin hacks instead of flexbox or grid alignment.
- Forgetting that justify-items does not work the same way in flexbox.
- Ignoring responsive alignment changes on small screens.
Alignment in CSS FAQ
How do I center text in CSS?
Use text-align: center on the block that contains the text.
How do I center a div horizontally?
Give it a width or max-width and use auto left and right margins.
How do I center content vertically?
Use flexbox with align-items and justify-content, or grid with place-items: center.
Why is align-items not working?
Check whether the element is a flex or grid container and whether the axis has space for alignment.
Continue learning CSS in order
Follow the topic sequence with the previous and next lesson.