如何使用 JavaScript 设置元素的水平对齐方式?


要水平对齐元素,请使用 cssFloat 属性。

示例

可以尝试运行以下代码,使用 JavaScript 设置元素的水平对齐方式 −

动态演示

<!DOCTYPE html>
<html>
   <body>
      <img id="myID" src="https://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>

更新于:2020 年 6 月 23 日

266 次浏览

开启您的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.