CSS appearance property ใช้ควบคุมการแสดงผล Native UI ของ Browser (เช่น Dropdown Arrow, Spin Buttons, Checkbox, Radio) ตัวอย่างนี้แสดงวิธีซ่อน Arrow ของ <select> และ Spin Buttons ของ <input type="number">
Syntax
selector {
appearance: none;
-webkit-appearance: none; /* Safari, Chrome */
-moz-appearance: none; /* Firefox */
}
ตัวอย่างที่ 1: ซ่อน Spin Buttons ของ Input Number
<!DOCTYPE html>
<html>
<head>
<style>
input[type=number] {
width: 40px;
-moz-appearance: textfield; /* Firefox */
}
input[type=number]:hover {
background-color: #e3f5a1;
}
input::-webkit-inner-spin-button {
-webkit-appearance: none; /* Chrome, Safari, Edge */
}
</style>
</head>
<body>
<p>Type/Scroll to change value</p>
<input type="number" value="6">
</body>
</html>
คำอธิบาย
-moz-appearance: textfield- Firefox ใช้ค่านี้เพื่อซ่อน Spinnerinput::-webkit-inner-spin-button- Pseudo-element สำหรับ WebKit/Blink (Chrome, Safari, Edge)-webkit-appearance: none- ปิด Native Appearance
ตัวอย่างที่ 2: ซ่อน Dropdown Arrow ของ Select
<!DOCTYPE html>
<html>
<head>
<style>
select {
width: 20%;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
}
</style>
</head>
<body>
Hiding dropdown arrow<br/><br/>
<select>
<option value="none" selected disabled hidden>Select color</option>
<option>Red</option>
<option>Blue</option>
<option>Green</option>
<option>Yellow</option>
<option>Orange</option>
</select>
</body>
</html>
Custom Dropdown Arrow (แทนที่ Arrow เริ่มต้น)
select {
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='https://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
background-repeat: no-repeat;
background-position: right 0.75rem center;
background-size: 1.25rem;
padding-right: 2.5rem; /* พื้นที่สำหรับ Arrow */
}
Custom Select แบบสมบูรณ์ (Fully Styled)
.custom-select {
position: relative;
display: inline-block;
width: 100%;
max-width: 300px;
}
.custom-select select {
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
width: 100%;
padding: 0.75rem 2.5rem 0.75rem 1rem;
border: 1px solid #ddd;
border-radius: 6px;
background: white;
font-size: 1rem;
cursor: pointer;
transition: border-color 0.2s, box-shadow 0.2s;
}
.custom-select select:hover { border-color: #999; }
.custom-select select:focus {
outline: none;
border-color: #4285f4;
box-shadow: 0 0 0 3px rgba(66,133,244,0.2);
}
.custom-select::after {
content: "";
position: absolute;
right: 1rem; top: 50%;
transform: translateY(-50%);
pointer-events: none;
width: 0; height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 6px solid #666;
}
Browser-Specific Selectors
| Browser | Pseudo-element | Property |
|---|---|---|
| Chrome/Safari/Edge | ::-webkit-inner-spin-button | Number Input Spinner |
| Chrome/Safari/Edge | ::-webkit-calendar-picker-indicator | Date Input Calendar Icon |
| Firefox | ::-moz-focus-inner | Focus Inner Border |
| Firefox | -moz-appearance | Appearance Property |
| IE/Edge Legacy | ::-ms-expand | Select Dropdown Arrow |
| IE/Edge Legacy | ::-ms-clear | Input Clear Button (X) |
| IE/Edge Legacy | ::-ms-reveal | Password Reveal Button |
Accessibility Warning
- เมื่อซ่อน Native Arrow ต้องใส่ Custom Arrow ให้ชัดเจน
- Select ต้องยังคง Keyboard Accessible (Tab, Enter, Space, Arrow Keys)
- Screen Readers ต้องอ่านค่า Selected ได้
- Focus Indicator ต้องชัดเจน (
:focus-visible)
Alternative: Native <select> Styling Limitations
<select> ยากต่อการ Style ให้สวยสมบูรณ์ (Option Styling จำกัดมาก) พิจารณาใช้ Custom Select Component (ARIA Listbox Pattern) หรือ Library เช่น:
- Select2, Choices.js, Tom Select (jQuery/Vanilla)
- Headless UI (React/Vue), Radix UI, React Aria
- Native
<selectmenu>(Experimental, Chrome Canary)