HTML 元素如何浮动?


我们可以使用 CSS 的 float 属性将元素浮动到包含父元素的左侧或右侧。其他元素会围绕浮动内容放置。具有相同 float 属性值的多个元素都并排放置。

左浮动

在这个例子中,图像使用 float 属性和 left 值放置在左侧。为了正确设置右侧边距,我们使用 margin-right 属性设置它 -

img {
   float: left;
   margin-right:20px;
}

示例

让我们看一个将元素左浮动的示例 -

<!DOCTYPE html>
<html>
<head>
   <style>
      img {
         float: left;
         margin-right:20px;
      }
   </style>
</head>
<body>
   <h1>Demo Heading</h1>
   <p>In this example, the image will float to the left in the text, and the text in the paragraph will wrap around the image.</p>
   <p><img src="https://tutorialspoint.com/sql/images/sql-mini-logo.jpg" alt="SQL">
   SQL is a database computer language designed for the retrieval and management of data in a relational databases like MySQL, MS Access, SQL Server, Oracle, Sybase, Informix, Postgres etc. SQL stands for Structured Query Language. SQL was developed in the 1970s by IBM Computer Scientists.</p>
</body>
</html>

右浮动

在这个例子中,图像使用 float 属性和 right 值放置在右侧。为了正确设置左侧边距,我们使用 margin-left 属性设置它 -

img {
   float: right;
   margin-left:20px;
}

示例

让我们看看将元素右浮动的示例 -

<!DOCTYPE html>
<html>
<head>
   <style>
      img {
         float: right;
         margin-left:20px;
      }
   </style>
</head>
<body>
   <h1>Demo Heading</h1>
   <p>In this example, the image will float to the left in the text, and the text in the paragraph will wrap around the image.</p>

   <p><img src="https://tutorialspoint.com/sql/images/sql-mini-logo.jpg" alt="SQL">
   SQL is a database computer language designed for the retrieval and management of data in a relational databases like MySQL, MS Access, SQL Server, Oracle, Sybase, Informix, Postgres etc. SQL stands for Structured Query Language. SQL was developed in the 1970s by IBM Computer Scientists.</p>
</body>
</html>

左右浮动

在这个例子中,我们展示了如何使用 CSS Float 属性。这里,<button> 元素使用 float 属性的 leftright 值分别设置在左侧或右侧。我们设置了一个 flex 布局。初始 flex-items 使用 align-items 属性设置为居中 -

示例

<!DOCTYPE html>
<html>
<head>
   <title>CSS Float</title>
   <style>
      form {
         width:70%;
         margin: 0 auto;
         text-align: center;
      }
      input[type="button"] {
         border-radius: 10px;
      }
      #container {
         display: flex;
         flex-direction: column-reverse;
         justify-content: center;
         align-items: center;
      }
      .child{
         height: 40px;
         width: 40px;
         color: white;
         border: 4px solid black;
      }
      .orange{
         background-color: #FF8A00;
      }
      .red{
         background-color: #F44336;
      }
      .violet{
         background-color: #C303C3;
      }
      .green{
         background-color: #4CAF50;
      }
      .blue{
         background-color: #03A9F4;
      }
      .yellow{
         background-color: #FEDC11;
      }
      #left{
         display: flex;
         float: left;
      }
      #right{
         display: flex;
         float: right;
      }
   </style>
</head>
<body>
   <form>
      <fieldset>
         <legend>CSS-Float</legend>
         <div id="container">
            <div class="child orange"></div><div class="child red"></div><div class="child violet"></div><div class="child green"></div><div class="child blue"></div><div class="child yellow"></div>
            </div><br>
            <input type="button" value="float-->left" onclick="floatDecider('left')">
            <input type="button" value="float-->right" onclick="floatDecider('right')">
         <div><div id="left"></div><div id="right"></div></div>
      </fieldset>
   </form>
   <script>
      var left = document.getElementById('left');
      var right = document.getElementById('right');
      function floatDecider(direction){
         var allChildren = document.getElementsByClassName('child');
         if(direction === 'left')
         left.insertAdjacentElement('beforeend',allChildren[0]);
         else
         right.insertAdjacentElement('afterbegin',allChildren[0]);
      }
   </script>
</body>
</html>

更新于: 2023-11-15

310 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告