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.tw/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.tw/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 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.