[英]Unable to change width and height on image
I have added two images to my web page which have loaded fine, however when I try to change the height, width, border-radius, etc with CSS it ignores this and stays at the original shape and size.我已经在我的 web 页面中添加了两个图像,它们已经加载得很好,但是当我尝试使用 CSS 更改高度、宽度、边框半径等时,它会忽略这一点并保持原始形状和大小。 Any help on what is causing this?关于导致这种情况的任何帮助?
My HTML:我的 HTML:
<div class="tile-image"></div>
<div class="tile-image"></div>
My CSS:我的 CSS:
.tile-image {
width: 432px;
height: 192px;
object-fit: cover;
border-radius: 4px;
}
My JavaScript:我的 JavaScript:
data = {
"tiles" : [
{
"img" : "example.jpg"
},
{
"img" : "example.jpg"
}
]
};
data.tiles.forEach((item) => {
let img = new Image();
img.src = item.img;
tile = document.getElementsByClassName("tile-image")[0];
tile.appendChild(img);
});
try to set your image width to the size of the container.尝试将图像宽度设置为容器的大小。
Add this to your css:将此添加到您的 css:
.tile-image img{
width: 100%;
}
Or maybe, what you are trying to achive is this:或者,您想要实现的是:
.tile-image img {
width: 432px;
height: 192px;
object-fit: cover;
border-radius: 4px;
}
You are setting a height and width on the parent div but not the images themselves.您在父 div 上设置高度和宽度,而不是图像本身。 Add the following CSS:添加以下 CSS:
img {
max-width: 100%;
max-height: 100%;
}
Should work for you应该为你工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.