Skip to content
Related Articles

Related Articles

Improve Article

How to align text content into center using JavaScript ?

  • Last Updated : 14 May, 2021

In this article, we will align the text content into centre by using JavaScript. TWe use HTML DOM style textAlign property to set the alignment of text.

Syntax: = "center"

The above syntax set the text alignment into centre dynamically.



<!DOCTYPE html>
        How to align text content into
        center using JavaScript ?
    <h1 style="color: green;">GeeksforGeeks</h1>
        How to align text content into
        center using JavaScript ?
    <p id="content">
        GeeksforGeeks: A computer
        science portal for geeks
    <button onclick="myFunction();">
        Set Alignment
    <script type="text/javascript">
        function myFunction() {
            let txt = document.getElementById("content");
   = "center";


Before Button Click:

After Button Click:

Supported Browsers: 

  • Google Chrome
  • Internet Explorer
  • Mozilla Firefox
  • Opera
  • Safari

Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Web Design for Beginners | HTML  course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :