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>
Monica Mona
Monica Mona

Student of life, and a lifelong learner


Advertisements