HTML DOM 样式 cssFloat 属性

htmlweb developmentfront end technology更新于 2024/3/14 3:56:17

HTML DOM 样式 cssFloat 属性用于设置或获取元素的水平对齐方式。您可以将元素浮动至左或右 −

以下是语法 −

设置 cssFloat 属性 −

object.style.cssFloat = "left|right|none|initial|inherit"

以上属性值的解释如下 −

值
描述
None
这是默认值,不会浮动元素。
Left
这会使元素浮动到父元素的最左侧位置。
Right
这会使元素浮动到父元素的最右侧位置。
Initial
将此属性设置为初始值。
Inherit
继承父级属性值

我们来看一个 cssFloat 属性的示例 −

示例

<!DOCTYPE html>
<html>
<head>
<style>
   #DIV1 {
      height: 50px;
      width: 50px;
      margin: 5px;
      float: left;
      background-color: rebeccapurple;
   }
   div+p{
      height:70px;
      color: red;
      border: 2px solid #b8860b;
   }
</style>
<script>
   function changeFloat() {
      document.getElementById("DIV1").style.float = "right";
      document.getElementById("Sample").innerHTML="The div element will now be floated to right";
   }
</script>
<body>
   <div id="DIV1"></div>
   <p>This is a sample paragraph. Here is another line in this paragraph. Here is the third line in the paragraph. </p>
   <p>Change the above div float property by clicking the below button</p>
   <button onclick="changeFloat()">Change Float</button>
   <p id="Sample"></p>
</body>
</html>

输出

点击"更改浮动"按钮 −


相关文章


有用资源