javascript 使 td 的背景图像适合非静态图像的 100%

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15542379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:08:42  来源:igfitidea点击:

Make background image of td to fit to 100% for non static images

javascriptjquerycss

提问by Milind Anantwar

I have td tag:

我有 td 标签:

<td class="np-logo" style="width: 228px; height: 50px;">
                                            </td>

And CSS:

和 CSS:

.np-logo
{
background-repeat: no-repeat;
background-size: 100%;
}

And i am changing the image according to company logo.and all images have different-different size and width.

我正在根据公司徽标更改图像。所有图像都有不同的大小和宽度。

.np-logo{background-image:url('" + companylogo + "');

The problem is images are not getting fitted to td correctly.i want them to occupy whole td .but for every company the background image position is non uniform.I even tried using java script on image load to fit to container. didn't worked though.

问题是图像没有正确地适合 td。我希望它们占据整个 td。但是对于每家公司,背景图像位置都不统一。我什至尝试在图像加载时使用 java 脚本以适应容器。虽然没有用。

any help guys...?

任何帮助家伙...?

回答by Mr_Green

Try

尝试

background-size: 100% 100%;

where first value is for width and the second is for height.

其中第一个值用于宽度,第二个值用于高度。

You can know more about it by following this link

您可以通过点击此链接了解更多信息

回答by Samuel Liew

Have you tried cover?

你试过cover吗?

.np-logo {
    background-repeat: no-repeat;
    background-size: cover;
}

回答by Pith

Try this :

试试这个 :

<td class="np-logo" style="max-height: 100%; max-width: 100%">

or this

或这个

<td class="np-logo" style="max-height: 228px; max-width: 50px">