html - 子元素的宽度改变了,但是父元素的宽度没有改变,为什么?

标签 html css web

reference

在这个页面中,当.gridster > ul的宽度改变时,.gridster的宽度不变。通常,父元素的宽度会随着子元素宽度的变化而变化。为什么在这种情况下保持不变。如何让父元素的宽度始终等于子元素的宽度。

最佳答案

因为.gridster是 block 级非替换元素。

10.3 Calculating widths and margins
10.3.3 Block-level, non-replaced elements in normal flow

The following constraints must hold among the used values of the other properties:

'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = width of  containing block

If 'width' is set to 'auto', any other 'auto' values become '0' and 'width' follows from the resulting equality.

在这种情况下,

  • 未设置宽度,所以它的值为初始auto
  • 水平边距设置为自动
  • 横向的border-*-style没有设置,所以它们的值是初始的none。因此水平 border-*-width 计算为 0
  • 没有设置水平填充,所以它们的值是初始的0

然后,

  • 水平边框和填充保持 0
  • 水平边距计算为 0,因为 widthauto
  • width 计算出 containing block 的宽度.

关于html - 子元素的宽度改变了,但是父元素的宽度没有改变,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29852568/

相关文章:

javascript - 绑定(bind)属性值时出错 - Angular 包含和指令

html - CSS:带有文本和渐变背景/边框的响应式拉长六边形

jquery - 使用 jquery 将事件类动态添加到菜单及其子菜单

css - 使用此自包含示例解析图像 + radio 的 `vertical-align: middle`

web - robots.txt 排除带有语言的路径

java - 将数据库连接从 GWT 应用程序传递到 Java 程序

html - 是否有一个库可以抽象 Web Audio API 和 Mozilla Audio Data API 来读取原始音频(MP3,ogg)

jquery - 使用 html5 将参数嵌入到 jQuery 的正确方法?

css - 添加超过 1 个绝对定位元素会破坏 css

html - 即使没有文本,如何保持段落高度?