How to set the horizontal alignment of an element with JavaScript?


To align the element horizontally, use the cssFloat property.

Example

You can try to run the following code to set the horizontal alignment of an element with JavaScript −

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <img id="myID" src="https://www.tutorialspoint.com/videotutorials/images/tutor_connect_home.jpg" width="300" height="300">
      <p>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.
      </p>
      <button type="button" onclick="display()">Align Horizontally</button>
      <script>
         function display() {
            document.getElementById("myID").style.cssFloat = "right";
         }
      </script>
   </body>
</html>

Updated on: 23-Jun-2020

199 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements