How to center text in HTML (and why it sometimes refuses to move)
Horizontal centering with text-align, centering a whole box, vertical centering with flexbox, the old <center> tag, and the four reasons centering does not work.
To center text in HTML, give the element that holds the text the CSS rule text-align: center. HTML on its own has no working centering tag any more, so centering is always done with CSS, either in a style attribute or in a <style> block.
Center one heading or paragraph
The quickest way is a style attribute on the element:
<h1 style="text-align: center;">Spring menu</h1>
To center every heading on the page, put the rule in a <style> block inside <head> instead:
<style> h1, h2 { text-align: center; } </style>
To center everything, use body { text-align: center; }. Every block inside inherits it, so set text-align: left on anything that should stay left-aligned, such as long paragraphs or lists.
Center a box, not just the text inside it
text-align moves the lines of text inside a box. It does not move the box. To center a card, form or image column on the page, give the box a width and set its left and right margins to auto:
<div style="max-width: 600px; margin: 0 auto;"> ... </div>
An image is inline by default, so center it with text-align: center on its parent, or give the image display: block and margin: 0 auto.
Center vertically
Vertical centering needs flexbox. Make the parent a flex container with a height, then center along both directions:
<div style="display: flex; justify-content: center; align-items: center; min-height: 100vh;"> <p>Coming soon</p> </div>
justify-content centers left to right, align-items centers top to bottom, and min-height: 100vh makes the box fill the screen.
What about the <center> tag?
<center> and align="center" come from old versions of HTML. Browsers still display them, but they are deprecated, code checkers flag them and they mix layout into your content. Use text-align: center instead.
When centering does nothing
- ✓text-align is on a <span>, <a> or <img>: these are inline, so the rule has no room to work. Put it on the parent block, such as the <p> or <div>.
- ✓margin: 0 auto does nothing: the box has no width, so it already fills the line. Add width or max-width.
- ✓align-items: center does nothing: the parent has no height. Add min-height, or set display: flex on the right element.
- ✓Another rule wins: a later rule in the stylesheet, or a class like text-left in a Tailwind page, overrides yours. Right-click the text, choose Inspect and look for a crossed-out text-align.
Pages made with AI tools
Pages from Claude, ChatGPT and other tools often use Tailwind, where centering is a class: text-center for text, mx-auto for a box, and flex items-center justify-center for both directions. Change the class rather than adding a style attribute, or the two can fight.
Share the page as a link
Check the centering on a phone as well as your laptop before you send it. With FileShare Pro you upload the .html file from the dashboard, copy the link and send it; it opens as a page in any browser and the person opening it needs no account. If something still sits off-center, fix the file and upload it to the same link. New to HTML files? Start with how to create an HTML file. Sign up free to share your first page.
Frequently asked
Add text-align: center to the element that holds the text, for example <p style="text-align: center;">.
Give it a width or max-width and margin: 0 auto. To center it vertically too, use display: flex with justify-content: center and align-items: center on its parent.
No. It is deprecated. Browsers still show it, but use text-align: center in CSS instead.
Usually the rule is on an inline element like a <span>, the box has no width, or another rule overrides it. Use Inspect in the browser to see which rule wins.
Drop the file, copy the link — live in seconds, free plan included.
Start sharing free