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

การตกแต่งช่องกรอกข้อความและพื้นที่ข้อความ

ใช้ยูทิลิตีเส้นขอบ ระยะขอบด้านใน วงแหวน และโฟกัสกับช่องกรอกข้อความและพื้นที่ข้อความ เพื่อสร้างฟิลด์ฟอร์มที่สะอาดและเข้าถึงได้

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

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

The Problem With Unstyled Inputs

Browsers render text inputs and textareas with their own default styles — different on every operating system and browser. On Chrome, the default input has a blue ring on focus; on Firefox, it may look different entirely.

Tailwind resets form elements with its preflight base styles, giving you a clean slate. From there, you apply your own border, padding, background, and focus styles explicitly, creating a consistent experience across all browsers.

<!-- Without styling: browser-dependent appearance -->
<input type="text" placeholder="Enter name" />

<!-- With Tailwind: consistent, styled input -->
<input
  type="text"
  placeholder="Enter name"
  class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm
         focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>

Base Input Structure

A well-styled text input combines sizing, border, background, and focus utilities. The core pattern is w-full px-3 py-2 text-sm bg-white border border-gray-300 rounded-lg for the default state.

Add placeholder:text-gray-400 to soften the placeholder text and text-gray-900 to ensure actual input text is dark and readable. The w-full utility ensures the input fills its container, which is the expected behavior for form fields.

<input
  type="text"
  placeholder="Full name"
  class="w-full px-3 py-2 text-sm text-gray-900 bg-white
         border border-gray-300 rounded-lg
         placeholder:text-gray-400"
/>

Focus Ring and Outline

The focus state is the most important interactive style for an input. Users navigating by keyboard need a clear visual indicator of which field is active. Use focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent to replace the browser's default outline with a Tailwind ring.

The focus:border-transparent removes the gray border when focused so the blue ring is not competing with it visually. Add transition-shadow duration-150 for a smooth ring appearance.

<input
  type="email"
  placeholder="you@example.com"
  class="w-full px-3 py-2 text-sm text-gray-900 bg-white
         border border-gray-300 rounded-lg
         focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent
         transition-shadow duration-150"
/>

Label and Input Pairing

Every input must have an associated label for accessibility. Wrap the label and input in a flex flex-col gap-1 container so they stack neatly. The label uses text-sm font-medium text-gray-700 and is connected to the input with matching for and id attributes.

Never rely solely on placeholder text as a label substitute — placeholders disappear when the user starts typing, leaving the field context-less.

<div class="flex flex-col gap-1">
  <label for="email" class="text-sm font-medium text-gray-700">
    Email address
  </label>
  <input
    id="email"
    type="email"
    placeholder="you@example.com"
    class="w-full px-3 py-2 text-sm text-gray-900 bg-white
           border border-gray-300 rounded-lg
           focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
  />
</div>

Input With Icon Prefix

Many inputs benefit from a prefix icon that communicates the expected input type — a search magnifier, an envelope for email, or a lock for passwords. Use relative on the wrapper, absolute left-3 top-1/2 -translate-y-1/2 on the icon, and pl-10 on the input to indent the text past the icon.

This technique keeps the icon and input cleanly aligned without any extra wrapper elements affecting layout.

<div class="relative">
  <svg class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400"
       fill="none" viewBox="0 0 24 24" stroke="currentColor">
    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
          d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/>
  </svg>
  <input
    type="email"
    placeholder="you@example.com"
    class="w-full pl-10 pr-3 py-2 text-sm text-gray-900 bg-white
           border border-gray-300 rounded-lg
           focus:outline-none focus:ring-2 focus:ring-blue-500"
  />
</div>

Input With Suffix Button

A suffix button sits attached to the right edge of an input — common in password fields (show/hide toggle) or search inputs (submit button). Use a flex wrapper with overflow-hidden rounded-lg border border-gray-300 flex, set the input to flex-1 border-0 focus:ring-0, and style the button with a left border separator.

The border-0 on the input removes its border since the outer flex wrapper provides the shared border.

<div class="flex overflow-hidden rounded-lg border border-gray-300 focus-within:ring-2 focus-within:ring-blue-500">
  <input
    type="text"
    placeholder="Search..."
    class="flex-1 px-3 py-2 text-sm text-gray-900 border-0 focus:ring-0 focus:outline-none"
  />
  <button class="px-4 py-2 bg-blue-600 text-white text-sm font-semibold
                 border-l border-gray-300 hover:bg-blue-700 transition-colors">
    Search
  </button>
</div>

Styling Textareas

Textareas use the same utility classes as text inputs. Set a minimum height with min-h-24 or a fixed height with h-32, and add resize-none or resize-y to control whether the user can resize the element.

Use resize-y to allow vertical resizing (the most natural behavior for textareas) and prevent horizontal resizing that can break the layout. Adding leading-relaxed improves readability as users type multi-line content.

<div class="flex flex-col gap-1">
  <label for="message" class="text-sm font-medium text-gray-700">Message</label>
  <textarea
    id="message"
    rows="4"
    placeholder="Write your message here..."
    class="w-full px-3 py-2 text-sm text-gray-900 bg-white
           border border-gray-300 rounded-lg
           resize-y leading-relaxed
           focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
  ></textarea>
</div>

Disabled Input Styling

Disabled inputs should be visually distinguished from active ones. Apply disabled:bg-gray-50 disabled:text-gray-400 disabled:cursor-not-allowed to create a lighter, greyed-out appearance. Also add disabled:border-gray-200 to soften the border.

Always include the HTML disabled attribute — Tailwind's disabled: variant reads this attribute, so the styles only apply when the attribute is present.

<input
  type="text"
  value="Cannot edit this"
  disabled
  class="w-full px-3 py-2 text-sm border border-gray-300 rounded-lg
         disabled:bg-gray-50 disabled:text-gray-400
         disabled:cursor-not-allowed disabled:border-gray-200"
/>

Read-Only Input Style

A readonly input displays a value users can read and copy but not modify. Unlike disabled, read-only inputs are still focusable and keyboard-navigable. Style them with read-only:bg-gray-50 read-only:text-gray-600 to subtly indicate they cannot be edited.

Read-only fields are used for computed values, confirmation fields, or reference data that users need to see but should not change.

<div class="flex flex-col gap-1">
  <label for="ref" class="text-sm font-medium text-gray-700">Reference ID</label>
  <input
    id="ref"
    type="text"
    value="ORD-2026-00847"
    readonly
    class="w-full px-3 py-2 text-sm border border-gray-300 rounded-lg
           read-only:bg-gray-50 read-only:text-gray-600
           focus:outline-none focus:ring-2 focus:ring-gray-400"
  />
</div>

Input Size Variants

Forms in different contexts need different input sizes. Define a small, medium, and large variant by changing the padding and text size. A small input is ideal for inline filters; a large input suits hero search bars.

  • Small: px-2.5 py-1.5 text-xs
  • Medium: px-3 py-2 text-sm (default)
  • Large: px-4 py-3 text-base
<!-- Small -->
<input type="text" placeholder="Small" class="w-full px-2.5 py-1.5 text-xs border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-blue-500" />

<!-- Medium (default) -->
<input type="text" placeholder="Medium" class="w-full px-3 py-2 text-sm border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" />

<!-- Large -->
<input type="text" placeholder="Large" class="w-full px-4 py-3 text-base border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500" />

Character Count Helper Text

Textareas for user-generated content often include a character count displayed below the field. Use a flex row with flex items-center justify-between mt-1 to position helper text on the left and the count on the right.

Style the helper text with text-xs text-gray-500 and the count with matching small gray text. JavaScript updates the count on each input event. When near the limit, switch the count color to text-red-600 as a warning.

<div class="flex flex-col gap-1">
  <label for="bio" class="text-sm font-medium text-gray-700">Bio</label>
  <textarea
    id="bio"
    maxlength="160"
    rows="3"
    oninput="document.getElementById('count').textContent = this.value.length"
    class="w-full px-3 py-2 text-sm border border-gray-300 rounded-lg resize-none
           focus:outline-none focus:ring-2 focus:ring-blue-500"
  ></textarea>
  <div class="flex items-center justify-between">
    <span class="text-xs text-gray-500">Tell us about yourself</span>
    <span class="text-xs text-gray-400"><span id="count">0</span>/160</span>
  </div>
</div>

Quick Check

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

Lesson Recap

In this lesson you learned: base input styling with border border-gray-300 rounded-lg px-3 py-2; focus rings replacing browser outlines using focus:outline-none focus:ring-2 focus:ring-blue-500; and special input patterns like icon prefixes with absolute positioning, suffix buttons using flex layout, and disabled/read-only variants. Next up we style select menus and checkboxes.

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

บทเรียน “การตกแต่งช่องกรอกข้อความและพื้นที่ข้อความ” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การตกแต่งช่องกรอกข้อความและพื้นที่ข้อความ”

ใช้ยูทิลิตีเส้นขอบ ระยะขอบด้านใน วงแหวน และโฟกัสกับช่องกรอกข้อความและพื้นที่ข้อความ เพื่อสร้างฟิลด์ฟอร์มที่สะอาดและเข้าถึงได้ คุณปฏิบัติ 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