How can I center text (horizontally and vertically) inside a div block?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Centering text inside a div is a very common CSS task, and the best modern answer is usually Flexbox or Grid. Both handle horizontal and vertical alignment cleanly without fragile hacks based on padding or line height.
Flexbox: The Most Practical Default
For a single block of content, Flexbox is usually the clearest solution.
justify-content: center centers content along the main axis, and align-items: center centers it along the cross axis. With the default row direction, that gives horizontal and vertical centering.
CSS Grid: Even Shorter for Pure Centering
Grid is also excellent when the job is simply “put this content in the middle.”
place-items: center is short and expressive. If you are already using Grid for layout, this is often the cleanest answer.
When text-align Helps and When It Does Not
text-align: center only centers inline content horizontally. It does not solve vertical centering by itself.
That line is useful, but it is not the full solution. Developers often reach for it first and then wonder why the text is still stuck to the top of the box.
Older Approaches and Why They Are Less Ideal
Older CSS patterns used tricks such as:
- matching
line-heightto container height - adding symmetrical padding
- absolute positioning with transforms
These can still work, but they are less flexible for multiline content or dynamic content sizes.
For example, the line-height trick only works well for a single line of text.
As soon as the text wraps, the centering breaks down.
A Good Rule of Thumb
Use Flexbox when you are centering content in a component. Use Grid when the element is already part of a grid-based layout or when place-items: center reads most naturally. Avoid older hacks unless you are working in a genuinely constrained legacy environment.
Multiline Text Still Works With Modern Layout
One reason Flexbox and Grid are better is that they handle multiline text naturally.
That is a big improvement over single-line hacks that assume fixed text length.
Common Pitfalls
The most common mistake is assuming text-align: center also handles vertical centering. It does not.
Another mistake is using line-height tricks for content that may wrap. That solution works only for very narrow cases.
Developers also sometimes forget that the container needs a defined height, or at least some vertical space, before vertical centering is visible at all.
It is also easy to center the text node but forget spacing, padding, or box sizing around it, which makes the result look visually off even when the alignment is technically correct.
Summary
- The best modern solutions are usually Flexbox or Grid.
- Flexbox uses
justify-content: centerandalign-items: center. - Grid can center content with
place-items: center. - '
text-align: centerhelps horizontally but not vertically.' - Avoid line-height or padding hacks unless you are dealing with a constrained legacy layout.
Related reading
- How can I change the color of an 'svg' element?
- How can I compare software version number using JavaScript? only numbers
- How can I construct a tree using d3 and its force layout?
- How can I Convert HTML to Text in C?
- How can I create a two dimensional array in JavaScript?
- How can I deal with floating point number precision in JavaScript?
- How can I debug javascript on Android?
- How can I delete folder on S3 with Node.js?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.