How is padding-top as a percentage related to the parent's width?

后端 未结 5 2094
[愿得一人]
[愿得一人] 2020-11-29 05:35

Here is an example:

http://jsfiddle.net/QZAd8/1/

Notice how all of the red divs are the same height and have padding-top:100%;, yet

相关标签:
5条回答
  • 2020-11-29 05:54

    The height of an element relies on its content, which include the padding and margin of its child elements, so that would a loop, if the child elements rely on its parent height.

    0 讨论(0)
  • 2020-11-29 05:57

    If you came here looking for an alternative like I did, you can set padding-top as a percentage of vertical height using a CSS calculation:

    padding-top: calc(.25 * 100vh);
    
    0 讨论(0)
  • 2020-11-29 06:01

    Just because it's the way it is suppose to be :) http://www.w3.org/TR/CSS2/box.html#propdef-padding-top

    0 讨论(0)
  • 2020-11-29 06:04

    From CSS fluid layout: margin-top based on percentage grows when container width increases :

    In CSS, all four margin: and padding: percentages are relative to the width ...even though that may seem nonsensical. That's just the way the CSS spec is, there's nothing you can do about it.

    Straight from the horse's mouth:

    'padding-top', 'padding-right', 'padding-bottom', 'padding-left'
    Value:      <padding-width> | inherit
    Initial:    0
    Applies to:     all elements except table-row-group, table-header-group, table-footer-group, table-row, table-column-group and table-column
    Inherited:      no
    Percentages:    refer to width of containing block
    Media:      visual
    Computed value:     the percentage as specified or the absolute length
    

    Percentages: refer to width of containing block

    0 讨论(0)
  • 2020-11-29 06:04

    It's true that padding percentage is relative to the width, but specifically, padding: 100% can also be read as padding: *width-of-container*px.

    The A and C blocks had a width of 70px. Applying padding: 100% was the same as padding: 70px

    0 讨论(0)
提交回复
热议问题