How to set Text alignment in HTML?

To set text alignment in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property text-align for the center, left, and right alignment. HTML5 do not support the align attribute of the <p> tag, so the CSS style is used to set text alignment.

Just keep in mind, the usage of style attribute overrides any style set globally. It will override any style set in the HTML <style> tag or external style sheet.


You can try to run the following code to set text alignment in HTML

Live Demo

<!DOCTYPE html>
      <title>HTML Document</title>

      <p style="text-align:center;">Learn for free</p>

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 29-Aug-2023

205K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started