DIV+CSS專題: CSS表單設計


css表單設計,包含以下內容和知識點:

改變文本框和文本域樣式
用圖片美化按鈕
改變下拉列表樣式
用label標籤提升用戶體驗

一、改變文本框和文本域樣式
      如果前邊幾章學習的比較紮實的話,本節教程就相當容易了。下邊先說一下文本框,文本框和文本域都是可以用css進行美化的。比如改變邊框精細,顏色,添加背景色、背景圖像等。請看下邊的實例:



text1 { border:1px solid #f60; color:#03C;}
text2 { border:2px solid #390; width:200px; height:24px; font-size:16px; font-weight:bold; line-height:1.6;}
text3 { border:2px solid #C3C; height:20px; background:url(icon9.gif) right 3px no-repeat;}
text4 { border:2px solid #F60; width:150px; height:29px;font-size:16px; line-height:1.6; background:url(bg_9.gif) 0 0 no-repeat;}
      這四個樣式表分別對應第2、3、4、5行表單,第一行是文本框的默認樣式;第二行爲設置邊框和字體顏色的樣式;第三行爲設置邊框、寬度、高度、字體大小、行高的樣式;第四行設置邊框和增加背景色和背景圖片;第五行爲增加一個gif動畫的背景圖片,看起來是不是生動許多,具體步驟不再贅述。下面我們看一下文本域的樣式設置:



area { border:1px solid #F90; overflow:auto; background:#fff url(bg_9_1.gif) right bottom no-repeat; width:99%; height:100px;}
      上圖中第一個爲默認的文本域樣式,第二個爲設置邊框、寬度爲百分比、高度和景圖片。overflow:auto定義當內容不超過現在文本域高度時不出現滾動條。好了,下面運行一下代碼看看兩者的效果吧.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style type="text/css">
.text1 { border:1px solid #f60; color:#03C;}
.text2 { border:2px solid #390; width:200px; height:24px; font-size:16px; font-weight:bold; line-height:1.6;}
.text3 { border:2px solid #C3C; height:20px; background:#ffeeff url(/upload/2010-08/28/icon9.gif) right 3px no-repeat;}
.text4 { border:2px solid #F60; width:150px; height:29px;font-size:16px; line-height:1.6; background:url(/upload/2010-08/28/bg_9.gif) 0 0 no-repeat;}
.area { border:1px solid #F90; overflow:auto; background:#fff url(/upload/2010-08/28/bg_9_1.gif) right bottom no-repeat; width:99%; height:100px;}
</style>
</head>
<body>
<p>
<input type="text" name="textfield" id="textfield" /> 
這是默認樣式
</p>
<p>
<input name="textfield2" type="text" class="text1" id="textfield2" value="我是藍色的" /> 
這是改變邊框的樣式和文字顏色
</p>
<p>
<input name="textfield3" type="text" class="text2" id="textfield3" value="看我大吧" />
這是改變邊框並設置高寬和字體大小的樣式
</p>
<p>
<input class="text3" type="text" name="textfield4" id="textfield4" />
這是增加背景圖片實例,也可放左側
</p>
<p>
<input class="text4" type="text" name="textfield5" id="textfield5" /> 
這是增加了一個背景圖片爲gif動畫
</p>
<p>
<textarea name="textarea" id="textarea" cols="45" rows="5"></textarea>
</p>
<p>
<textarea class="area" name="textarea2" id="textarea2" cols="45" rows="5"></textarea>
</p>
</body>
</html>


提示:可以先修改部分代碼後再運行
二、用圖片美化按鈕
      按鈕也是網頁中經常見的元素,但默認的樣式有時候和頁面整體效果不協調,需要把它美化一下,它的樣式設置和文本框如出一轍,沒有什麼特別之處。下面以三個實例來說明一下:



btn02 { background:#fff url(btn_bg2.gif) 0 0; height:22px; width:55px; color:#297405; border:1px solid #90be4a; font-size:12px; font-weight:bold; line-height:180%; cursor:pointer;}
btn04 { background:url(btn_bg2.gif) 0 -24px; width:70px; height:22px; color:#9a4501; border:1px solid #dbb119; font-size:12px; line-height:160%; cursor:pointer;}
btn07 { background:url(submit_bg.gif) 0px -8px; border:1px solid #cfab25; height:32px; font-weight:bold; padding-top:2px; cursor:pointer; font-size:14px; color:#660000;}
btn08 { background:url(submit_bg.gif) 0px -64px; border:1px solid #8b9c56; height:32px; font-weight:bold; padding-top:2px; cursor:pointer; font-size:14px; color:#360;}
btn09 { background:url() 0 0 no-repeat; width:107px; height:37px; border:none; font-size:14px; font-weight:bold; color:#d84700; cursor:pointer;}
圖中的按鈕,前兩個爲固定寬度,但寬度可以根據需要隨意調整;中間兩個爲自適應寬度,根據字數多少去適應;這四個樣式都是採用一個背景圖片橫向循環實現,所以寬度不受限制,最後一個完全採用背景圖片,這樣寬度就得固定死了,要不會影響美觀。需要注意的是這種方式需要去掉按鈕邊框。

採用以上的按鈕有一個好處是當css樣式表沒有加載上時,將會顯示爲默認按鈕樣式,這樣用戶可以清楚地知道這是個按鈕,正常加載後,會使按鈕更加美觀。它和我們第五天講的css按鈕有所不同,那裏的按鈕實際還是個鏈接,而這裏的是按鈕元素。注:不同瀏覽器下顯示效果略有不同。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style type="text/css">
.btn02 { background:#fff url(/upload/2010-08/28/btn_bg2.gif) 0 0; height:22px; width:55px; color:#297405; border:1px solid #90be4a; font-size:12px; font-weight:bold; line-height:180%; cursor:pointer;}
.btn04 { background:url(/upload/2010-08/28/btn_bg2.gif) 0 -24px; width:70px; height:22px; color:#9a4501; border:1px solid #dbb119; font-size:12px; line-height:160%; cursor:pointer;}
.btn07 { background:url(/upload/2010-08/28/submit_bg.gif) 0px -8px; border:1px solid #cfab25; height:32px; font-weight:bold; padding-top:2px; cursor:pointer; font-size:14px; color:#660000;}
.btn08 { background:url(/upload/2010-08/28/submit_bg.gif) 0px -64px; border:1px solid #8b9c56; height:32px; font-weight:bold; padding-top:2px; cursor:pointer; font-size:14px; color:#360;}
.btn09 { background:url() 0 0 no-repeat; width:107px; height:37px; border:none; font-size:14px; font-weight:bold; color:#d84700; cursor:pointer;}
</style>
</head>
<body>
<p>
<input name="button" type="submit" class="btn02" id="button" value="提交" />
</p>
<p>
<input name="button2" type="submit" class="btn04" id="button2" value="提交" />
</p>
<p>
<input name="button" type="submit" class="btn07" id="button" value="提交" />
</p>
<p>
<input name="button2" type="submit" class="btn08" id="button2" value="看看我的寬度有多寬" />
</p>
<p>
<input name="button" type="submit" class="btn09" id="button" value="免費註冊" />
</p>
</body>
</html>


提示:可以先修改部分代碼後再運行
三、改變下拉列表樣式
下拉列表(菜單)是大家最頭疼的一個元素,因其對許多樣式不生效,故而在頁面中顯示很醜陋,而且在IE6下總在最上層,造成許多彈出層不能把其遮擋(可惡的IE6呀),使頁面的用戶檢驗大打折扣,所以一些設計師想出了許多辦法來改變這種情況。

IE6下對下拉列表的背景和寬度樣式生效,其它絕大部分不生效,IE8下增加了對邊框和高度的支持。但這樣似乎離我們要求還很遠,所以不得不尋求其它的辦法了。先看下面三個圖,同一代碼分別在IE6、FF、IE8下顯示的差異吧。







<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style type="text/css">
.select { border:1px solid #f60; background:#FF9; height:30px;}
.tip { width:50px; border:1px solid #ccc; background:#fff; position:absolute; top:5px; left:70px; font-size:12px; height:100px; padding:5px;}
</style>
</head>
<body>
<p>請選擇項目:</p>
<p>
<select name="select" id="select" class="select">
<option>請選擇----------------</option>
<option>標準之路</option>
</select>
</p>
<div class="tip">看看誰能把我擋着</div>
</body>
</html>
提示:可以先修改部分代碼後再運行
在三個瀏覽器下顯示都不盡相同,所以最好是尋求其它的辦法或者使用默認樣式了。基中IE6下被遮擋可以把浮動層用iframe,因下拉列表不會跑到iframe上邊。有更高美化需求的可以用div模擬來代替下拉列表,但這種方法實現起來麻煩。
四、用label標籤提升用戶體驗
label標籤常常被大家忽略了,合理利用會使頁面的用戶體驗得到提升,我們可以對錶單的說明文字使用label標籤,這樣當用戶點擊文字時,光標就定位到表單上了




如上圖,當鼠標點擊姓名文字時,光標就會定位到後邊的文本框上了;點擊男女文字也會選中相應的選項;同理,複選框和文本域也是一樣的。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style type="text/css">
</style>
</head>
<body>
<p>
<label for="name">姓名:</label>
<input type="text" name="name" id="name" />
</p>
<p>性別:
<input type="radio" name="sex" id="male" value="radio" /><label for="male">男</label>
<input type="radio" name="sex" id="female" value="radio2" /><label for="female">女</label></p>
<p>愛好:
<input type="checkbox" name="music" id="music" /><label for="music">聽音樂</label> 
<input type="checkbox" name="web" id="web" /><label for="web">上網</label> 
<input type="checkbox" name="book" id="book" /><label for="book">看書</label></p>
<p>
<label for="content">簡歷:</label>
<textarea name="content" id="content" cols="45" rows="5"></textarea>
</p>
<p> </p>
<p>  </p>
</body>
</html>

html5博客模板

文章來源:http://www.huiyi8.com/sc/9523.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章