Step 1 Pick what to center
Choose a line of text, a paragraph, a heading, a button, an image, a div or a table. Type your own text in Your text if you like (it’s escaped, so tags show up as plain text).
Tutorials · Live examples
Short answer: add text-align: center to the element that holds the text. To center something both
ways, give its parent a height and display: flex; justify-content: center; align-items: center;.
Try every method below and copy the code.
Center Text Tool
Leave it empty to use the sample text.
.container {
display: flex;
justify-content: center; /* left to right */
align-items: center; /* top to bottom */
height: 200px; /* room to move up and down */
outline: 2px dashed #94a3b8; /* only so you can see the box */
}
.item {
margin: 0;
font-size: 18px;
}
body {
margin: 0;
padding: 16px;
font-family: system-ui, sans-serif;
color: #111827;
} <div class="container">
<p class="item">Hello, I’m centered!</p>
</div> Want to keep going? Paste the full page into the HTML Editor and build on it.
For text, use text-align: center on the element that holds it. To put anything in the middle of a box
both ways, make the box a flexbox, center it on both axes and give the box a height. These two snippets cover most
cases:
<style>
.note {
text-align: center;
}
</style>
<p class="note">This line is centered.</p> <style>
.box {
display: flex;
justify-content: center; /* left to right */
align-items: center; /* top to bottom */
height: 120px;
}
</style>
<div class="box">
<p>Right in the middle</p>
</div> The dashed line in each preview only shows the edge of the outer box. It isn’t part of the code. Want to play with an example? Copy it and paste it into the HTML Editor.
Almost every “why won’t it center?” question comes down to this. HTML elements are either inline or block:
<span>,
<a>, <img>, <button>. You center them with
text-align: center on their parent.
<div>, <p>,
<h1> to <h6>, <table>. text-align doesn’t
move a block itself, only the text inside it. You center a block with margin: 0 auto (plus a width),
or with flexbox or grid on its parent.
line-height trick, and the parent needs a height.
Choose a line of text, a paragraph, a heading, a button, an image, a div or a table. Type your own text in Your text if you like (it’s escaped, so tags show up as plain text).
Click a spot in the 3×3 grid: left, center or right, and top, middle or bottom. The middle square is the true center. Drag the Box height slider to give it more or less room up and down.
Switch between text-align, margin: 0 auto, Flexbox, Grid, Absolute + transform and
line-height. Each one says if it can’t do what you picked, and why, and offers a method that can.
The preview always shows what the code really does, even when it doesn’t center.
The lines that do the centering are in green in the code. Copy the HTML, the CSS or the full page, or download it as an .html file. Your choices are remembered in this browser for next time.
text-align sets where each line of text sits inside its element: left (the default for
English), center, right or justify. Put it on the block that holds the text, like
a <p>, a heading, a <div> or a table cell. Everything inside follows, because
text-align is inherited.
<style>
.left { text-align: left; }
.center { text-align: center; }
.right { text-align: right; }
</style>
<div>
<p class="left">Left (the default)</p>
<p class="center">Center</p>
<p class="right">Right</p>
</div> <p style="text-align: center;">
Centered with a style attribute
</p>
The inline style attribute is handy for a quick fix or an email, but a class in your stylesheet is easier
to change later, because you can update every centered paragraph in one place.
Vertical centering needs two things: a parent that’s taller than the text, and a method that can move things up and down. Here are four that work. Flexbox or grid is the right pick almost every time.
<style>
.box {
display: flex;
align-items: center;
justify-content: center;
height: 110px;
}
</style>
<div class="box">Flexbox: in the middle</div> <style>
.box {
display: grid;
place-items: center;
height: 110px;
}
</style>
<div class="box">Grid: in the middle</div> <style>
.bar {
height: 56px;
line-height: 56px; /* same as the height */
text-align: center;
background: #0b0f19;
color: #a3e635;
}
</style>
<div class="bar">One line, in the middle</div> <style>
.outer {
display: table;
width: 100%;
height: 110px;
}
.inner {
display: table-cell;
vertical-align: middle;
text-align: center;
}
</style>
<div class="outer">
<div class="inner">Table-cell: the old way</div>
</div> align-items moves things up and down, justify-content left and right.place-items: center does both in one line.vertical-align: middle works in table cells. It was the usual trick before
flexbox. You don’t need it any more, but you’ll still see it in older code and in HTML emails.
A <div> is a block, so it fills the whole row unless you give it a width. Once it has one,
margin: 0 auto splits the free space evenly on the left and right, and the div sits in the middle. To
center it up and down as well, put grid or flexbox on the parent instead.
<style>
.card {
width: 220px;
margin: 0 auto;
padding: 16px;
background: #0b0f19;
color: #fff;
border-radius: 8px;
}
</style>
<div class="page">
<div class="card">I’m a centered div</div>
</div> <style>
.page {
display: grid;
place-items: center;
height: 140px;
}
.card {
width: 220px;
padding: 16px;
background: #0b0f19;
color: #fff;
border-radius: 8px;
}
</style>
<div class="page">
<div class="card">Centered both ways</div>
</div>
💡 For a page column that shrinks on phones, use max-width instead of width:
max-width: 700px; margin: 0 auto;. It’s 700px on a big screen and the full width on a small one.
An <img> is inline, so it behaves like a word in a line. That gives you two easy ways: turn it into a
block and use auto margins, or leave it inline and put text-align: center on its parent. Both give the
same result.
<style>
.photo {
display: block;
margin: 0 auto;
}
</style>
<div>
<img class="photo" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='72'%3E%3Crect width='120' height='72' rx='8' fill='%23a3e635'/%3E%3C/svg%3E" width="120" height="72" alt="A green box">
</div> <style>
.pictures {
text-align: center;
}
</style>
<div class="pictures">
<img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='72'%3E%3Crect width='120' height='72' rx='8' fill='%23a3e635'/%3E%3C/svg%3E" width="120" height="72" alt="A green box">
</div>
For an image in the middle of a box both ways, use the flexbox or grid parent from above. The old
align="center" attribute on images doesn’t exist in HTML5, so skip it.
A <button> is inline-block, so text-align: center on its parent is all you need. For a
row of buttons, flexbox is nicer: gap keeps the space between them even, and you can center them up and
down too.
<style>
.actions {
text-align: center;
}
</style>
<div class="actions">
<button type="button">Sign up</button>
</div> <style>
.actions {
display: flex;
justify-content: center;
align-items: center;
gap: 8px;
height: 90px;
}
</style>
<div class="actions">
<button type="button">Cancel</button>
<button type="button">Save</button>
</div>
A table is a block that shrinks to fit its content, so margin: 0 auto centers it without setting a
width. text-align: center on the cells centers the text inside them. Need a table to start from? Build
one in the HTML Table Generator.
<style>
.prices {
margin: 0 auto;
border-collapse: collapse;
}
.prices th,
.prices td {
padding: 6px 14px;
border: 1px solid #cbd5e1;
text-align: center;
}
</style>
<div>
<table class="prices">
<tr><th>Size</th><th>Price</th></tr>
<tr><td>Small</td><td>$3</td></tr>
<tr><td>Large</td><td>$5</td></tr>
</table>
</div>
You might see <table align="center"> in old code. It’s obsolete, so use the CSS above instead.
Both center anything in every browser in use today. A simple rule of thumb:
| Use | When | Code on the parent |
|---|---|---|
| Grid | One item, centered both ways. It’s the shortest code. | display: grid; place-items: center; |
| Flexbox | A row (or column) of several items, like buttons, icons or a logo and a menu. | display: flex; justify-content: center; align-items: center; |
| margin: auto | Inside a flex or grid parent, margin: auto on the child centers it both ways too. | display: flex; + child margin: auto; |
<style>
.box {
display: flex;
height: 110px;
}
.box p {
margin: auto; /* works inside flex */
}
</style>
<div class="box">
<p>margin: auto in a flex box</p>
</div>
When text has to sit on top of something, like a caption over a banner or a photo, use absolute positioning.
top: 50% and left: 50% put the item’s top-left corner in the middle, then
transform: translate(-50%, -50%) pulls it back by half its own width and height. The parent needs
position: relative so the item is placed inside it.
<style>
.banner {
position: relative;
height: 120px;
background: #0b0f19;
}
.banner p {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
margin: 0;
color: #a3e635;
}
</style>
<div class="banner">
<p>Sitting on top</p>
</div> The catch: an absolute item doesn’t take up space, so the parent won’t grow around it. Give the parent a height, and keep this method for overlays. For normal layout, flexbox and grid are easier.
<center> is from the early days of HTML. It’s obsolete in HTML5: browsers still show it centered
so old pages don’t break, but the HTML Validator flags it and it mixes looks into your
HTML. Use CSS instead. It does the same job and you can change it in one place.
<style>
.title {
text-align: center;
}
</style>
<div>
<center>Old: the center tag</center>
<p class="title">New: text-align in CSS</p>
</div> <span style="text-align: center"> does
nothing, because a span is only as wide as its text. Put text-align on the parent block, like the
<p> or <div>.
width or max-width. On an image or a span, add display: block
first.
height or min-height (for a full screen,
min-height: 100vh).
justify-content and align-items do
nothing on their own. They need display: flex (or grid) on the same element, and that element must be
the parent of the thing you want to move.
<p> and headings have a top and bottom margin, which
can push text a bit off center. Set margin: 0 on them when it matters.
<style>
.wrong { text-align: center; } /* on a span: does nothing */
.right { text-align: center; } /* on the p: works */
</style>
<div>
<p><span class="wrong">Not centered</span></p>
<p class="right">Centered</p>
</div> Long stylesheet getting messy after all this? Tidy it with the CSS Formatter, or check it for typos with the CSS Validator.
Add text-align: center to the element that holds the text, for example p { text-align: center; } in your CSS, or <p style="text-align: center;"> right in the HTML. It centers every line of text inside that element from left to right.
Give the parent a height and make it a flexbox: display: flex; align-items: center; height: 200px;. Add justify-content: center as well to center it left to right too. Grid does the same with display: grid; place-items: center;. For a single line of text you can also set line-height to the same value as the height.
Give the div a width and set margin: 0 auto, for example .card { width: 300px; margin: 0 auto; }. That centers it from left to right. To center it both ways, put display: flex; justify-content: center; align-items: center; and a height on its parent instead.
text-align only moves inline content: text, images, buttons and other inline-block things. A div or a table is a block, so it stays where it is and only the words inside it move. Use margin: 0 auto on the div or table (a div also needs a width), or make the parent a flexbox.
An image is inline, so the easiest way is text-align: center on its parent. Or turn the image into a block and use auto margins: img { display: block; margin: 0 auto; }. To center it up and down too, use a flex or grid parent with a height.
Put text-align: center on the element around the button, like <div style="text-align: center;"><button>Send</button></div>. If you have several buttons or want them in the middle of a box both ways, use display: flex; justify-content: center; align-items: center; with a gap on the parent.
It still works in browsers, but it has been obsolete since HTML5 and validators flag it. Use CSS instead: text-align: center for text and inline things, margin: 0 auto for blocks, or flexbox and grid when you need both directions.
Both work in every browser in use today. Grid is the shortest for one item: display: grid; place-items: center;. Flexbox is the better pick when you center a row of several items, like buttons or icons, because it keeps them side by side with justify-content and gap.
Usually the parent has no height, so it’s exactly as tall as the text and there’s no room to move. Give the parent a height (or min-height). Also check that display: flex or display: grid is on the parent, not on the text, and that the text has no big top or bottom margin.