Skip to content

Tutorials · Live examples

How to Center Text in HTML & CSS

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

Preview

What to center
Where
Left to right
center
Top to bottom
middle
Method
px

Leave it empty to use the sample text.

Code Green lines do the centering
CSS
.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;
}
HTML
<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.

The quick answer

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:

Center text (left to right)
<style>
  .note {
    text-align: center;
  }
</style>
<p class="note">This line is centered.</p>
Center both ways with flexbox
<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.

Inline or block? The one thing to know first

Almost every “why won’t it center?” question comes down to this. HTML elements are either inline or block:

  • Inline things flow along a line like words: text, <span>, <a>, <img>, <button>. You center them with text-align: center on their parent.
  • Block things take a whole row: <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.
  • Up and down is a different story. Most ways only work left to right. For vertical centering you need flexbox, grid, absolute positioning or the line-height trick, and the parent needs a height.

How to use the Center Text tool

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).

Step 2 Pick where it goes

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.

Step 3 Try each method

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.

Step 4 Copy the code

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.

How to center text horizontally

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.

left, center and right
<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>
Inline style, right in the HTML
<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.

How to center text vertically

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.

Flexbox
<style>
  .box {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 110px;
  }
</style>
<div class="box">Flexbox: in the middle</div>
Grid
<style>
  .box {
    display: grid;
    place-items: center;
    height: 110px;
  }
</style>
<div class="box">Grid: in the middle</div>
line-height (one line only)
<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>
Table-cell (old but works)
<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>
  • Flexbox: align-items moves things up and down, justify-content left and right.
  • Grid: place-items: center does both in one line.
  • line-height: make the line as tall as the box. Only safe for one short line. If the text wraps, each line gets the full height.
  • Table-cell: 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.

How to center a div

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.

margin: 0 auto (left to right)
<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>
Grid parent (both ways)
<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.

How to center an image in HTML

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.

display: block + margin: 0 auto
<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>
text-align on the parent
<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.

How to center a button

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.

One button: text-align on the parent
<style>
  .actions {
    text-align: center;
  }
</style>
<div class="actions">
  <button type="button">Sign up</button>
</div>
Several buttons: flexbox with gap
<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>

How to center a table

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.

margin: 0 auto on the table
<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.

Flexbox vs grid: which one should you use?

Both center anything in every browser in use today. A simple rule of thumb:

UseWhenCode 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;
margin: auto inside a flex parent
<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>

Center with position: absolute and transform

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.

Absolute + transform
<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.

Why you shouldn’t use the <center> tag

<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.

Old tag vs CSS
<style>
  .title {
    text-align: center;
  }
</style>
<div>
  <center>Old: the center tag</center>
  <p class="title">New: text-align in CSS</p>
</div>

Common mistakes (and the fix)

  • text-align on an inline element. <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>.
  • margin: 0 auto with no width. A div without a width already fills the row, so there’s no free space to split. Add a width or max-width. On an image or a span, add display: block first.
  • Vertical centering with no height. If the parent is only as tall as its content, there’s nowhere to move. Give the parent a height or min-height (for a full screen, min-height: 100vh).
  • align-items without display: flex. 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.
  • vertical-align: middle on a div. It only works on inline things and table cells, not to move a block inside its parent. Use flexbox or grid.
  • Forgetting default margins. <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.
Mistake: text-align on a span
<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.

Frequently Asked Questions

How do I center text in HTML?

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.

How do I center text vertically?

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.

How do I center a div?

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.

Why doesn’t text-align: center work on my div or table?

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.

How do I center an image in HTML?

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.

How do I center a button?

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.

Can I still use the <center> tag?

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.

Should I use flexbox or grid to center something?

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.

Why is my text not centered vertically even with align-items: center?

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.