📦 CSS Display & Layout Guide

1. Display: Block

HTML
<div class="display-block-demo">
  <div class="block-item">
    Block Item 1
  </div>
  <div class="block-item">
    Block Item 2
  </div>
</div>
CSS
.block-item {
  display: block;
  width: 100%;
  padding: 15px;
  margin: 10px 0;
  background: #667eea;
}
Block Item 1
Block Item 2
Block Item 3

2. Display: Inline

HTML
<div class="display-inline-demo">
  <span class="inline-item">
    Inline 1
  </span>
  <span class="inline-item">
    Inline 2
  </span>
</div>
CSS
.inline-item {
  display: inline;
  background: #764ba2;
  color: white;
  padding: 5px 10px;
  margin: 0 5px;
}

/* width & height ไม่ทำงาน */
Inline 1 Inline 2 Inline 3 Inline 4

3. Display: Inline-Block

HTML
<div class="inline-block-demo">
  <div class="inline-block-item">
    Box 1
  </div>
  <div class="inline-block-item">
    Box 2
  </div>
</div>
CSS
.inline-block-item {
  display: inline-block;
  width: 120px;
  padding: 15px 20px;
  margin: 5px;
  background: #667eea;
}
Box 1
Box 2
Box 3

4. Display: Flex

HTML
<div class="display-flex-demo">
  <div class="flex-demo-item">
    Flex 1
  </div>
  <div class="flex-demo-item">
    Flex 2
  </div>
</div>
CSS
.display-flex-demo {
  display: flex;
  gap: 10px;
}

.flex-demo-item {
  flex: 1;
  padding: 15px;
  background: #667eea;
}
Flex 1
Flex 2
Flex 3

5. Flex Direction & Wrap

CSS
/* flex-direction: row (ค่าเริ่มต้น) */
flex-direction: row;

/* flex-direction: column */
flex-direction: column;

/* flex-wrap: wrap */
flex-wrap: wrap;

/* ข้อมูลมากขึ้นจึงอยู่บรรทัดถัดไป */

flex-wrap: wrap

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

flex-direction: row vs column

Row

1
2

Column

1
2

6. Justify Content (การจัดเรียงแนวนอน)

CSS
/* ชิดซ้าย */
justify-content: flex-start;

/* ตรงกลาง */
justify-content: center;

/* ชิดขวา */
justify-content: flex-end;

/* กระจายเต็ม */
justify-content: space-between;

/* กระจายเท่า */
justify-content: space-around;

flex-start

A
B

center

A
B

space-between

A
B

7. Display: Grid

HTML
<div class="display-grid-demo">
  <div class="grid-demo-item">
    Item 1
  </div>
  <div class="grid-demo-item">
    Item 2
  </div>
</div>
CSS
.display-grid-demo {
  display: grid;
  grid-template-columns:
    repeat(3, 1fr);
  gap: 10px;
}

.grid-demo-item {
  background: #667eea;
}
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

8. Visibility vs Display None

CSS
/* ซ่อนแต่ยังใช้พื้นที่ */
visibility: hidden;

/* ลบออกจาก flow */
display: none;

/* ความแตกต่าง */
/* visibility: จองพื้นที่ */
/* display: ไม่จองพื้นที่ */

visibility: hidden (มีช่องว่าง)

Visible
Hidden
Visible

9. Holy Grail Layout

HTML Structure
<div class="holy-grail">
  <header class="hg-header">
    Header
  </header>
  <div class="hg-content">
    <aside class="hg-sidebar">
      Sidebar
    </aside>
    <main class="hg-main">
      Content
    </main>
  </div>
  <footer class="hg-footer">
    Footer
  </footer>
</div>
CSS
.holy-grail {
  display: flex;
  flex-direction: column;
  min-height: 250px;
}

.hg-content {
  display: flex;
  flex: 1;
}

.hg-sidebar {
  flex: 0 0 120px;
}
Header
Sidebar
Main

10. Grid Span & Complex Layout

CSS
.grid-complex-demo {
  display: grid;
  grid-template-columns:
    repeat(4, 1fr);
}

/* ยืดเต็มความกว้าง */
.grid-span-full {
  grid-column: 1 / -1;
}

/* ยืด 2 คอลัมน์ */
.grid-span-2-cols {
  grid-column: span 2;
}
Full Width
Item 1
Span 2 Cols
Item 3
Item 4
Item 5
Item 6
Full Width Footer

11. Card Layout (Responsive)

CSS
.card-demo-grid {
  display: grid;
  grid-template-columns:
    repeat(auto-fit,
    minmax(200px, 1fr));
  gap: 15px;
}

.card-demo {
  background: white;
  border-radius: 5px;
  box-shadow: 0 2px 8px
    rgba(0,0,0,0.1);
}
📸 Card 1
Beautiful card layout
🎨 Card 2
Responsive design
💻 Card 3
Auto-fit columns

12. Masonry Layout

CSS
.masonry-demo {
  column-count: 2;
  column-gap: 15px;
}

.masonry-item {
  margin-bottom: 15px;
  break-inside: avoid;
  /* ห้ามแตกข้ามคอลัมน์ */
}

.masonry-item.tall {
  height: 120px;
}
Item 1
Item 2 (Tall)
Item 3
Item 4
Item 5 (Tall)
Item 6

📚 สรุป Display Properties

Display พฤติกรรม Width/Height ใช้งาน
block ขึ้นบรรทัดใหม่ ✅ ได้ Section, div
inline เรียงแนวเดียว ❌ ไม่ได้ Span, link
inline-block เรียงแนวเดียว ✅ ได้ Button, nav
flex ยืดหยุ่น ✅ ได้ 1D layout
grid 2D layout ✅ ได้ Dashboard
none ลบออก N/A ซ่อนองค์ประกอบ