咱們知道select標籤在各個瀏覽器中的屬性和各瀏覽器的支持各有些不一樣,從而形成select選擇框在各瀏覽器的顯示有不一樣,下面咱們經過對主要 外形CSS屬性的支持,打造全兼容select。html
我對select的height、padding、line-height分別利用控制變量的方法寫了個DEMO在各瀏覽器上測試三種狀況:height.100.padding.0、height.no.padding.100、no.height.no.padding,結果如 連接圖片各瀏覽器DEMO外觀瀏覽器
咱們能夠得出如下研究屬性。測試
ie6 | ie7 | ie8 | ie9 | ff | ch | sf | op | |
默認高度 | 22px | 22px | 19px | 20px | 19px | 19px | ||
height | F | T | T | T | T | T | F | T |
padding | F | F | T | T | T | T | F | T |
line-height | F | F | F | F | F | F | T | F |
文字垂直居中 | T | T | T | F | F | T | T | T |
經過上述的研究成果屬性彙總,咱們知道IE6是不管如何設置都是固定高度爲22px不變的,而其餘瀏覽器除safari都是支持height屬性的,那麼咱們設置 height:22px。那麼如今咱們修正一下safari瀏覽器,,咱們發現僅有safari支持line-height屬性那麼正好能夠利用line-height修正其高度爲22px,在font-size爲12px的前提下設置 line-height:22px,最後FF和IE9裏面的文字不居中,對其設定 padding:2px 0,咱們發現FF和IE9都居中了,可是各個瀏覽器的select的高度也並無增長,那麼這裏有點疑問,在高度設定的狀況下,小量數字的padding不增長總體高度?ui
下面是全兼容代碼示例。spa
<!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=utf-8" />
<title>demo</title>
<style>
*{padding:0; margin:0}
body{font-size:12px}
select{height:22px; line-height:18px; padding:2px 0}
</style>
</head>
<body>
<div style="margin-top:20px; margin-left:20px; background:#000">
<select>
<option>演示問題一</option>
<option>演示問題二</option>
<option>演示問題三</option>
<option>演示問題四</option>
<option>演示問題五</option>
</select>
</div>
</body>
</html>
另外,若是你想要設置寬度或高度,還想要實如今各瀏覽器中兼容的效果,那麼你最好使用js作出下拉效果,這就和select標籤沒有關係了。xml
淘寶使用的是js效果實現的下拉效果;而京東使用的是select,但其高度都在20px左右,因此文字的效果看上去是垂直居中的,在Google中你能夠看到改變高度文字仍是居中,可是在FF下文字就偏上了,因此要使用select的話,最好仍是按照上面的兼容性寫法吧。htm