gpt4 book ai didi

按钮和 div 元素的 CSS 宽度不同

转载 作者:太空宇宙 更新时间:2023-11-04 11:09:54 24 4
gpt4 key购买 nike

我试图弄清楚是什么导致 CSS width 属性被视为按钮的整个宽度,包括填充和边框宽度。 CSS 通常有 width 定义内容宽度,填充内框的宽度,浏览器(IE、Firefox、Windows 7 上的 Chrome,至少)遵守 divs.

在我下面的示例中,我将 buttondiv 设置为看起来相似(并且具有悬停效果),但是对于 div>,我使用width单独指定内容区域的宽度。因此,即使这两个按钮整体占用相同的宽度,按钮 的 CSS 宽度为 190px,而 CSS 宽度为 152px(悬停时为 150px)。

(无论有没有我为 div 添加到 CSS 以尝试匹配浏览器的 display: inline-block 结果都是一样的按钮的显示属性。)

结果:

enter image description here

有什么见解吗?

<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>

关于按钮和 div 元素的 CSS 宽度不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33702749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com