Open In App
Related Articles

HTML | DOM Style textAlign Property

Improve Article
Save Article
Like Article

The DOM style textAlign property is pretty much similar to the text-align property in the CSS, it sets the alignment for the inner content of a block element. 
The only difference is that just like any other HTML DOM property, we can use JavaScript to handle it programmatically.

We can use textAlign in two different ways, one to set the alignment, and the other to get the current alignment. 

  • Get the value from the current DOM object.
  • Set the value of the current DOM object. = "left | right | center | justify | 
initial | inherit";

Property Values: 

  • left: It is the default value. The content gets aligned to the left side.
  • right: The content gets aligned to the right side.
  • center: This sets the content to the centre, between the left and right edges.
  • justify: This introduces additional spaces between the words such that the very first word of the line gets aligned to the left side and the last word to the right.
  • inherit: It does not do anything fancy rather it sets the value exactly the same as of its immediate parent.

Return Value: It returns a string value which represents the alignment of the text inside the element. 



<!DOCTYPE html>
        HTML | DOM Style textAlign Property
        #box-element {
            border: 1px solid #ff8888;
            background-color: #ffaaaa;
        .screen-center {
            margin: 30px auto;
            width: 400px;
        #controls {
            text-align: center;
        #inside-content {
            font-size: 0.8rem;
    <div id="box-element" class="screen-center">
        <p id="inside-content">
          An operating system acts as an intermediary
          between the user of a computer and computer
          hardware. The purpose of an operating system
          is to provide an environment in which a user
          can execute programs in a convenient and
          efficient manner. An operating system is
          software that manages the computer hardware.
          The hardware must provide appropriate mechanisms
          to ensure the correct operation of the computer
          system and to prevent user programs from
          interfering with the proper operation of
          the system.
    <div id="controls" class="screen-center">
        <label>Set Alignment: </label>
        <select id="alignment">
            <option value="left" default>Left</option>
            <option value="right">Right</option>
            <option value="center">Center</option>
            <option value="justify">Justify</option>
        <button id="do-align">Set Align Property</button>
    <script type="text/javascript">
        //  Collecting elements 
        let inside_content = document.getElementById(
        let align_option = document.getElementById(
        let align_btn = document.getElementById(
        //  Adding an event to the button   
        align_btn.onclick = function() {
            //    Get current value from the dropdown   
            let align_val =
            //    Set this value to alignment of the content
   = align_val;


  • Before set align property:

  • After set align property: 

Supported Browsers: The browser supported by HTML | DOM Style textAlign Property are listed below: 

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

Last Updated : 09 Sep, 2022
Like Article
Save Article
Similar Reads
Related Tutorials