Why centring can be tricky
In web design, visual balance makes a page look polished and professional, and one of the most common tasks is centring something: text, an image, a button or a whole section. It's a fundamental CSS skill, yet it has a reputation for being surprisingly fiddly.
The reason is that there's no single "centre this" property. The right technique depends on two questions:
- Which direction? Horizontally (left to right), vertically (top to bottom), or both.
- What are you centring? Text inside a box, a box inside its parent, or an element floating over others.
In this post we'll look at the main techniques, each with a live demo. In every demo, the dashed box is the container and the blue box is the item being centred.
New: we've also created a CSS Centering Guide. The CSS Centering Guide is an interactive tool designed to make mastering these techniques simple and visual.
Centring text
Horizontally: text-align
The simplest technique of all: text-align: center centres text, and other inline content such as images and links, within a block element like a <div> or <p>.
.centered-text {
text-align: center;
}
Vertically: line-height (single lines only)
A classic trick for one line of text is to set line-height equal to the container's height. The text sits in the middle of that tall line:
.centered-text {
text-align: center; /* horizontal */
height: 150px;
line-height: 150px; /* vertical: matches the height */
}
Watch out: this only works for a single line. If the text wraps, each line becomes 150px tall and the text spills out of the box. For text that might wrap, use one of the methods below instead.
Vertically: align-content
Modern browsers can now centre content vertically in an ordinary block with align-content: center, no flexbox or grid needed. It works however many lines the text wraps onto:
.centered-text {
height: 150px;
text-align: center;
align-content: center;
}
This is a newer ability, supported in all current major browsers since 2024. In older browsers, the text simply stays at the top.
Centring with flexbox
Flexbox is a CSS layout system for arranging items in a row or column, and it makes centring easy. Turn the container into a flex container, then use justify-content and align-items:
.flex-container {
display: flex;
justify-content: center; /* along the row: horizontal */
align-items: center; /* across the row: vertical */
}
In a row (the default), justify-content handles the horizontal direction and align-items the vertical. If you switch to a column with flex-direction: column, the two swap roles.
Tip: flexbox is ideal when there are several items to centre together, such as a row of buttons, because they stay side by side as a centred group.
Centring with grid
CSS Grid offers the shortest centring code of all. One property, place-items, centres the content in both directions:
.grid-container {
display: grid;
place-items: center; /* horizontal and vertical */
}
place-items is a shorthand for align-items and justify-items. To centre just one item in a grid, leaving the others alone, use place-self: center on that item instead.
Correction: the original version of this post said grid centring uses the grid-template-areas property. That property is for naming areas of a grid layout; the centring itself comes from place-items, as the code shows.
The margin: auto technique
Setting the left and right margins to auto is the classic way to centre a block horizontally. The browser shares the leftover space equally on both sides:
.centered-element {
width: fit-content; /* or a fixed width, such as 300px */
margin: 0 auto;
}
The element needs a width smaller than its container, or there's no leftover space to share. fit-content makes it just wide enough for its content. This is the go-to technique for centring a page's main content column:
.page {
max-width: 1100px;
margin-inline: auto; /* modern shorthand for left and right margins */
}
margin: auto inside flex and grid
Inside a flex or grid container, margin: auto works in both directions, so it can centre an item vertically too:
.container { display: flex; }
.item { margin: auto; }
Centring with absolute positioning
Sometimes an element needs to sit on top of others, such as a play button over a video, or a popup over the page. For these, use position: absolute, with position: relative on the container so the item is positioned inside it.
The translate trick
Move the item's top-left corner to the centre, then pull it back by half its own size:
.container {
position: relative;
}
.centered-item {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Because percentages in translate() are based on the item's own size, this works whatever the item's width and height.
The inset and margin: auto method
A newer alternative stretches the item's position to all four edges with inset: 0, then lets margin: auto centre it in the middle:
.centered-item {
position: absolute;
inset: 0;
margin: auto;
width: fit-content;
height: fit-content;
}
This avoids using transform, which leaves it free for animations, such as scaling the item up on hover.
Which method should you use?
| You want to centre... | Try |
|---|---|
| Text horizontally | text-align: center |
| Text vertically, in a box with a set height | align-content: center |
| A block, such as a page column, horizontally | margin-inline: auto with a width or max-width |
| One item, both directions | Grid with place-items: center |
| A group of items in a row or column | Flexbox with justify-content and align-items |
| An item floating over other content | position: absolute with inset: 0 and margin: auto, or the translate trick |
Still not sure? Our CSS Centering Guide walks you through it: choose what you're centring and in which direction, and it gives you the right code to copy.
Wrapping up
Centring in CSS is all about picking the right tool. Use text-align for text, margin: auto for blocks, grid or flexbox when you need both directions, and absolute positioning for items that sit on top of others. Experiment with the demos above, and you'll soon centre anything with confidence.