Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

สร้าง Custom Radio Buttons ด้วย CSS appearance Property

Radio Buttons เริ่มต้นของ Browser ยากต่อการจัดสไตล์ ใช้ appearance: none ปิด Native Rendering แล้วสร้าง UI เองด้วย CSS (Border, Background, Pseudo-elements) ตัวอย่างนี้แสดง 2 รูปแบบ: Radio Button กลม และ Checkbox Style Toggle

Syntax

selector {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  /* Custom Styles */
}

ตัวอย่างที่ 1: Radio Button กลมแบบเรียบง่าย

<!DOCTYPE html>
<html>
<style>
  input[type=radio] {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    width: 20px;
    height: 20px;
    padding: 10px;
    background-color: orange;
    border-radius: 50%;
  }
  input[type=radio]:checked {
    background-color: magenta;
  }
  input[type=radio]:hover {
    cursor: pointer;
  }
</style>
<body>
  Custom radio button example<br/>
  <form>
    <label for="r1">r1</label>
    <input type="radio" id="r1" name="btn" value="v1">
    <input type="radio" id="r2" name="btn" value="v2">
    <label for="r2">r2</label>
  </form>
</body>
</html>

วิเคราะห์

  • appearance: none - ปิด Native Radio
  • width/height + border-radius: 50% - สร้างวงกลม
  • :checked - สีพื้นหลังเปลี่ยนเมื่อเลือก
  • ปัญหา: ไม่มี "Dot" ภายในแสดงสถานะ Checked (แค่เปลี่ยนสีพื้นหลัง)

ตัวอย่างที่ 2: Checkbox Style Toggle (Switch)

<!DOCTYPE html>
<html>
<style>
  label {
    display: flex;
    align-items: center;
    gap: 10px;
  }
  input[type=checkbox] {
    appearance: none;
    -moz-appearance: none;
    -webkit-appearance: none;
    width: 60px;
    height: 30px;
    border: 3px ridge currentcolor;
    border-radius: 60px;
    box-sizing: content-box;
    color: lightblue;
    transition: border-color 280ms, color 280ms;
    position: relative;
  }
  input[type=checkbox]::after {
    content: "";
    position: absolute;
    top: 2px; left: 2px;
    width: 20px; height: 20px;
    border-radius: 10px;
    background-color: currentcolor;
    transform: translateY(0);
    transition: transform 300ms ease-in;
  }
  input[type=checkbox]:checked {
    color: lightgreen;
  }
  input[type=checkbox]:checked::after {
    transform: translateY(40px); /* ขยับไปขวา */
  }
</style>
<body>
  <p>Custom radio button example<br/>
    <label for="crb">Blue <input id="crb" type="checkbox"/> Green</label>
  </p>
</body>
</html>

เทคนิคสำคัญ

  • currentColor - ใช้สีจาก color property เดียวกัน ทำให้สลับสีง่าย
  • ::after - สร้าง "Knob" (ปุ่มกลม) ที่เลื่อนได้
  • transform: translateY(40px) - เลื่อน Knob (ในที่นี้แนวตั้ง แต่ควรเป็น translateX สำหรับ Switch แนวนอน)
  • transition - Animation นุ่มนวล

Radio Button แบบสมบูรณ์ (มี Inner Dot)

.radio-group {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.radio-option {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  cursor: pointer;
}

.radio-option input[type="radio"] {
  appearance: none;
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  border: 2px solid #ccc;
  border-radius: 50%;
  position: relative;
  flex-shrink: 0;
  transition: border-color 0.2s, background 0.2s;
}

.radio-option input[type="radio"]:hover {
  border-color: #4285f4;
}

.radio-option input[type="radio"]:checked {
  border-color: #4285f4;
}

.radio-option input[type="radio"]::before {
  content: "";
  position: absolute;
  top: 50%; left: 50%;
  width: 10px; height: 10px;
  border-radius: 50%;
  background: #4285f4;
  transform: translate(-50%, -50%) scale(0);
  transition: transform 0.15s ease;
}

.radio-option input[type="radio"]:checked::before {
  transform: translate(-50%, -50%) scale(1);
}

.radio-option input[type="radio"]:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(66,133,244,0.3);
}

HTML Structure

<fieldset class="radio-group">
  <legend>Choose an option</legend>
  <label class="radio-option">
    <input type="radio" name="option" value="1">
    <span>Option One</span>
  </label>
  <label class="radio-option">
    <input type="radio" name="option" value="2">
    <span>Option Two</span>
  </label>
  <label class="radio-option">
    <input type="radio" name="option" value="3">
    <span>Option Three</span>
  </label>
</fieldset>

Accessibility Checklist

  • ใช้ <label> ผูกกับ <input> (for/id หรือ Wrap)
  • Group Radio Buttons ด้วย <fieldset> + <legend>
  • name attribute ต้องเหมือนกันใน Group เดียว
  • :focus-visible สำหรับ Keyboard Navigation
  • Color Contrast ต้องผ่าน WCAG (Border, Background, Dot)
  • รองรับ prefers-reduced-motion ปิด Animation

CSS-only Toggle Switch (ไม่ต้อง JS)

.toggle {
  position: relative;
  width: 60px; height: 30px;
  appearance: none;
  -webkit-appearance: none;
  background: #ccc;
  border-radius: 30px;
  outline: none;
  cursor: pointer;
  transition: background 0.3s;
}
.toggle::before {
  content: "";
  position: absolute;
  top: 3px; left: 3px;
  width: 24px; height: 24px;
  background: white;
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(0,0,0,0.2);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.toggle:checked {
  background: #4285f4;
}
.toggle:checked::before {
  transform: translateX(30px);
}
.toggle:focus-visible::after {
  content: "";
  position: absolute;
  inset: -3px;
  border-radius: 33px;
  box-shadow: 0 0 0 3px rgba(66,133,244,0.4);
}