0Pricing
Tailwind CSS Academy · บทเรียน

การจัดเนื้อหาและการจัดแนวองค์ประกอบ

จัดตำแหน่งองค์ประกอบลูกตามแกนหลักด้วย justify-* และตามแกนตัดขวางด้วย items-* เพื่อสร้างรูปแบบการจัดวางที่ใช้กันทั่วไป

การจัดเนื้อหาและการจัดแนวองค์ประกอบ เป็นบทเรียน Tailwind CSS Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Tailwind CSS Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Tailwind CSS Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Two Axes of Flex Alignment

Flexbox alignment operates on two perpendicular axes. The main axis runs in the direction of flex-row (horizontal) or flex-col (vertical). The cross axis is perpendicular to the main axis. justify-* controls alignment along the main axis. items-* controls alignment along the cross axis. Understanding this distinction is the key to mastering flex layout.

justify-content: Main Axis Positioning

The justify-* utilities correspond to CSS justify-content. In a flex row, they control horizontal positioning of the group of items. justify-start packs items to the left (default). justify-end packs to the right. justify-center centers the group. justify-between distributes items with equal space between them. justify-around adds equal space around each item. justify-evenly distributes space evenly including the edges.

<div class="flex justify-start  gap-2 bg-gray-100 p-4 mb-2"><div class="bg-blue-400 p-3">A</div><div class="bg-blue-400 p-3">B</div></div>
<div class="flex justify-center gap-2 bg-gray-100 p-4 mb-2"><div class="bg-blue-400 p-3">A</div><div class="bg-blue-400 p-3">B</div></div>
<div class="flex justify-between bg-gray-100 p-4 mb-2"><div class="bg-blue-400 p-3">A</div><div class="bg-blue-400 p-3">B</div></div>
<div class="flex justify-end   gap-2 bg-gray-100 p-4"><div class="bg-blue-400 p-3">A</div><div class="bg-blue-400 p-3">B</div></div>

justify-between: The Space Distributor

justify-between is one of the most used flex utilities. It places the first item at the start of the container, the last item at the end, and distributes remaining items with equal space between them. This is the standard pattern for navbars (logo left, links right), cards (icon left, action right), and any component where items should occupy the full width with space between.

<!-- Navbar with justify-between -->
<div class="flex items-center justify-between px-6 py-4 bg-white shadow">
  <span class="font-bold text-indigo-600">Logo</span>
  <button class="bg-indigo-600 text-white text-sm px-4 py-2 rounded">Sign Up</button>
</div>

<!-- Card footer with actions -->
<div class="flex items-center justify-between pt-4 border-t">
  <span class="text-sm text-gray-500">Last updated: Today</span>
  <button class="text-indigo-600 text-sm font-medium">Edit</button>
</div>

align-items: Cross Axis Positioning

The items-* utilities correspond to CSS align-items and control how items are positioned on the cross axis. In a flex row, this controls vertical alignment. items-start aligns to the top. items-end aligns to the bottom. items-center vertically centers all items. items-stretch (default) stretches items to fill the container's height. items-baseline aligns text baselines.

<!-- All four items-* values demonstrated in a row -->
<div class="flex items-start h-24 gap-4 bg-gray-100 p-4 mb-2">
  <div class="bg-blue-400 p-2 text-sm">Start</div>
</div>
<div class="flex items-center h-24 gap-4 bg-gray-100 p-4 mb-2">
  <div class="bg-green-400 p-2 text-sm">Center</div>
</div>
<div class="flex items-end h-24 gap-4 bg-gray-100 p-4">
  <div class="bg-red-400 p-2 text-sm">End</div>
</div>

items-center: The Most Used Alignment

items-center is used in virtually every flex row because it vertically centers all children regardless of their individual heights. Icons, text, and buttons — even when they differ in size — all align along their vertical midpoints. This makes navbars, list items, and card footers look polished without any manual height adjustments.

<!-- Icon + text + badge: all different heights, all centered -->
<div class="flex items-center gap-3 px-4 py-3 hover:bg-gray-50">
  <div class="w-10 h-10 bg-indigo-100 rounded-full flex items-center justify-center text-indigo-600 font-bold">
    JD
  </div>
  <div>
    <p class="font-medium text-gray-900">Jane Doe</p>
    <p class="text-xs text-gray-500">Product Designer</p>
  </div>
  <span class="ml-auto bg-green-100 text-green-700 text-xs font-semibold px-2 py-0.5 rounded-full">
    Active
  </span>
</div>

items-baseline for Text Alignment

items-baseline aligns items so that their text baselines (the bottom of capital letters) line up. This is useful when mixing different font sizes in a row — for example, a large price number next to a smaller currency symbol or unit label. Without baseline alignment, the different sizes look awkward; with it, they form a visually cohesive group.

<!-- Price with currency and period aligned on baseline -->
<div class="flex items-baseline gap-1">
  <span class="text-sm font-medium text-gray-500">$</span>
  <span class="text-4xl font-extrabold text-gray-900">49</span>
  <span class="text-sm font-medium text-gray-500">/month</span>
</div>

Combining justify and items

Power comes from combining justify-* and items-* together. The classic flex items-center justify-center centers content on both axes simultaneously. flex items-center justify-between distributes items horizontally while keeping them vertically centered — the standard navbar pattern. Think of it as controlling two independent axes at once.

<!-- Perfect vertical and horizontal centering -->
<div class="min-h-screen flex items-center justify-center">
  <p class="text-2xl font-bold">Dead center</p>
</div>

<!-- Navbar pattern -->
<nav class="flex items-center justify-between px-8 h-16 bg-white border-b">
  <span class="font-bold">Brand</span>
  <button class="text-sm font-medium text-indigo-600">Login</button>
</nav>

align-self: Individual Item Override

While items-* aligns all children uniformly, self-* utilities override alignment for a single flex item. self-start, self-end, self-center, self-stretch, and self-baseline work exactly like their items-* counterparts but apply to the element they are written on rather than all children. Use this to break one item out of the group's alignment.

<!-- Most items centered, but one aligned to the top -->
<div class="flex items-center gap-4 bg-gray-100 p-4 h-24">
  <div class="self-start bg-indigo-400 p-3">Top</div>
  <div class="bg-green-400 p-3">Middle</div>
  <div class="bg-blue-400 p-3">Middle</div>
</div>

justify-items in Grid (not Flex)

Do not confuse justify-content (for flex containers) with justify-items (for grid containers). In Tailwind, justify-* controls justify-content on flex containers. For CSS Grid, you use justify-items-* and place-items-*. These control how grid children are positioned within their individual grid cells rather than how the grid tracks are distributed.

<!-- Flex: justify-between distributes the group -->
<div class="flex justify-between">
  <div>A</div><div>B</div><div>C</div>
</div>

<!-- Grid: justify-items centers each item within its cell -->
<div class="grid grid-cols-3 justify-items-center">
  <div>A</div><div>B</div><div>C</div>
</div>

align-content for Multi-Line Flex

When flex items wrap across multiple lines (using flex-wrap), content-* utilities control alignment of the entire group of lines. content-center centers all lines as a group in the container. content-between distributes lines with space between them. This operates on the collection of rows (or columns), not individual items.

<!-- Multi-line flex with content alignment -->
<div class="flex flex-wrap content-center gap-4 bg-gray-100 h-64 p-4">
  <div class="bg-blue-400 p-4 rounded">A</div>
  <div class="bg-blue-400 p-4 rounded">B</div>
  <div class="bg-blue-400 p-4 rounded">C</div>
  <div class="bg-blue-400 p-4 rounded">D</div>
  <div class="bg-blue-400 p-4 rounded">E</div>
</div>

Practical: Feature Card Row

A feature card row commonly uses flex items-start (to let variable-height cards align from the top) combined with gap-6. Each card internally uses flex flex-col so that a 'Learn More' link at the bottom can be pushed there with mt-auto. This combination creates a professional feature grid where all cards start at the same vertical position.

<div class="flex items-start gap-6">
  <div class="flex flex-col flex-1 bg-white rounded-xl p-6 shadow">
    <div class="w-10 h-10 bg-indigo-100 rounded-lg mb-4"></div>
    <h3 class="font-bold text-gray-900 mb-2">Fast</h3>
    <p class="text-gray-500 text-sm flex-1">Lightning-fast builds with the JIT engine.</p>
    <a href="#" class="mt-4 text-indigo-600 text-sm font-medium">Learn more &rarr;</a>
  </div>
  <div class="flex flex-col flex-1 bg-white rounded-xl p-6 shadow">
    <div class="w-10 h-10 bg-green-100 rounded-lg mb-4"></div>
    <h3 class="font-bold text-gray-900 mb-2">Flexible</h3>
    <p class="text-gray-500 text-sm flex-1">Customize every aspect with the config.</p>
    <a href="#" class="mt-4 text-green-600 text-sm font-medium">Learn more &rarr;</a>
  </div>
</div>

Quick Check

Test your understanding of Tailwind CSS Mastery concepts from this lesson.

Lesson Recap

In this lesson you learned: justify-* controls main axis alignment (horizontal in flex-row), items-* controls cross axis alignment (vertical in flex-row), and combining flex items-center justify-between is the standard navbar pattern. Next up we control how flex children grow and shrink with flex-grow, flex-shrink, and basis utilities.

คำถามที่พบบ่อย

บทเรียน “การจัดเนื้อหาและการจัดแนวองค์ประกอบ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดเนื้อหาและการจัดแนวองค์ประกอบ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Tailwind CSS Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Tailwind CSS Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดเนื้อหาและการจัดแนวองค์ประกอบ”

จัดตำแหน่งองค์ประกอบลูกตามแกนหลักด้วย justify-* และตามแกนตัดขวางด้วย items-* เพื่อสร้างรูปแบบการจัดวางที่ใช้กันทั่วไป คุณปฏิบัติ Tailwind CSS Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Tailwind CSS Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Tailwind CSS Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดเนื้อหาและการจัดแนวองค์ประกอบ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Tailwind CSS Academy นี้ได้ไหม

ได้ บทเรียน Tailwind CSS Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การเปิดใช้ Flex และการกำหนดทิศทาง
  2. การจัดเนื้อหาและการจัดแนวองค์ประกอบ
  3. การขยาย การหด และพื้นฐานของ Flex
  4. การตัดบรรทัดและช่องว่าง
← กลับไปที่ Tailwind CSS Academy