如何使用 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 日

267 次浏览

启动你的职业生涯

完成课程获取认证

开始
广告
© . All rights reserved.