【发布时间】:2013-12-31 00:31:59
【问题描述】:
好的,这很可能是基本的 Web 开发 101,但是有人可以向我解释为什么这两个按钮的大小不同吗?唯一的区别是第一个按钮使用em 单位,第二个按钮使用px。
jsFiddle(演示)
HTML
<button type="button" class="em">em</button>
<button type="button" class="px">px</button>
CSS
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-size: 100%;
}
.em {
position: relative;
width: 9.375em; /* 150px */
height: 3.125em; /* 50px */
background: #18a397;
text-align: center;
margin: 1em;
box-sizing: content-box;
}
.px {
position: relative;
width: 150px; /* 9.375em */
height: 50px; /* 3.125em */
background: #18a397;
text-align: center;
margin: 16px;
}
如果我将第一个按钮更改为 div,例如:<div class="em">em</div> 元素的大小将调整为与第二个按钮相同的大小。
因此,出于某种原因,px 和 em 与 <button> 一起使用是有区别的 - 但我不知道是什么以及为什么......?
测试于: Safari 7、Chrome 31.0.1650.63 和 Firefox 26.0。
【问题讨论】: