2

I have a container image-wrapper and an image inside it.

The container has a fixed height and width. Inside it, the image resolution will be dynamic.

All I am trying to do is to center the image to the container, horizontally and vertically and apply some box-shadow styling to it.

But it leaves a blank space around the image, and it looks weird. Below is the code. It happens same with a vertical image.

.image-wrapper {
  height: 400px;
  width: 400px;
  background-color: rgb(0,200,100);
  padding: 40px;
}

.image-wrapper img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  box-shadow: 10px 10px 10px red;
}
<div class="image-wrapper">
  <img src="https://via.placeholder.com/500x200">
</div>

I want to have a shadow around the image only, without the spaces. I think I am missing a very simple thing here.

I have tried to add position: relative to the container, but it didn't work.

I tried to search about it by searching something like "css object fit image shadow", "css object fit on image adds spaces" and didn't found anything similar to it.

Debsmita Paul
  • 1,442
  • 9
  • 22

3 Answers3

1

Remove the height: 100%; and object-fit: contain; from .image-wrapper img

Also, remove height: 400px; from the .image-wrapper.

To centre image use CSS Grid: (btw, it is not required to center in this condition)

display: grid;
place-items: center;

.image-wrapper {
  width: 500px;
  background-color: rgb(0, 200, 100);
  padding: 40px;
  display: grid;
  place-items: center;
}

.image-wrapper img {
  width: 100%;
  box-shadow: 10px 10px 10px red;
}
<div class="image-wrapper">
  <img src="https://via.placeholder.com/500x1000">
</div>

Some useful resources:

Manas Khandelwal
  • 3,790
  • 2
  • 11
  • 24
1

My solution without cropped and with Flexbox :

.image-wrapper {
  height: 100%;
  width: 500px;
  background-color: rgb(0,200,100);
  padding: 40px;
  display : flex;
  justify-content: center;
  align-items: center;
}

.image-wrapper img {
  width: 100%;
  object-fit: contain;
  box-shadow: 10px 10px 10px red;
}
<div class="image-wrapper">
  <img src="https://via.placeholder.com/500x1000">
</div>
1

Consider this solution:

As per the requirement,

  1. the container has a fixed height and width. Hence, added the following to class .image-wrapper
      height: 400px;
      width: 400px;
  1. image is to be centred in the container, horizontally and vertically. Hence, added the following to class .image-wrapper
      display: flex;
      align-items: center;
      justify-content: center;
  1. To prevent blank space around the image when box-shadow is added, instead of height and width, added max-height and max-width to the img. This will prevent height and width of the image from going more than 400px parent, and the aspect ratio of the image will also be maintained.
      max-width: 100%;
      max-height: 100%;

Overall, this is the solution:

.image-wrapper {
  height: 400px;
  width: 400px;
  background-color: rgb(0, 200, 100);
  padding: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.image-wrapper img {
  max-width: 100%;
  max-height: 100%;
  box-shadow: 10px 10px 10px red;
}
<div class="image-wrapper">
  <img src="https://via.placeholder.com/500x200">
</div>

<div class="image-wrapper">
  <img src="https://via.placeholder.com/500x1000">
</div>
Omkar Rajam
  • 721
  • 7
  • 13