表單是 Web 應用程序中最基本的用戶交互方式之一,而且是處理數據非常重要的一環。因此,表單交互體驗對於用戶的感受和使用體驗非常重要。CSS input 樣式可以讓表單看起來更漂亮、易於使用,同時能夠提高交互體驗。
一、input 樣式基礎
input 元素是表單中最常見也最重要的元素之一,因此為它設置好樣式是非常有必要的。我們常常會看到兩種基礎樣式,一種是有邊框的實線樣式,另一種是沒有邊框的樣式。
/* 實線樣式 */ input { border: 1px solid #ccc; padding: 5px 10px; border-radius: 4px; outline: none; } /* 無邊框樣式 */ input { border: none; border-bottom: 1px solid #ccc; padding: 5px 10px; outline: none; }
前者通過實線邊框,讓 input 四周有明顯的邊界,同時通過圓角讓邊框看起來更加平滑。而後者則只有下邊框,讓 input 更簡潔、明快。其中 outline 屬性能夠去除 input 獲得焦點時的默認外邊框。
二、input 狀態樣式
在交互中,input 會經歷不同的狀態,如獲得焦點、失去焦點、被禁用、滑鼠移上去等。合適的樣式能夠讓用戶更好地了解當前的狀態以及相應的行為。
下面給出不同狀態時 input 的樣式:
/* 獲得焦點樣式 */ input:focus { border-color: #1E90FF; box-shadow: 0 0 5px #1E90FF; } /* 失去焦點樣式 */ input:blur { border-color: #ccc; box-shadow: none; } /* 禁用狀態樣式 */ input:disabled { opacity: 0.5; cursor: not-allowed; } /* 滑鼠懸停時樣式 */ input:hover { border-color: #999; cursor: pointer; }
其中,獲得焦點樣式中的 box-shadow 屬性可以讓 input 邊框周圍產生光暈效果,讓用戶更容易定位當前控制項。禁用狀態樣式中的 cursor 屬性能夠告訴用戶此時禁止操作。
三、input 類型樣式
除了基礎樣式和狀態樣式,不同類型的 input 也需要相應的樣式來區分它們的作用。下面是常見 input 類型的樣式實現:
/* 文本輸入框 */ input[type="text"] { /* 樣式 */ } /* 密碼輸入框 */ input[type="password"] { /* 樣式 */ } /* 數字輸入框 */ input[type="number"] { /* 樣式 */ } /* 單選框 */ input[type="radio"] { /* 樣式 */ } /* 多選框 */ input[type="checkbox"] { /* 樣式 */ }
通過使用 input[type=”type”] 的選擇器,可以自定義不同類型 input 的樣式,例如文本輸入框的樣式可以讓背景色和字體顏色一致,看起來更具統一性;而單選框和多選框的樣式需要利用偽元素來模擬勾選和未勾選的狀態。
四、input 的寬度樣式
在表單交互中,input 的寬度往往是非常重要的。適當增加 input 的寬度,可以讓用戶更容易輸入和查看文本。下面是一些常見的 input 寬度樣式實現:
/* 固定像素寬度 */ input { width: 200px; } /* 百分比寬度 */ input { width: 50%; } /* 彈性盒子模型 */ input { flex: 1; } /* 水平自適應 */ input { display: block; width: 100%; box-sizing: border-box; } /* 水平自適應 + 向內縮進 */ input { display: block; width: 100%; box-sizing: border-box; padding: 10px; }
其中,固定像素寬度和百分比寬度可以讓 input 的寬度具有預見性;彈性盒子模型能夠根據不同的布局,動態設定不同 input 的比例;水平自適應則能夠讓 input 隨著容器寬度自動調整寬度。
五、input 的自動填充樣式
隨著瀏覽器的不斷升級,自動填充功能在表單中的應用也越來越普遍。自動填充應用於 input 中,可以大大提高用戶使用體驗,在輸入表單前就將一些常見的和個人信息填寫上,從而減少操作。
下面是自動填充樣式的實現:
input:-webkit-autofill, input:-webkit-autofill:hover, input:-webkit-autofill:focus, input:-webkit-autofill:active { -webkit-box-shadow: 0 0 0 30px white inset !important; }
當瀏覽器自動填寫 input 時,會在 input 上添加 -webkit-autofill 屬性,我們可以利用該屬性控制樣式。上述代碼中,將 input 設置為白色底色,並添加內陰影效果,讓 input 看起來更加美觀和清晰。
六、總結
通過以上的介紹和實現,相信大家能夠加深對於表單交互和 input 樣式的理解和掌握。通過優化表單樣式和交互,能夠大大提高用戶的使用體驗和轉化率。
下面是本文所使用的完整代碼示例:
表單 input 樣式優化 /* 實線樣式 */ input { border: 1px solid #ccc; padding: 5px 10px; border-radius: 4px; outline: none; } /* 無邊框樣式 */ input { border: none; border-bottom: 1px solid #ccc; padding: 5px 10px; outline: none; } /* 獲得焦點樣式 */ input:focus { border-color: #1E90FF; box-shadow: 0 0 5px #1E90FF; } /* 失去焦點樣式 */ input:blur { border-color: #ccc; box-shadow: none; } /* 禁用狀態樣式 */ input:disabled { opacity: 0.5; cursor: not-allowed; } /* 滑鼠懸停時樣式 */ input:hover { border-color: #999; cursor: pointer; } /* 文本輸入框 */ input[type="text"] { /* 樣式 */ } /* 密碼輸入框 */ input[type="password"] { /* 樣式 */ } /* 數字輸入框 */ input[type="number"] { /* 樣式 */ } /* 單選框 */ input[type="radio"] { /* 樣式 */ } /* 多選框 */ input[type="checkbox"] { /* 樣式 */ } /* 固定像素寬度 */ input { width: 200px; } /* 百分比寬度 */ input { width: 50%; } /* 彈性盒子模型 */ input { flex: 1; } /* 水平自適應 */ input { display: block; width: 100%; box-sizing: border-box; } /* 水平自適應 + 內縮進 */ input { display: block; width: 100%; box-sizing: border-box; padding: 10px; } /* 自動填充樣式 */ input:-webkit-autofill, input:-webkit-autofill:hover, input:-webkit-autofill:focus, input:-webkit-autofill:active { -webkit-box-shadow: 0 0 0 30px white inset !important; }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/151527.html