如何用图像填充div的内容

时间:2020-08-26 20:54:03

标签: css

我正在创建一个社交网站,人们可以上传图像。当他们上传这些图片时,它位于div中。如何填充整个div,以便在侧面看不到空白?

这是现在的样子:

First Example

Second e.g

我的代码:

.postedImageDiv {
max-width: 500px;
overflow: hidden;
max-height: 400px;
border-radius: 10px;
border: 1px solid gray;
background: #fff;
margin-bottom: 1em;
}

.profilePostedImage img {

max-height: 450px;
max-width: 450px;
display: block;
margin: 5px auto;
}

.postedImage img {

align-self: center;
object-fit: contain;
max-height: 600px;
max-width: 600px;
display: block;
margin: 5px auto;
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

    max-width: 100%;
    max-height: 100%;
    display: block;

.outside {
  max-width: 500px;
  overflow: hidden;
  max-height: 400px;
  border-radius: 10px;
  border: 1px solid gray;
  background: #fff;
  margin-bottom: 1em;
}
.img-inside{
  width: 100%;
  height: 100%;
  display: block;
}
<!-- vertical image example -->
<div class="outside">
  <img class="img-inside" src="https://i.guim.co.uk/img/media/86c3481516dce247943ac2978b4f48d16a3ac265/0_170_5120_3074/master/5120.jpg?width=1200&height=1200&quality=85&auto=format&fit=crop&s=637dc5731d52754675ef36344a6af3c8">
</div>
<!-- landscape image example -->
<div class="outside">
  <img class="img-inside" src="https://www.straight.com/files/v3/styles/gs_standard/public/images/15/03/shutterstock_grizzlybear_0.jpg?itok=vz6hpl50">
</div>