CSS:块元素的高度是如何计算的?

发布于 2024-10-05 14:02:52 字数 340 浏览 3 评论 0原文

我使用包含一些注释的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帝王念 2024-10-12 14:02:52

关于“元素的高度如何计算”的一般问题,可以在这里找到一些信息: http://www.w3.org/TR/CSS2/visudet.html#Computing_heights_and_margins

如您所见,这有点复杂。

一些摘录:

当“溢出”计算为“可见”时,正常流中的块级非替换元素

当“溢出”未计算为“可见”但已传播到视口时,本节也适用于正常流中的块级非替换元素。

[。 。 .]

如果“height”为“auto”,则高度取决于该元素是否具有任何块级子元素以及它是否具有填充或边框:

如果它只有内联级子级,则高度是最顶部行框的顶部和最底部行框的底部之间的距离。

如果它有块级子框,则高度是没有折叠边距的最顶部块级子框的顶部边框边缘与最底部块级子框的底部边框边缘之间的距离没有通过它折叠边距。

[。 。 。 ]

仅考虑正常流程中的子项(即忽略浮动框和绝对定位框,并考虑相对定位框而不考虑其偏移)。

[。 。 。 ]

当“溢出”未计算为“可见”时,正常流中的块级非替换元素(除非“溢出”属性的值已传播到视口)。

如果“height”为“auto”,高度取决于元素的后代< /a>.

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:

Block-level non-replaced elements in normal flow when 'overflow' computes to 'visible'

This section also applies to block-level non-replaced elements in normal flow when 'overflow' does not compute to 'visible' but has been propagated to the viewport.

[. . .]

If 'height' is 'auto', the height depends on whether the element has any block-level children and whether it has padding or borders:

If it only has inline-level children, the height is the distance between the top of the topmost line box and the bottom of the bottommost line box.

If it has block-level children, the height is the distance between the top border-edge of the topmost block-level child box that does not have margins collapsed through it and the bottom border-edge of the bottommost block-level child box that does not have margins collapsed through it.

[. . . ]

Only children in the normal flow are taken into account (i.e., floating boxes and absolutely positioned boxes are ignored, and relatively positioned boxes are considered without their offset).

[. . . ]

Block-level, non-replaced elements in normal flow when 'overflow' does not compute to 'visible' (except if the 'overflow' property's value has been propagated to the viewport).

If 'height' is 'auto', the height depends on the element's descendants.

千秋岁 2024-10-12 14:02:52

是所有浏览器都会出现这种情况还是只有一种浏览器会出现这种情况?不同浏览器的高度计算略有不同。

当页面没有像我应该的那样呈现时,我首先查看的是 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.

情归归情 2024-10-12 14:02:52

您要附加到 DIV 还是 UL 中?它应该是一个 UL

试试这个 CSS

li{clear:both}

Are you appending to a DIV or a UL? It should be a UL

Try this CSS

li{clear:both}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文