How to set how the last line of a block or a line right before a forced line break is aligned when text-align is "justify" with JavaScript?


Use the textAlignLast property in JavaScript to set the last line to right. Set it to right and allow the right alignment.

Example

You can try to run the following code to return how the last line of a block or a line right before a forced line break is aligned when text-align is "justify" with JavaScript −

<!DOCTYPE html>
<html>
   <head>
      <style>
         #myDIV {
            text-align: justify;
         }
      </style>
   </head>

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

Updated on: 23-Jun-2020

289 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements