背景画像を指定すると、何らかの理由で、最新のブラウザーのほとんどは、デフォルトの入力枠スタイルをテキスト ボックスに適用しなくなります。代わりに、醜い挿入スタイルが得られます。私が知る限り、デフォルトのブラウザー スタイルを適用する CSS の方法もありません。
IE 8 にはこの問題はありません。Chrome 2 と Firefox 3.5 はそうですし、他のブラウザも同様だと思います。私がオンラインで読んだことから、IE 7には同じ問題がありますが、その投稿には解決策がありませんでした.
次に例を示します。
<html>
<head>
<style>
.pictureInput {
background-image: url(http://storage.conduit.com/images/searchengines/search_icon.gif);
background-position: 0 1px;
background-repeat: no-repeat;
}
</style>
<body>
<input type="text" class="pictureInput" />
<br />
<br />
<input type="text">
</body>
</html>
Chrome 2 では次のようになります。
Firefox 3.5 の場合: http://www.screencast.com/users/jadeonly/folders/Snagit/media/d70dd690-9273-45fb-9893-14b38202ddcc
更新: JS 解決策:純粋な CSS-on-the-input ソリューションを見つけたいと思っていますが、今のところ使用する回避策を次に示します。これは私のアプリから直接貼り付けられているため、上記のようなスタンドアロンの良い例ではないことに注意してください。大規模な Web アプリから関連する部分を含めました。あなたはその考えを得ることができるはずです。HTML は「リンク」クラスの入力です。背景の縦位置が大きいのはスプライトだからです。IE6、IE7、IE8、FF2、FF3.5、Opera 9.6、Opera 10、Chrome 2、Safari 4 でテスト済み。一部のブラウザでは、背景の位置を数ピクセル微調整する必要があります。
JS:
$$('input.link').each(function(el) {
new Element('span',{'class':'linkIcon'}).setText(' ').injectBefore(el);
if (window.gecko) el.setStyle('padding', '2px 2px 2px 19px');
});
CSS:
input.link { padding-left: 19px; }
span.linkIcon { z-index: 2; width: 19px; height: 19px; position: absolute; background-image: url(img/fields.gif); background-position: 1px -179px; background-repeat: no-repeat; }
更新: CSS Close Enough Solution: kRON からの提案に基づいて、Vista で入力を FF と IE に一致させる CSS を次に示します。これは、純粋なデフォルトをあきらめて 1 つのスタイルを強制する場合に適しています。私は彼を少し変更し、「青みがかった」効果を追加しました。
CSS:
input[type=text], select, textarea {
border-top: 1px #acaeb4 solid;
border-left: 1px #dde1e7 solid;
border-right: 1px #dde1e7 solid;
border-bottom: 1px #e3e9ef solid;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
padding: 2px;
}
input[type=text]:hover, select:hover, textarea:hover, input[type=text]:focus, select:focus, textarea:focus {
border-top: 1px #5794bf solid;
border-left: 1px #c5daed solid;
border-right: 1px #b7d5ea solid;
border-bottom: 1px #c7e2f1 solid;
}
select { border: 1px; }