【发布时间】:2016-03-17 04:23:11
【问题描述】:
有一个内边距为 30 像素、宽度和高度为 100 像素的 div。框大小设置为边框框。尝试将宽度和高度设置为零时,它不起作用。元素保持在 60px X 60px 尺寸。 (即使溢出设置为隐藏)。
知道这里发生了什么吗?有什么方法可以使宽度和高度为零,包括填充,以便它完全折叠?
.test {
padding: 30px;
width: 400px;
height: 400px;
box-sizing: border-box;
background-color: orange;
overflow: hidden;
}
/* Setting width and height to zero. */
.test {
width: 0;
height: 0;
}
<div class="test"></div>
<p>Have a div with padding of 30px and width and height of 100px. Box sizing is set to border box. When trying to set the width and height to zero, it does not work. Element stays at 60px X 60px dimensions. (Even with the overflow set to hidden).</p>
<p>Any idea what's going on here? Is there any way to make the width and height to zero including the paddings, so that it collapses completely?</p>
【问题讨论】:
-
移除填充 - 填充在宽度中
-
根据 CSS 框模型,填充存在于边框内。因此这是正常行为。
-
如果您不想应用
padding,为什么还要添加padding? -
如果你想隐藏为什么不能使用
display:none?你能告诉我你需要这个的场景吗? -
有没有办法用一个电话来完成?比如 $('.test').css('width': '0'); ?