【问题标题】:Understanding the flexible padding了解灵活的填充
【发布时间】:2014-10-10 19:14:50
【问题描述】:

在正则框模型下:

Ethan Marcotte 在响应式网页设计第 35 页中写道:

在元素上设置灵活填充时,您的上下文是 宽度 元素本身。

请看下面的例子:

.main-wrapper {
  width: 98%; /*960px - to give some space on the viewport*/ 
}

.box-one,
.box-two {
    width: 44.791666666667%; /* 430 / 960 */
    float: left;
    margin: 30px 0 20px 0;
    padding: 2.083333333333%; /* padding should be 20px*/
    text-align: center;
}

.box-one {
    margin-right: 2.083333333333%; 
    /*margin is relative to the container (here 960px), so:
      20/960
    */
    background-color: red;
}

.box-two {
    background-color: blue;
}
<div class="main-wrapper">

  <div class="box-one">
    <p>box one here</p>
  </div>

  <div class="box-two">
    <p>box two here</p>
  </div>

</div>

我的问题是:

根据那本书上写的内容,填充设置为: 不应该是正确的(但它是!)。

padding: 2.083333333333%;

相反: 我们应该考虑元素宽度本身,即430px。但如果我们将该值用作上下文,我们会得到 4.x %。

我没有得到什么?

【问题讨论】:

  • 如果你不明白我的问题,我可以努力澄清。只需发表评论。谢谢。
  • 主要的包装是元素,不是吗?嵌套 div 受父 div 约束。
  • 你能解释一下“上下文”是什么意思吗?
  • Gary Hayes,据我所知,根据我的阅读,元素本身不应该是main-wrapper,而是.box-one和@ 987654327@具体?
  • @web-tiki:根据我对作者文字的理解,上下文是您作为参考的价值。 IE。如果您希望将 20px 的 margin 转换为灵活的,在给定的布局上,您应该将该值除以容器宽度。在这种情况下,container widthcontext

标签: css


【解决方案1】:

我没有读过你提到的那本书,很难猜出它在说什么,因为我没有你引用的句子的上下文。

这就是说,如果你参考specs关于百分比填充计算:

百分比

百分比是相对于宽度计算的 生成框的包含块,[...]

所以百分比是根据父母的宽度计算的,因此2.083333333333%的值是正确的。

【讨论】:

  • 本书作者回复并表示将在本书的第二版中修复该问题。 :)
猜你喜欢
  • 1970-01-01
  • 2021-05-03
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多