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

ยูทิลิตีความกว้าง

กำหนดความกว้างตายตัวด้วย w-4, w-64 และอื่น ๆ ใช้ความกว้างแบบเศษส่วน เช่น w-1/2 และขยายให้เต็มด้วย w-full กับ w-screen

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

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

Controlling Width in Tailwind

Setting element widths is one of the most common layout tasks in CSS. Tailwind provides the w-* family of utilities that cover fixed sizes, fractional sizes, percentage-based sizes, and viewport-relative sizes. Instead of memorizing pixel values, you work with Tailwind's consistent spacing scale and semantic keywords that map to well-designed CSS values.

<!-- Width examples side by side -->
<div class="space-y-2">
  <div class="w-32 bg-blue-100 p-2">w-32 (128px)</div>
  <div class="w-1/2 bg-blue-200 p-2">w-1/2 (50%)</div>
  <div class="w-full bg-blue-300 p-2">w-full (100%)</div>
</div>

Fixed Width With the Spacing Scale

Fixed widths follow Tailwind's spacing scale where each unit equals 4px (0.25rem). So w-4 is 16px, w-16 is 64px, and w-64 is 256px. The scale goes up to w-96 (384px). These values are perfect for icons, avatars, sidebars, and any element with a known, fixed size that should not change with viewport width.

<!-- Fixed-size avatar and sidebar examples -->
<div class="flex gap-4 items-center">
  <!-- Avatar: 12 x 12 = 48px -->
  <div class="w-12 h-12 rounded-full bg-indigo-400 flex items-center justify-center text-white font-bold">
    JD
  </div>

  <!-- Sidebar: 64 = 256px -->
  <div class="w-64 bg-gray-100 p-4 rounded">
    Sidebar panel (256px)
  </div>
</div>

Fractional Widths

Fractional width utilities express width as a fraction of the parent container. Tailwind supports halves (w-1/2), thirds (w-1/3, w-2/3), quarters (w-1/4 through w-3/4), fifths, and sixths. These compile to percentage values — for example, w-1/3 becomes width: 33.333333% — making them ideal for side-by-side column layouts.

<!-- Two-thirds / one-third split layout -->
<div class="flex gap-4">
  <div class="w-2/3 bg-emerald-100 p-4 rounded">Main (2/3 wide)</div>
  <div class="w-1/3 bg-emerald-200 p-4 rounded">Sidebar (1/3 wide)</div>
</div>

w-full and w-screen

w-full sets width to 100% of the parent container, while w-screen sets width to 100vw (the viewport width). The difference matters when the parent is not the full viewport: w-full fills the parent, but w-screen always fills the entire browser window and may cause horizontal overflow if there is a scrollbar. Use w-screen for full-viewport overlays and w-full for everything else.

<!-- w-full fills the parent, regardless of parent width -->
<div class="w-1/2 bg-gray-100 p-4">
  <button class="w-full bg-blue-500 text-white py-2 rounded">
    Full-width button inside half-width parent
  </button>
</div>

<!-- w-screen fills the entire viewport -->
<div class="w-screen bg-indigo-800 text-white py-3 text-center">
  Full-viewport-width banner
</div>

w-auto for Natural Width

w-auto lets the element take its natural or content-driven width. This is the browser default for most block elements, but you may need to apply it explicitly to override a wider width set at a different breakpoint. It is commonly used when switching from w-full on mobile back to auto sizing on desktop, for example in a button that should only be as wide as its text.

<!-- Full-width on mobile, natural width on desktop -->
<button class="w-full md:w-auto bg-purple-500 text-white px-6 py-3 rounded">
  Get Started
</button>

w-fit, w-min, and w-max

Three modern width keywords help elements size themselves based on content: w-fit makes an element as wide as its content (like fit-content), w-min sets width to the minimum content size (collapses to the longest unbreakable word), and w-max expands to the maximum content width without wrapping. These are useful for tags, pills, tooltips, and inline labels.

<!-- Tags that size to their text content -->
<div class="flex gap-2">
  <span class="w-fit bg-blue-100 text-blue-800 px-3 py-1 rounded-full text-sm">
    Design
  </span>
  <span class="w-fit bg-green-100 text-green-800 px-3 py-1 rounded-full text-sm">
    Development
  </span>
  <span class="w-fit bg-purple-100 text-purple-800 px-3 py-1 rounded-full text-sm">
    Marketing
  </span>
</div>

Arbitrary Width Values

When the built-in scale does not cover your exact need, use the arbitrary value syntax: w-[347px] or w-[calc(100%-2rem)]. This compiles to exactly that CSS value in the JIT engine. While convenient, arbitrary values bypass the design system, so use them sparingly — first check if a fractional or fixed scale value achieves the same visual result.

<!-- One-off width for a specific UI element -->
<div class="w-[280px] bg-amber-100 p-4 rounded">
  Tooltip or popover of exact width
</div>

<!-- Calc inside brackets -->
<div class="w-[calc(100%-3rem)] bg-amber-200 p-4 rounded">
  Full width minus 3rem padding
</div>

Responsive Width Changes

Combining responsive prefixes with width utilities is one of the most powerful patterns in Tailwind. A common pattern is w-full sm:w-1/2 lg:w-1/3: full width on mobile, half on small screens, and one-third on large screens. This creates a responsive card layout from a single set of utility classes without any media query CSS.

<!-- Responsive-width cards in a flex wrap container -->
<div class="flex flex-wrap gap-4">
  <div class="w-full sm:w-1/2 lg:w-1/3 bg-rose-100 p-6 rounded">
    Card A
  </div>
  <div class="w-full sm:w-1/2 lg:w-1/3 bg-rose-100 p-6 rounded">
    Card B
  </div>
  <div class="w-full sm:w-1/2 lg:w-1/3 bg-rose-100 p-6 rounded">
    Card C
  </div>
</div>

max-w-* for Readable Content

While w-* sets an exact width, max-w-* sets an upper limit. The most important max-width utility is max-w-prose, which limits text to around 65 characters per line — the sweet spot for readability identified by typography researchers. max-w-sm through max-w-7xl give you semantic container widths matching common design breakpoints.

<!-- Readable article column with max-width -->
<article class="max-w-prose mx-auto px-4">
  <h1 class="text-2xl font-bold mb-4">Article Title</h1>
  <p class="text-gray-700 leading-relaxed">
    This paragraph stays at a comfortable reading width regardless
    of the viewport size. max-w-prose limits it to about 65 characters
    per line for optimal readability.
  </p>
</article>

Width in Flex Containers

Inside a flex container, w-* classes interact with flex-grow and flex-shrink. A child with w-full will try to be 100% wide but the flex container can shrink it. To prevent shrinking, add shrink-0 alongside the width class. This pattern is commonly used for icons and avatars that should never squish, while the sibling text content takes the remaining space.

<!-- Icon stays fixed width, text takes remaining space -->
<div class="flex items-center gap-3 p-4 border rounded">
  <!-- Icon: fixed 40px, never shrinks -->
  <div class="w-10 h-10 shrink-0 bg-blue-500 rounded-full flex items-center justify-center">
    <span class="text-white font-bold">AI</span>
  </div>
  <!-- Text: takes all remaining width -->
  <div class="min-w-0">
    <p class="font-semibold truncate">Long title that might overflow</p>
    <p class="text-sm text-gray-500">Subtitle text</p>
  </div>
</div>

Centering Fixed-Width Elements

A classic layout pattern is a fixed-width container centered on the page. The combination of a max-width utility and mx-auto (auto horizontal margins) achieves this cleanly. Wrap your page content in max-w-5xl mx-auto px-4 to create a centered, readable column with comfortable side padding on all screen sizes.

<!-- Classic centered page layout -->
<div class="max-w-5xl mx-auto px-4 py-8">
  <header class="mb-6">
    <h1 class="text-3xl font-bold">Page Title</h1>
  </header>
  <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
    <div class="bg-gray-100 p-4 rounded">Column 1</div>
    <div class="bg-gray-100 p-4 rounded">Column 2</div>
    <div class="bg-gray-100 p-4 rounded">Column 3</div>
  </div>
</div>

Quick Check

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

Lesson Recap

In this lesson you learned: w-* fixed utilities follow the 4px spacing scale up to w-96, fractional utilities like w-1/2 set percentage widths relative to the parent, and w-full, w-screen, w-fit, w-min, and w-max cover semantic sizing needs. Next up we explore height utilities that complement width controls.

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

บทเรียน “ยูทิลิตีความกว้าง” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ยูทิลิตีความกว้าง”

กำหนดความกว้างตายตัวด้วย w-4, w-64 และอื่น ๆ ใช้ความกว้างแบบเศษส่วน เช่น w-1/2 และขยายให้เต็มด้วย w-full กับ w-screen คุณปฏิบัติ Tailwind CSS Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

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

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

บทเรียน “ยูทิลิตีความกว้าง” ใช้เวลานานแค่ไหน

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

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

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

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

  1. ยูทิลิตีความกว้าง
  2. ยูทิลิตีความสูง
  3. ข้อจำกัดค่าต่ำสุดและสูงสุด
  4. อัตราส่วนกว้างยาว
← กลับไปที่ Tailwind CSS Academy