V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
rabbbit
V2EX  ›  CSS

CSS 属性 width:100%相对于父元素的哪部分计算?

  •  
  •   rabbbit · Nov 11, 2017 · 6150 views
    This topic created in 3101 days ago, the information mentioned may be changed or developed.
    普通状态下,似乎相对于父元素宽度计算



    子元素加上 position:absolute 后,变成相对于父元素的 width + padding 了?




    [示例]( http://htmlpreview.github.io/?https://github.com/Aaron-Bird/baiduIFE/blob/master/note/width100%25.html)
    Supplement 1  ·  Nov 12, 2017
    https://css-tricks.com/almanac/properties/w/width/#article-header-id-0
    For absolutely positioned elements whose containing block is based on a block container element, the percentage is calculated with respect to the width of the padding box of that element.
    oh
        1
    oh  
       Nov 12, 2017   ❤️ 2
    这是两个问题了。
    一、默认状态下 width 100% 是相对于父元素的可用宽度,不含 padding。
    二、加了 absolute 之后元素变成了 绝对定位,会一直向上寻找第一个非 static 的父元素(例如其它的 absolute 或 relative ),如果上级元素中都没有,那就是相对于 body 了,所以这时候搭配 width 100% 的话就是占据整个视图区域的宽度了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2901 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 11:25 · PVG 19:25 · LAX 04:25 · JFK 07:25
    ♥ Do have faith in what you're doing.