简体   繁体   English

Firefox在带边框的按钮中的图像周围增加了额外的空间

[英]Firefox adds extra space around image in button with border

I have an <img> in a <button> . 我在<button>有一个<img> <button> The button has a border. 该按钮带有边框。 The image is scaled to 100% height and the width of the button is determined by the calculated image width. 图像缩放到100%的高度,按钮的宽度由计算出的图像宽度确定。 So far so good, as it works as long as there's no border on the button. 到目前为止,只要按钮上没有边框,它就可以正常工作。 If I add a border, Firefox is going to downscale the image but not to adjust to the new image width. 如果添加边框,Firefox将缩小图像,但不会调整为新的图像宽度。 It will simply add extra space. 它只会增加额外的空间。 Chrome and Safari are doing this as expected – or at least as I expect it. Chrome和Safari可以按预期进行,或至少按我的预期进行。

How can I get Firefox to behave the same? 如何使Firefox具有相同的行为?

Figure 数字

You can see the extra green area I'm trying to get rid of. 您可以看到我想要摆脱的多余绿色区域。

在此处输入图片说明

HTML 的HTML

<div class="wrapper">
    <button>
        <img src="//placehold.it/160x90">
    </button>
</div>

CSS 的CSS

.wrapper {
    height: 100px;
}

button {
    height: 100%;
    padding: 0;
    background: green;
    border: 0;
    border: 3px solid tomato;
    cursor: pointer;
}

button::-moz-focus-inner {
    padding: 0;
    border: 0;
}

button img {
    height: 100%;
    width: auto;
}

Fiddle 小提琴

https://jsfiddle.net/4tjmmq58/ https://jsfiddle.net/4tjmmq58/

That is how Firefox interprets the div tag to add the width div but you can just change the css to be like the following: 这就是Firefox解释div标签以添加width div的方式,但是您可以将css更改为如下所示:

.wrapper {
    height: 100px;
    width: 170px;
}

button {
    height: 100%;
    padding: 0;
    background: green;
    border: 0;
    border: 3px solid tomato;
    cursor: pointer;
}

button::-moz-focus-inner {
    padding: 0;
    border: 0;
}

button img {
    height: 100%;
    width: auto;
}

and this should fix the over-sized div around the button. 这应该修复按钮周围的超大div。 But I would recommend using different browsers and <tags> when doing what your doing. 但是我建议您在执行操作时使用不同的浏览器和<tags>

reference: http://www.w3schools.com/cssref/pr_dim_width.asp 参考: http : //www.w3schools.com/cssref/pr_dim_width.asp

You just need to add box-sizing: content-box; 您只需要添加box-sizing: content-box; to the button and your problem will be fixed but another will emerge. 按钮,您的问题将得到解决,但还会出现另一个问题。 Now your border won't be calculated in the elements width / height so you may have to subtract it by manually. 现在,您的边框不会在元素的width / height进行计算,因此您可能需要手动将其减去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM