如何padding-top作为一个百分比与父的宽度相关? [英] How is padding-top as a percentage related to the parent's width?

查看:107
本文介绍了如何padding-top作为一个百分比与父的宽度相关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个示例:

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

注意所有红色div是否相同 height 并有 padding-top:100%; ,但他们A& B具有不同的填充大小...并且看起来父级的宽度会更改此值(请注意,C会更改父级的 height

Notice how all of the red divs are the same height and have padding-top:100%;, yet they A & B have different padding sizes... and it appears that the width of the parent changes this value (note that C changes the height of the parent, yet that doesn't alter the padding).

为什么padding以这种方式与宽度相关?

Why is padding related to width in this way?

>编辑:出于历史原因,如果jsfiddle不见了,我在我的例子中使用的代码如下...

for historical reasons, and in case jsfiddle goes away, the code I used in my example is as follows...

HTML:

<div class='outer'>
    <div class='middle'>
        A
      <div class='inner'>
      </div>
    </div>
</div>

<div class='outer' style='width:100px'>
    <div class='middle'>
        B
      <div class='inner'>
      </div>
    </div>
</div>

<div class='outer' style='height:400px'>
    <div class='middle'>
        C
      <div class='inner'>
      </div>
    </div>
</div>

CSS:

.outer {
    background-color: green;
    height: 300px;
    width: 70px;
    float: left;
    margin-right: 10px;
}

.middle {
    background-color: red;
    height: 100px;
    width: 50px;
    padding-top: 100%;
}

.inner {
    background-color: blue;
    height: 3px;
    width: 100%;
}


推荐答案

http://stackoverflow.com/questions/10629294/css-fluid-layout-margin-top-based-on-percentage-grows-when-container-width-incr\"> CSS流体布局:margin-top基于百分比增长当容器宽度增加时:

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


在CSS中,所有四个边距和填充:百分比是相对于宽度...即使这可能看起来荒谬。这只是CSS规范的方式,你没有什么可以做的。

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.

直接从马的嘴巴

'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




百分比:指包含块的宽度

这篇关于如何padding-top作为一个百分比与父的宽度相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆