这个看似简单的问题困扰了我整整 10 年。(好吧,不是每天!)
它在 IE 中可以正常工作,但在 FF 和 Chrome 中却不行,这通常表明代码有问题......我想在两边都有 DIV 框(它们实际上是各种大小的 IMG,所以不要t 继续使用表格),文本对齐。在不同的窗口宽度下尝试这个例子。在 FF 中,第 3 行显示 ABOVE ABOVE 前面的 DIV。为什么 ?
基本上,这个想法是 DIV 应该沿着窗口的每一侧堆叠,文本在中间。如果文本太多,下一个 div 就会被下推。在 IE 中运行良好。
<HTML>
<HEAD>
<TITLE>Align test</TITLE>
<STYLE TYPE="text/css">
.DL { float:left; clear:left; width:10em; height:10em; background:red; margin:2; display:inline; }
.DR { float:right; clear:right; width:10em; height:10em; background:green; margin:2; display:inline; }
.PL { text-align:left; background:#F0E0E0; }
.PR { text-align:right; background:#E0F0E0; }
</STYLE>
</HEAD>
<BODY>
<DIV CLASS=DL></DIV><P CLASS=PL>This is the 1st line.</P>
<DIV CLASS=DR></DIV><P CLASS=PR>This is the 2nd line.</P>
<DIV CLASS=DL></DIV><P CLASS=PL>This is the 3rd line which should align with the 2nd red square (*)</P>
<DIV CLASS=DR></DIV><P CLASS=PR>This is the 4th line which should align with the 2nd green square.</P>
<P>(*) No, I don't want a clear:right in here. And adding a float:left works fine if the text is short, but moves the right image down if it reaches it, and it moves the whole line down below the left image if it reaches the right border. Which I don't want.</P>
</BODY>
</HTML>
附加问题:如果我用 SPAN 替换 P,那就更糟了。为什么 P 和 SPAN 之间有这么大的区别?