gpt4 book ai didi

html - 交替 DIV 和 P 垂直对齐

转载 作者:太空宇宙 更新时间:2023-11-03 18:55:38 26 4
gpt4 key购买 nike

这个看似简单的问题困扰了我整整 10 年。 (好的,不是每天!)

它在 IE 中运行良好,但在 FF 和 Chrome 中运行不正常,这通常表示代码有问题...我想在两侧都有 DIV 框(它们实际上是各种大小的 IMG,所以不要继续使用表格),文本对齐。在各种窗口宽度下尝试此示例。在 FF 中,第 3 行显示在前面的 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>

补充问题:如果我把P换成SPAN,就更乱了。为什么 P 和 SPAN 有这么大的区别?

最佳答案

由于您的 div 左右浮动,<p>元素坐在中间不符合任何东西。它们既不与左侧也不与右侧 div 对齐。

实际上,代码中通常存在一系列基本缺陷,因此我将列出所有缺陷,我们可以更正它们以使行为正确:

  1. 如果您想让左 div 和 p 对齐,请考虑将它们包装在父 div 容器中。
  2. 考虑为 p 元素指定一个高度,以确保它们彼此对齐。
  3. 将父 div 的样式设置为两个元素应在的位置的 float 。
  4. 不要忘记用引号将您的 HTML 属性括起来(即 <div class="dl"> - 这比其他任何东西都更整洁)。

无论如何,我得到了这个解决方案,它比以前工作得更好,但它仍然不完美。我不确定为什么你想要在同一条线上的左右区域而不是交替左右之间的线。这种布局不太可能非常美观。在这里查看:http://jsfiddle.net/z26JM/

关于html - 交替 DIV 和 P 垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13552546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com