CSS 将宽度设置为父节点的宽度

发布于 2024-08-28 15:15:29 字数 79 浏览 1 评论 0原文

CSS 中有没有办法将任何元素的宽度设置为等于其父节点的宽度?我尝试过“继承” - 听起来很逻辑 - 但它不起作用。

提前致谢。

Is there a way in CSS to set the width of any element to equal the width of its parentNode? I tried 'inherit' - sounds logic - but it didn't work.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

吹泡泡o 2024-09-04 15:15:29
width: 100%;

这意味着父元素宽度的 100%。

请注意,如果元素的 display 属性设置为与“block”不同的内容,以及在其他一些情况下,它可能不起作用。

如果你在 2 个嵌套 div 上尝试,它会起作用。

width: 100%;

This means 100% of the parent element's width.

Note that it might not work if the element has the display property set to something different to "block", and in a few other cases.

If you try it on 2 nested divs, it'll work.

清欢 2024-09-04 15:15:29

根据它是什么元素,设置 CSS 属性 display: block 通常会使其自动填充所有可能的宽度,即拉伸到最大宽度,而不会溢出填充或边框。

Depending on what element it is, setting the CSS property display: block will usually make it automatically fill all possible width, aka stretch to its maximum without overflowing on padding or borders.

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