Computer >> Computer tutorials >  >> Programming >> Javascript

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 −

<!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>