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

รูปแบบและสถานะของปุ่ม

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

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

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

Why Button Variants Matter

Buttons are one of the most interactive elements on a page, and users rely on visual cues to understand their purpose. Button variants communicate intent: a primary button drives the main action, a secondary button provides an alternative, and an outline or ghost button offers a subtle option.

With Tailwind, you achieve all of these without writing any custom CSS. Each variant is just a different combination of utility classes applied to the same <button> element.

<button class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700">
  Primary Button
</button>

The Primary Button Pattern

A primary button uses a solid background color to attract maximum attention. The pattern combines padding utilities, a background color, white text, and a rounded corner class.

Adding hover:bg-blue-700 darkens the button on hover, providing clear feedback that the element is interactive. The font-semibold class increases the visual weight of the label.

<button class="px-5 py-2.5 bg-blue-600 text-white font-semibold rounded-lg
           hover:bg-blue-700 active:bg-blue-800
           focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2
           transition-colors duration-150">
  Get Started
</button>

Secondary and Outline Variants

A secondary button uses a lighter background to signal a less prominent action. An outline button drops the fill entirely and shows only a border, using border border-blue-600 text-blue-600 on a transparent background.

On hover, outline buttons can fill in with a light background like hover:bg-blue-50 to give visual feedback without overwhelming the primary button nearby.

<!-- Secondary -->
<button class="px-5 py-2.5 bg-blue-100 text-blue-700 font-semibold rounded-lg
           hover:bg-blue-200 transition-colors duration-150">
  Learn More
</button>

<!-- Outline -->
<button class="px-5 py-2.5 border border-blue-600 text-blue-600 font-semibold rounded-lg
           hover:bg-blue-50 transition-colors duration-150">
  View Details
</button>

The Ghost Button Variant

A ghost button has no background and no visible border by default, appearing as styled text until the user interacts with it. This variant is ideal for low-emphasis actions like cancel or dismiss.

Applying hover:bg-gray-100 reveals a subtle fill on hover, and using text-gray-700 keeps the label readable without drawing attention away from primary actions.

<button class="px-5 py-2.5 text-gray-700 font-semibold rounded-lg
           hover:bg-gray-100 active:bg-gray-200
           transition-colors duration-150">
  Cancel
</button>

Destructive and Danger Buttons

A destructive button uses red tones to warn users about irreversible actions like deleting data. The color signals danger without the user needing to read the label carefully.

Combine bg-red-600 with a strong hover state hover:bg-red-700 and always pair the button with a confirmation dialog to prevent accidental actions.

<button class="px-5 py-2.5 bg-red-600 text-white font-semibold rounded-lg
           hover:bg-red-700 active:bg-red-800
           focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2
           transition-colors duration-150">
  Delete Account
</button>

Disabled State Styling

When a button is not yet actionable — for example, while a form is being submitted — you should visually communicate this with a disabled state. Tailwind provides the disabled: variant prefix for exactly this purpose.

Apply disabled:opacity-50 disabled:cursor-not-allowed to dim the button and change the cursor, signaling that it cannot be clicked. Always use the HTML disabled attribute alongside these classes for proper accessibility.

<button
  disabled
  class="px-5 py-2.5 bg-blue-600 text-white font-semibold rounded-lg
         disabled:opacity-50 disabled:cursor-not-allowed
         hover:bg-blue-700 transition-colors duration-150">
  Submitting...
</button>

Loading State With Spinner

A common pattern is showing a loading spinner inside a button while an async operation completes. You can use Tailwind's animate-spin on an SVG icon placed before the label text.

Use inline-flex items-center gap-2 on the button to align the spinner and text side by side, and add pointer-events-none while loading to prevent double submissions.

<button class="inline-flex items-center gap-2 px-5 py-2.5
           bg-blue-600 text-white font-semibold rounded-lg
           pointer-events-none opacity-80">
  <svg class="w-4 h-4 animate-spin" fill="none" viewBox="0 0 24 24">
    <circle class="opacity-25" cx="12" cy="12" r="10"
            stroke="currentColor" stroke-width="4"></circle>
    <path class="opacity-75" fill="currentColor"
          d="M4 12a8 8 0 018-8v8z"></path>
  </svg>
  Saving...
</button>

Button Sizes With Tailwind

Different contexts call for different button sizes. A small button is useful in dense UIs like tables, while an extra-large button is effective as a hero CTA.

Define sizes by varying the px-*, py-*, and text-* values. A common scale is sm (px-3 py-1.5 text-sm), md (px-5 py-2.5 text-base), and lg (px-6 py-3 text-lg).

<!-- Small -->
<button class="px-3 py-1.5 text-sm bg-blue-600 text-white rounded-md">Small</button>

<!-- Medium (default) -->
<button class="px-5 py-2.5 text-base bg-blue-600 text-white rounded-lg">Medium</button>

<!-- Large -->
<button class="px-6 py-3 text-lg bg-blue-600 text-white rounded-xl">Large</button>

Icon Buttons and Icon-Plus-Text

Icon buttons use only an icon without a visible label, so they must have an accessible aria-label attribute. Style them as a square with equal padding: p-2 creates a compact clickable area around the icon.

For icon-plus-text buttons, use inline-flex items-center gap-2 to align the SVG icon vertically with the text label at any font size.

<!-- Icon only -->
<button aria-label="Delete" class="p-2 text-gray-500 hover:text-red-600 hover:bg-red-50 rounded-lg transition-colors">
  <svg class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
          d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6M9 7h6m2 0H7"/>
  </svg>
</button>

<!-- Icon + text -->
<button class="inline-flex items-center gap-2 px-4 py-2 bg-green-600 text-white rounded-lg">
  <svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
  </svg>
  Add Item
</button>

Full-Width and Group Buttons

A full-width button uses w-full to stretch across its container, common in mobile layouts and form submissions. Adding justify-center ensures the label stays centered.

A button group connects multiple buttons side by side. Remove the border radius from middle buttons and apply negative margins to merge borders: first:rounded-l-lg last:rounded-r-lg border-r-0 creates a unified pill appearance.

<!-- Full width -->
<button class="w-full flex justify-center px-5 py-2.5 bg-blue-600 text-white font-semibold rounded-lg">
  Sign In
</button>

<!-- Button group -->
<div class="inline-flex">
  <button class="px-4 py-2 border border-gray-300 text-gray-700 rounded-l-lg hover:bg-gray-50">Left</button>
  <button class="px-4 py-2 border-t border-b border-gray-300 text-gray-700 hover:bg-gray-50">Middle</button>
  <button class="px-4 py-2 border border-gray-300 text-gray-700 rounded-r-lg hover:bg-gray-50">Right</button>
</div>

Consistent Focus Rings for Accessibility

Every button variant needs a visible focus indicator for keyboard users. Tailwind's focus:ring-* utilities render a colored ring outside the button border without affecting layout.

A good pattern is focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2. The ring-offset-2 adds a small white gap between the button and the ring, making it pop on colored backgrounds. Use focus-visible:ring-2 to show the ring only when navigating with a keyboard.

<button class="px-5 py-2.5 bg-purple-600 text-white font-semibold rounded-lg
           focus:outline-none focus-visible:ring-2
           focus-visible:ring-purple-500 focus-visible:ring-offset-2
           hover:bg-purple-700 transition-colors">
  Accessible Button
</button>

Quick Check

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

Lesson Recap

In this lesson you learned: primary, secondary, outline, and ghost button variants are built by combining background, border, and text color utilities; interactive states like hover, active, disabled, and loading are handled with Tailwind variant prefixes; and focus rings using focus-visible:ring-2 ensure keyboard accessibility without cluttering mouse interactions. Next up we explore card component patterns.

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

บทเรียน “รูปแบบและสถานะของปุ่ม” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “รูปแบบและสถานะของปุ่ม”

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