一個好看的搜尋框通常由容器、輸入框、提示文字與送出按鈕組成。容器負責背景與內距,輸入框負責文字輸入,提示文字則在未取得焦點時顯示引導語。
.search {
padding: 5px 10px;
width: 280px;
background: #1A93CC;
float: left;
position: relative;
}
.search .input {
width: 180px;
height: 16px;
border: 1px solid #0086C6;
background: #fff url(search_btn.gif) no-repeat right 0;
}
.label {
position: absolute;
left: 12px;
top: 6px;
color: #ccc;
}
span.submit {
position: absolute;
left: 171px;
top: 6px;
width: 20px;
height: 20px;
text-indent: -999em;
cursor: pointer;
}
當輸入框獲得焦點時,可隱藏提示文字並切換背景位置;失去焦點且內容為空時,再顯示提示文字。送出按鈕則可用單純的表單提交或少量 JavaScript 觸發,讓搜尋框兼具視覺與可用性。