29 Jan 25
How To Center Text In HTML?
Alrightโcentering text sounds simple. And sometimes, it is. But depending on your page layout, container types, or the weird little quirks of browsers, thereโs actually a few ways to approach it. Some better than others.
Over the years, Iโve had countless clients ask: “Why is this text not centered properly?”
Well. Letโs run through the main ways you can center text in HTML and CSS. Iโll keep it practical, like I would if we were mapping it out on a whiteboard.
ย
The Basics: HTML vs CSS
First up. HTML on its own doesnโt give you much. CSS is where all the heavy lifting happens.
Technically, HTML does have a <center>
tag (more on that in a sec), but in 2025? You want to avoid that. CSS gives you better control, it plays nicer with responsive designs, and it keeps your code tidy for the long haul.
ย
5 Solid Ways To Center Text
1) The Old <center>
Tag (Don’t Do Thisโฆ Anymore)
Yeah, back in the early 2000s, we all used:
It worked. It still technically works. But itโs been deprecated for years now. Google wonโt like it. Screen readers donโt love it. And if youโre building anything modern (which you should be), you want to avoid leaning on old-school tags like this.
2) The Quick & Easy text-align: center;
This one still works great for simple horizontal centering.
3) Flexbox โ My Personal Go-To
Flexbox is a bit of a game changer. When I was working on a site for a client called EcoFit last month, we used Flexbox to handle all their call-to-actions across mobile and desktop. Zero headaches.
Hereโs a full center (both horizontal and vertical):
-
justify-content: center
handles horizontal -
align-items: center
handles vertical -
And
height: 100vh
makes sure it takes up the full viewport
Perfect for hero banners, landing pages, or full-screen modals.
4) CSS Grid โ Super Clean For Complex Layouts
Grid is Flexboxโs cooler cousin. Similar idea, slightly different vibe.
-
place-items: center
is shorthand for both vertical and horizontal centering. -
I tend to use Grid when Iโm dealing with multi-column layouts or more complex page structures.
We used this recently for a client’s product catalog where Flexbox was just getting messy.
5) The margin: auto
Trick
Sometimes, youโre just trying to center a block-level element. This still works like a charmโas long as you give the element a width.
-
The browser automatically calculates the left and right margins.
-
Great for cards, containers, or images where you donโt need full viewport alignment.
Some Real-World Considerations
Before you pick your method, letโs touch on a few things I always remind my team:
-
Responsiveness matters: Flexbox and Grid handle screen sizes better. Donโt break your layout on mobile.
-
Vertical centering used to be a nightmare: Flexbox and Grid basically solved that.
-
Accessibility isnโt optional: Donโt center huge blocks of text just because it โlooks nice.โ Think readability.
-
Avoid hard-coding absolute positions: Unless you love debugging for hours. Seriously.
So… Which Method Should You Use?
Honestly? Depends on the situation.
-
Quick headings? Use
text-align: center
. -
Full-page layouts? Flexbox or Grid.
-
Block containers?
margin: auto
still holds up.
Most of my own builds default to Flexbox first. It just handles the most common use cases with minimal code.
If you’re still stuckโor if your layout just isn’t playing niceโsometimes it’s worth having a pro take a look. At Chromatix, weโve been wrangling HTML, CSS, and everything in between for years. We can make sure your text sits where it shouldโand more importantly, help your whole site convert better.
By the way โ what’s your most frustrating centering issue youโve hit lately?