CSS:块元素的高度是如何计算的?
我使用包含一些注释的 div 元素。我基本上使用 jQuery 添加更多注释,如下所示:
$(myDiv).append(
'<li>'
+ '<img width="32px" height="32px" src="mySource"/></a>'
+ '<p>' + myComment + '</p>'
+ '</li>'
);
问题是,整个 div 的高度似乎没有正确更新!当我添加更多注释时,div 的高度会增加,但还不够,所以在添加一些注释后,它往往会溢出。
元素的高度实际上是如何计算的?
I use a div-Element that holds some comments. I basically use jQuery to add more Comments like so:
$(myDiv).append(
'<li>'
+ '<img width="32px" height="32px" src="mySource"/></a>'
+ '<p>' + myComment + '</p>'
+ '</li>'
);
The problem is, that it seems that the height of the whole div doesnt get updated properly! The height of the div increases while I add more comments, but not enough, so after some comments it tends to overflow.
How is the height of an element actually calculated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关于“元素的高度如何计算”的一般问题,可以在这里找到一些信息: http://www.w3.org/TR/CSS2/visudet.html#Computing_heights_and_margins
如您所见,这有点复杂。
一些摘录:
Regarding the general question of "How is the height of an element calculated", some info can be found here: http://www.w3.org/TR/CSS2/visudet.html#Computing_heights_and_margins
As you can see, it's a bit complicated.
Some excerpts:
是所有浏览器都会出现这种情况还是只有一种浏览器会出现这种情况?不同浏览器的高度计算略有不同。
当页面没有像我应该的那样呈现时,我首先查看的是 HTML 是否有效。在您的代码中,您有一个结束
标记,但没有开始标记。这种类型的不匹配足以让多个浏览器崩溃。
我发生过好几次导航按钮似乎位于非常不同的位置的情况,这都是因为我的内容中的某个地方我忘记关闭
或者我在某处添加了额外的关闭元素。
Is this happening in all browsers or just one? Height calculations differ subtly between browsers.
The first thing I look at when the page isn't rendering like I should is whether the HTML is valid. In your code, you have a closing
</a>
tag with no opening one. This type of mismatch is enough to throw several browsers off.It's happened to me several times where the navigation buttons seem to be in a very different location all because somewhere in my content I forgot to close a
<div>
or I added an extra closing element somewhere.您要附加到 DIV 还是 UL 中?它应该是一个 UL
试试这个 CSS
Are you appending to a DIV or a UL? It should be a UL
Try this CSS