我试图弄清楚导致CSS width属性被视为按钮的整个宽度(包括内边距和边框宽度)的button元素是什么。CSS通常具有宽度限定所述内容宽度,填充内部的框的宽度,和浏览器(IE,火狐,铬在Windows 7,至少)通过,对于遵守DIV秒。
在下面的示例中,我将按钮和div设置为样式相似(并具有悬浮效果),但是对于div,我使用width单独指定内容区域的宽度。因此,即使两个按钮总体上占据相同的宽度,该按钮的CSS宽度也为190px,而CSS宽度为152px(或悬停时为150px)。
(无论是否显示,结果都是相同的:我添加到div的CSS中的inline-block试图匹配按钮的浏览器的display属性。)
结果:
有什么见解吗?
<html>
<style>
.xbutton {
width: 190px;
text-align: left;
background: -webkit-linear-gradient(#eaf2f5, #e0eaee); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(#eaf2f5, #e0eaee); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(#eaf2f5, #e0eaee); /* For Firefox 3.6 to 15 */
background: linear-gradient(#eaf2f5, #e0eaee); /* Standard syntax */
border: 1px solid #88949a;
border-radius: 5px;
padding: 8px 18px 8px 18px;
font-family: Arial;
font-size: 14px;
color: #000000;
}
.xbutton:hover {
background: -webkit-linear-gradient(#d2e0e8, #b8c3c9); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(#d2e0e8, #b8c3c9); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(#d2e0e8, #b8c3c9); /* For Firefox 3.6 to 15 */
background: linear-gradient(#d2e0e8, #b8c3c9); /* Standard syntax */
border: 2px solid #7b888e;
padding: 8px 16px 8px 20px;
}
.xbutton-icon {
margin: 0 7px 0 0;
vertical-align: middle;
}
div.xbutton { display: inline-block; width: 152px; }
div.xbutton:hover { width: 150px; }
</style>
<title>Button test</title>
<div><button class="xbutton"><img class="xbutton-icon" src="GetFileAttachment.png" width="25" height="18">Email attachment</button></div>
<div class="xbutton"><img class="xbutton-icon" src="GetFileAttachment.png" width="25" height="18">Email attachment</div>
</html>
您需要重置默认情况下为每个浏览器定义的按钮css样式。
/*fix for Firefox */
button::-moz-focus-inner{
border: 0;
padding: 0;
}
/*reset button CSS */
button{
margin: 0;
padding: 0;
border: none;
font: inherit;
box-sizing: border-box;
}
/*fix for Firefox */
button::-moz-focus-inner {
border: 0;
padding: 0;
}
/*reset button CSS */
button {
margin: 0;
padding: 0;
border: none;
font: inherit;
box-sizing: border-box;
}
.xbutton {
width: 190px;
text-align: left;
background: -webkit-linear-gradient(#eaf2f5, #e0eaee);
/* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(#eaf2f5, #e0eaee);
/* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(#eaf2f5, #e0eaee);
/* For Firefox 3.6 to 15 */
background: linear-gradient(#eaf2f5, #e0eaee);
/* Standard syntax */
border: 1px solid #88949a;
border-radius: 5px;
padding: 8px 18px 8px 18px;
font-family: Arial;
font-size: 14px;
color: #000000;
}
.xbutton:hover {
background: -webkit-linear-gradient(#d2e0e8, #b8c3c9);
/* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(#d2e0e8, #b8c3c9);
/* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(#d2e0e8, #b8c3c9);
/* For Firefox 3.6 to 15 */
background: linear-gradient(#d2e0e8, #b8c3c9);
/* Standard syntax */
border: 2px solid #7b888e;
padding: 8px 16px 8px 20px;
}
.xbutton-icon {
margin: 0 7px 0 0;
vertical-align: middle;
}
div.xbutton {
display: inline-block;
width: 152px;
}
div.xbutton:hover {
width: 150px;
}
<title>Button test</title>
<div>
<button class="xbutton">
<img class="xbutton-icon" src="http://lorempixel.com/25/18" width="25" height="18">Email attachment</button>
</div>
<div class="xbutton">
<img class="xbutton-icon" src="http://lorempixel.com/25/18" width="25" height="18">Email attachment</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句