简体   繁体   English

CSS-百分比填充与像素填充

[英]CSS - padding with percent vs. padding with pixels

I'm trying to build a UI that's all relatively based on the current DOM size. 我正在尝试构建一个相对全部基于当前DOM大小的UI。 I'm doing this by heavily using percents. 我正在通过大量使用百分比来做到这一点。

One thing I'm trying to do is render nodes inside of an absolute container with padding-top:%. 我想做的一件事是使用padding-top:%在绝对容器内渲染节点。

The size that the % calculates is totally out of whack for me. %所计算出的大小对我来说完全不可行。 I'm not sure what number the % is based off of but it certainly isn't the parent node. 我不确定%是基于哪个数字,但它肯定不是父节点。 However, padding with a fixed pixel in the same dom hierarchy works fine. 但是,在同一dom层次结构中使用固定像素进行填充可以很好地工作。

See the JS fiddle below: http://jsfiddle.net/AK3eT/ 请参见下面的JS小提琴: http : //jsfiddle.net/AK3eT/

    <div style="display:inline-block;width:600px;border:solid 1px black;position:absolute; top:10%; width:30%; height:40%;">
    <div style="border:solid 1px blue;height:20px;margin-top:20%"></div>
</div>
<div style="display:inline-block;width:600px;border:solid 1px black;position:absolute; left:160px;top:10%; width:30%; height:40%;">
    <div style="border:solid 1px blue;height:20px;margin-top:48px"></div>
</div>

The left node is using % padding, the right node is using fixed pixel padding. 左节点使用%填充,右节点使用固定像素填充。 They should be identical but the % padding is completely off. 它们应该相同,但%填充完全关闭。

Any ideas? 有任何想法吗? I feel like I'm missing something fundamental here. 我觉得这里缺少基本的东西。

margin-top: 20% specifies a top margin which is 20 percent of the width of the containing element. margin-top: 20%指定一个上边距,该边距是包含元素宽度的20%。 What I see from your code that the width of the containing element is 600px. 我从您的代码中看到,包含元素的宽度为600px。 so the top margin comes to 120px .Whereas in the case of right one it is only 48 px . 因此最高边距为120px 48 px 。而在右边的情况下,只有48 px That is why they are not identical. 这就是为什么它们不相同的原因。

Yes you do :) 是的你是 :)

vertical margin or padding takes parent's width as reference when % is used : http://www.w3.org/TR/CSS2/box.html#padding-properties & http://www.w3.org/TR/CSS2/box.html#propdef-margin-top 使用%时,垂直边距或边距以父级的宽度为参考: http : //www.w3.org/TR/CSS2/box.html#padding-propertieshttp://www.w3.org/TR/CSS2/ box.html#propdef余裕顶

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM