CSS Flexbox (กล่องแบบยืดหยุ่น)


สารบัญ

    แสดงสารบัญ


1

2

3

4

5

6

7

8

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-wrap: nowrap;
  background-color: DodgerBlue;
}

.flex-container > div {
  background-color: #f1f1f1;
  width: 100px;
  margin: 10px;
  text-align: center;
  line-height: 75px;
  font-size: 30px;
}
</style>
</head>
<body>
<h1>Flexible Boxes</h1>

<div class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>  
  <div>4</div>
  <div>5</div>
  <div>6</div>  
  <div>7</div>
  <div>8</div>
</div>

<p>Try to resize the browser window.</p>
<p>A container with "flex-wrap: nowrap;" will never wrap its items.</p>
<p><strong>Note:</strong> Flexbox is not supported in Internet Explorer 10 or earlier versions.</p>

</body>
</html>

โมดูลเค้าโครง CSS Flexbox

ก่อนโมดูลเค้าโครง Flexbox มีโหมดเค้าโครงสี่โหมด:

Block

เหมือนบล็อกสำหรับส่วนต่างๆ ในหน้าเว็บ

Inline

อินไลน์สำหรับข้อความ

Table

ตาราง สำหรับข้อมูลตารางสองมิติ

Positioned

ตำแหน่งสำหรับตำแหน่งที่ชัดเจนขององค์ประกอบ

โมดูลเค้าโครงกล่องแบบยืดหยุ่น ช่วยให้การออกแบบโครงสร้างเค้าโครงแบบตอบสนองที่ยืดหยุ่นได้ง่ายขึ้น โดยไม่ต้องใช้โฟลตหรือการวางตำแหน่ง


รองรับเบราว์เซอร์

คุณสมบัติ flexbox ได้รับการสนับสนุนในเบราว์เซอร์สมัยใหม่ทั้งหมด

29.0 11.0 22.0 10 48

องค์ประกอบเฟล็กซ์บ็อกซ์

หากต้องการเริ่มใช้โมเดล Flexbox คุณต้องกำหนดคอนเทนเนอร์ Flex ก่อน

1

2

3

องค์ประกอบด้านบนแสดงถึงคอนเทนเนอร์ดิ้น (พื้นที่สีน้ำเงิน) พร้อมด้วยรายการดิ้น 3 รายการ

ตัวอย่าง

คอนเทนเนอร์ดิ้นที่มีรายการดิ้นสามรายการ:

<div 
  class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  background-color: DodgerBlue;
}

.flex-container > div {
  background-color: #f1f1f1;
  margin: 10px;
  padding: 20px;
  font-size: 30px;
}
</style>
</head>
<body>

<h1>Create a Flex Container</h1>

<div class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>  
</div>

<p>A Flexible Layout must have a parent element with the <em>display</em> property set to <em>flex</em>.</p>

<p>Direct child elements(s) of the flexible container automatically becomes flexible items.</p>

</body>
</html>


คุณจะได้เรียนรู้เพิ่มเติมเกี่ยวกับคอนเทนเนอร์ Flex และรายการ Flex ในบทถัดไป