![CSS と IE7 でフロートをクリアしますか?](https://rvso.com/image/1428754/CSS%20%E3%81%A8%20IE7%20%E3%81%A7%E3%83%95%E3%83%AD%E3%83%BC%E3%83%88%E3%82%92%E3%82%AF%E3%83%AA%E3%82%A2%E3%81%97%E3%81%BE%E3%81%99%E3%81%8B%3F%20.png)
私は、古いバージョンの Internet Explorer で float と clear が適切に機能するように 1 日中頭を悩ませてきました。.clear:after トリックの使用法などに関するチュートリアルを数多く読みましたが、実際に機能するものが見つかりません。
次のような HTML があります:
<div id="section">
<h2>Section Title</h2>
<label for="name">Name</label>
<input type="text" id="name" />
<label for="dob">Date of Birth</label>
<input type="text" id="dob" />
<label for="email">Email</label>
<input type="text" id="email" />
</div>
CSS は次のようになります:
#section {border:solid 2px #b7ddf2; background:#ebf4fb; margin-top: 20px;}
#label{clear: left; float: left; width: 300px; margin-left: 20px; margin-bottom: 10px;}
#input{float: left; margin-bottom: 10px;}
最新のブラウザ(例:Opera 25)では、次のように表示されます。
Name [Name field]
Date of birth [Date of birth field]
Email [Email]
Internet Explorer の古いバージョン (6 または 7) では、次のように表示されます。
Name [Name field] [Date of birth field] [Email field]
Date of birth
E-mail
HTML を調整したくないのですが、CSS の修正を手伝ってくれる人はいますか?
答え1
あなたがしようとしていることは悪い考えです。第一に、新しい Web アプリケーションで IE6 をサポートするためにこれほどの努力を払う人はいないはずですし、第二に、HTML 構造は柔軟性がなく、非常に特殊なシナリオでしか機能しません。
しかし、私は、IE6 の典型的なフロート バグが、余分な HTML を使わずに CSS だけで克服できるかどうか知りたかったのですが、驚いたことに、それが可能なようです。その秘訣は、IE の CSS 式を使用して、テキスト ボックスの後のページに <br> 要素を挿入することです。
次のコードは、IE6 および IE7 をエミュレートするときに IETester で機能しました。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
#section {border:solid 2px #b7ddf2; background:#ebf4fb; margin-top: 20px; display: block; zoom: 1}
label{clear: left; float: left; width: 300px; margin-left: 20px; margin-bottom: 10px;}
input{
float: left;
margin-bottom: 10px;
*zoom: expression(this.runtimeStyle.zoom="1",
this.parentNode.insertBefore(document.createElement("br"), this.nextSibling).style.cssText="clear:both;font:0/0 serif");
}
</style>
</head>
<body>
<div id="section">
<h2>Section Title</h2>
<label for="name">Name</label>
<input type="text" id="name" />
<label for="dob">Date of Birth</label>
<input type="text" id="dob" />
<label for="email">Email</label>
<input type="text" id="email" />
</div>
</body>
</html>
情報に基づくこのページ