How to center text in html
Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.
Last updated: April 4, 2026
Key Facts
- The `text-align: center;` CSS property is the standard and recommended method for centering text.
- This CSS property should be applied to the parent element of the text you wish to center.
- The `<center>` HTML tag was used for centering but is now deprecated and should be avoided.
- CSS offers more control and flexibility for layout and alignment compared to older HTML tags.
- Centering text can be applied to various elements like paragraphs, headings, divs, and spans.
Overview
Centering text in HTML is a fundamental aspect of web design, allowing for better visual hierarchy and aesthetic appeal. Whether you need to center a heading, a paragraph, or an entire block of content, there are several methods available. However, with the evolution of web standards, modern approaches using Cascading Style Sheets (CSS) are strongly preferred over older HTML attributes or tags.
Understanding Text Alignment in HTML and CSS
Text alignment refers to how lines of text are arranged within their containing element. The most common alignments are left (default), right, center, and justify. In HTML, the actual visual presentation is largely controlled by CSS.
Method 1: Using CSS `text-align` (Recommended)
The most robust and widely supported method for centering text is by using the CSS `text-align` property. This property is applied to the parent container of the text you want to center. It tells the browser to center all inline content (like text, images, and inline-block elements) within that container.
Inline Styling
You can apply CSS directly to an HTML element using the `style` attribute. This is suitable for quick, one-off centering.
<p style="text-align: center;">This text is centered using inline CSS.</p><h1 style="text-align: center;">Centered Heading</h1>Internal Stylesheet
For more organized styling, you can define CSS rules within the `