搜索 | 用户支持

防范以用户支持为名的诈骗。我们绝对不会要求您拨打电话或发送短信,及提供任何个人信息。请使用“举报滥用”选项报告涉及违规的行为。

Learn More

Why is a floating box outside its container?

  • 5 个回答
  • 1 人有此问题
  • 7 次查看
  • 最后回复者为 cor-el

more options

This code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
><TITLE ></TITLE >
<BLOCKQUOTE STYLE="BACKGROUND: YELLOW; HEIGHT: 10EX" ><P STYLE="FLOAT: RIGHT" >Lorem<HR ><P STYLE="FLOAT: RIGHT" >ipsum</BLOCKQUOTE >
  • Why is BLOCKQUOTE wider than HR?
  • Why is ‘Lorem’ aligned with the widened BLOCKQUOTE?
  • Why is ‘ipsum‘ not aligned likewise?
This code: <pre><nowiki> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ><TITLE ></TITLE > <BLOCKQUOTE STYLE="BACKGROUND: YELLOW; HEIGHT: 10EX" ><P STYLE="FLOAT: RIGHT" >Lorem<HR ><P STYLE="FLOAT: RIGHT" >ipsum</BLOCKQUOTE ></nowiki></pre> * Why is BLOCKQUOTE wider than HR? * Why is ‘Lorem’ aligned with the widened BLOCKQUOTE? * Why is ‘ipsum‘ not aligned likewise?

由cor-el于修改

被采纳的解决方案

To achieve what you want, you have to add some clearing DIV elements or CSS rules.


data:text/html;charset=utf-8,
<!doctype html public "-//w3c//dtd html 4.01//en">
<head> 
 <title></title>
</head> 
<body>
<blockquote style="background: yellow;" >
 <p style="float: right;" >Lorem</p>
 <hr style="clear: both;" >
 <p style="float: right;" >ipsum</p>
 <div style="clear: both;"></div>
</blockquote>
</body>
定位到答案原位置 👍 0

所有回复 (5)

more options

Well, not quite the code I intended…

Maybe this will work:

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
>< TITLE >< /TITLE >
< BLOCKQUOTE STYLE="BACKGROUND: YELLOW; HEIGHT: 10EX" >< P STYLE="FLOAT: RIGHT" >Lorem< HR >< P STYLE="FLOAT: RIGHT" >ipsum< /BLOCKQUOTE >

由cor-el于修改

more options

Or this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ><TITLE ></TITLE > <BLOCKQUOTE STYLE="BACKGROUND: YELLOW; HEIGHT: 10EX" ><P STYLE="FLOAT: RIGHT" >Lorem<HR ><P STYLE="FLOAT: RIGHT" >ipsum</BLOCKQUOTE >

more options

选择的解决方案

To achieve what you want, you have to add some clearing DIV elements or CSS rules.


data:text/html;charset=utf-8,
<!doctype html public "-//w3c//dtd html 4.01//en">
<head> 
 <title></title>
</head> 
<body>
<blockquote style="background: yellow;" >
 <p style="float: right;" >Lorem</p>
 <hr style="clear: both;" >
 <p style="float: right;" >ipsum</p>
 <div style="clear: both;"></div>
</blockquote>
</body>

由cor-el于修改

more options

What is ‘!doctype’?

I think that:

  • the rule is being shortened by ‘Lorem’ because its margin overlaps it,
  • ‘ipsum’ is lower than ’Lorem’ because of the margin under the rule,
  • ‘ipsum’ is to the left of ‘Lorem’ because the margin under the rule is too narrow.

Correct me if I am wrong. I have probably missed two important points:

  • a float can shorten a rule,
  • margins around a float do not collapse.

Thank you,

Chris

more options

Floats within a container do not make that container adjust its width and height to fit the floats unless you use a clear rule.
So in your case the P elements float, but the HR element doesn't and is rendered first (your code looks the same in Google Chrome as in Firefox).

If you need a more precise explanation about how floats work then it is best to ask advice at the MozillaZine "Web Development/Standards Evangelism" forum.

The helpers at that forum are more knowledgeable about web development issues.
You need to register at the MozillaZine forum site in order to post at that forum.