0

I'm trying to cover a flex-box container with an image but the CSS property object-fit doesn't seem to be working and is not fitting my image which is 1536 x 1536 which is much bigger than what the dimensions are for the container. This is what it looks like:

enter image description here

And this is what the image looks like:

enter image description here

How can I fit this image into that flex box container?

.flex-container {
  display: flex;
  flex-wrap: wrap;
  border-radius: 16px;
  background-color: linear-gradient(90deg, rgba(244, 247, 252, 0.5) 0%, rgba(244, 247, 252, 0.5) 100%);
  justify-content: center;
}

.flex-container>div {
  background-color: white;
  width: 20vh;
  margin: 15px;
  text-align: center;
  line-height: 20vh;
  font-size: 30px;
  object-fit: contain;
}

.sdg1 {
  background: url("https://via.placeholder.com/1536.jpg");
}
<div className="flex-container">
  <div className="sdg1"></div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
  <div>SDG</div>
</div>
tacoshy
  • 10,642
  • 5
  • 17
  • 34
Soccerball123
  • 741
  • 5
  • 17

1 Answers1

2

object-fit should be used on img elements to determine how they will be displayed inside of their own "container".

For this to work, you must replace your <div> element with an <img> element. Here is an example of how object-fit works:

.contained {
  border: 1px solid black;
  width: 100px;
  height: 100px;
  object-fit: contain;
}
<img src="https://cdn.pixabay.com/photo/2016/02/19/15/46/labrador-retriever-1210559__480.jpg" class="contained">

Codepen

Geeky Quentin
  • 2,469
  • 2
  • 7
  • 28
ace
  • 66
  • 4