javascript - 自动调整 Div 背景图像

标签 javascript html size

我想将 Div 背景图像设置为适合 div 大小。我从谷歌帮助中尝试了很多。但无法得到解决方案。

我的 div 属性如下

userPic.style.width = "65px";
userPic.style.height = "85px";
userPic.style.position = "absolute";
userPic.style.left = "10px";
userPic.style.top = "10px";
userPic.style.borderRadius = '4em';

现在我正在设置背景图像,如下所示

userPic.style.background = "url('"+employee.profilepic+"') no-repeat";
userPic.className = "profilePic";

我设置了如下的个人资料图片类

.profilePic
{
    background-size: 100%;
    background-repeat: no-repeat;
    overflow: hidden;
    margin:auto;
}

但是background-size:100%在chrome中不能正常工作,而且也不能完美工作。我的 div 大小为 65x85,而我的图像大小大于此。那么如何让大图片或小图片适合这个div呢?

最佳答案

我会尝试background-size: cover;,看看这是否能达到您想要的效果。

关于javascript - 自动调整 Div 背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31384836/

相关文章:

css - 使标题占据与下面的框相同的宽度

html - 如何修复 "Refused to execute script because its MIME type (' text/html') 不可执行,并且启用了严格的 MIME 类型检查。”对于 React?

objective-c - Finder 和 Carbon 文件管理器中的文件夹大小存在差异

c++ - 不同列宽的QTableView

html - 使用 Bootstrap 将响应图像叠加在一起

c++在 vector 末尾插入元素

javascript - 火力地堡存储 : String does not match format 'base64' : Invalid character found

javascript - Nuxt + Vuex - 如何将 Vuex 模块分解为单独的文件?

javascript - 如何使用 jQuery 设置 externalHTML

javascript - fancybox 没有正确加载内容