Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ล้าง float ขององค์ประกอบด้วย Bootstrap clearfix

เพื่อล้าง float ขององค์ประกอบ ให้ใช้คลาส .clearfix ของ Bootstrap

ตัวอย่าง

ลองรันโค้ดด้านล่างเพื่อดูการทำงานของ clearfix

<!DOCTYPE html> <html>    <head>       <title>Bootstrap Example</title>       <link href = "/bootstrap/css/bootstrap.min.css" rel = "stylesheet">       <script src = "/scripts/jquery.min.js"></script>       <script src = "/bootstrap/js/bootstrap.min.js"></script>    </head>    <body>       <div class = "clearfix" style = "background: orange;border: 1px solid #000; padding: 10px;">          <div class = "pull-left" style = "background: blue; color: white;">             ซ้าย          </div>          <div class = "pull-right" style = "background: blue; color:white;">             ขวา          </div>       </div>    </body> </html>

การทำงานของ .clearfix:

  • แก้ปัญหา parent container หดตัวเมื่อลูกหลานใช้ float
  • ใช้ pseudo-element ::after พร้อม clear: both ภายใน
  • ใน Bootstrap 5 ใช้ clearfix คลาสเดียวกัน แต่ไม่ต้องพึ่ง jQuery