How to set the horizontal alignment of text with JavaScript?


Use the textAlign property in JavaScript and set it to right for aligning it horizontally. You can try to run the following code to return the horizontal alignment of text with JavaScript −

Example

<!DOCTYPE html>
<html>
   <body>
      <p id = "myText">This is demo text</p>
      <button onclick = "display()">Set Horizontal Alignment</button>
      <script>
         function display() {
            document.getElementById("myText").style.textAlign = "right";
         }
      </script>
   </body>
</html>

Updated on: 23-Jun-2020

612 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements