When implementing CSS object-fit: contain on an image, it may result in empty spaces surrounding the image

Within my image-wrapper container, I have an image with a variable resolution.

The container itself has fixed dimensions for height and width. My goal is to center the image within the container both horizontally and vertically while adding a box-shadow effect.

However, upon applying the styling, I noticed that there is unwanted blank space surrounding the image which appears odd. The issue persists regardless of whether the image is horizontal or vertical in orientation.

.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 aim to achieve a shadow effect around the image exclusively, without any additional spacing. It seems like there might be a simple solution that I am overlooking.

Attempts such as adding position: relative to the container did not yield the desired result.

In my search for solutions, queries like "css object fit image shadow" and "css object fit on image adds spaces" did not lead me to resources addressing this specific issue.

Answer №1

Here is my solution featuring Flexbox and no cropping:

.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>

Answer №2

Modify the CSS for .image-wrapper img by removing height: 100%; and object-fit: contain;.

Similarly, eliminate height: 400px; from the .image-wrapper class.

To position the image in the center, utilize CSS Grid:

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>

Here are some helpful resources on this topic:

Answer №3

Here's a proposed solution:

Based on the specified requirements,

  1. The container is expected to have fixed height and width, so the following styles were applied to the class .image-wrapper:
      height: 400px;
      width: 400px;
  1. To center the image both horizontally and vertically within the container, the following styles were added to the class .image-wrapper:
      display: flex;
      align-items: center;
      justify-content: center;
  1. In order to avoid blank spaces around the image when adding box-shadow, instead of using height and width properties, max-height and max-width were assigned to the img. This ensures that the image does not exceed the dimensions of the parent container (400px), while also maintaining its aspect ratio.
      max-width: 100%;
      max-height: 100%;

This sums up the suggested 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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Can anyone provide guidance on how to use Twig to read from a JSON file

I currently have a basic PHP file set up as follows: <?php // Loading our autoloader require_once __DIR__.'/vendor/autoload.php'; // Setting the location of our Twig templates $loader = new Twig_Loader_Filesystem(__DIR__.'/views& ...

Activate single elements one at a time

If you want to understand the question better, take a look at my code on jsfiddle. Each Div contains only one link. When you click on the link, it sets the Div to active and shows a hidden Div within it. Clicking the link again toggles the active style an ...

Is Amazon altering the names of their CSS selectors and HTML elements on the fly?

For my Amazon.es web scraper built with Selenium, I am using a CSS selector to determine the total number of pages it will iterate through. However, the selector name seems to change dynamically and I must update it daily. As someone not well-versed in H ...

Guide to modifying the text color of a Primefaces/jqPlot line chart:

I've implemented a basic JSF line chart with PrimeFaces (using jqPlot library) in my project: <p:lineChart id="linear" value="#{team.chart}" title="Lap Times" xaxisLabel="Lap" yaxisLabel="Time ...

When the Navbar div is set to Position:fixed, it generates empty space on the page

I'm facing the issue of unwanted white space in the center of the page below the Navigation Bar. Despite numerous attempts to remove it, I haven't been successful. Below is the HTML code snippet: html: <!DOCTYPE html> <html> <h ...

Changing the size of an image in an HTML5 canvas

I have been attempting to generate a thumbnail image on the client side using Javascript and a canvas element. However, when I reduce the size of the image, it appears distorted. It seems as though the resizing is being done with 'Nearest Neighbor&apo ...

CSS- Strategically placing and centering images above specific keywords in (any) HTML content without disrupting the flow of text

My main objective involves dynamically inserting images above text on any given page using a content script. The challenge lies in maintaining the proper alignment of the text after adding the images. To achieve this, I surround the words where the image i ...

Utilize jQuery to export HTML or JSON data to an Excel spreadsheet

I am looking for a way to export json or html data to an excel sheet using only html and jquery, without involving any server code. I have found some fiddles that allow me to download the excel sheet successfully, but unfortunately, none of them work in In ...

Using Python to access files

I have developed a system that allows me to load image files from HTML. However, after selecting a file and loading it from the front end, I am in need of a Python API to read the selected image file. How can I establish a connection between Python and HTM ...

I'm having trouble getting my HTML to scroll properly

Apologies for asking a common question, but I'm facing a specific issue with scrolling that I can't seem to figure out. Below is the code snippet: * { font-family: Arial, Helvetica, sans-serif; text-align: center; font-size: 40px; } < ...

JQuery mishandles its left positioning

Here's the code snippet I've been working with: $(this).closest("p").after('<strong>' + arMess[iCurIndex] + '</strong>').next().animate({ top: $(this).offset().top - 57, left: -$(this).widt ...

Stop users from being able to select or highlight text within a content editable div

Is there a method to develop a content-editable div where users are unable to select or highlight content, but can still input information? I'm interested in creating an interface that forces users to delete and enter data character by character, with ...

Tips on completing landing page animations and displaying the main page of a website by utilizing React JavaScript

https://i.stack.imgur.com/M4VgY.pngIs there a way to stop the landing page animation after 2-3 seconds and show the main page instead? Can I achieve this by using setTimeOut function in JavaScript? [ export default Loader; ...

developing a multimedia player using HTML5

I'm attempting to create a dynamic video "collage" that showcases videos with different aspect ratios in a flexible grid layout. Each row should have videos of the same height, while filling up the horizontal space within a container. With known widt ...

Picture not displaying properly in alternative browsers

While working on a simple website using html and css, I encountered an issue where my logo was not displaying for users on different browsers. Here is the image description Below is the HTML code: <section id="header"> <div class=& ...

Create a dynamic feature on a JSP page that allows users to click a button and instantly see changes

How can I create a button on an HTML or JSP page that, when clicked, will dynamically add content to the same page? For example, imagine I have a JSP page called Prescription.jsp with input forms like: ID name select commodity weight tolerance ...

Chat lines in Chrome displaying double entries - troubleshooting needed

I developed a chat plugin for my website with a simple HTML structure: <div id="div_chat"> <ul id="ul_chat"> </ul> </div> <div id="div_inputchatline"> <input type="text" id="input_chatline" name="input_chatline" val ...

Adding text to a Video Js fullscreen window on an iPad

Looking to enhance user experience on iPad while using a video js player, I want to make an image pop up upon completion of the video. Currently, I have it set up to work without full screen mode, but iPad users prefer watching videos in full screen. Is th ...

Is it possible to impose a different style on an element from another culture?

I am currently developing a themes library along with a demo page. The challenge I'm facing is that the demo page needs to showcase styles from the library without using all of the elements. For instance, consider the following style in an external s ...

Strange circle border appearance in Android / Chrome device

Having an issue with my code on a Samsung Galaxy S5 in Chrome 54.0.2840.85, Android 6.01. You can view the problematic codepen here: http://codepen.io/johnsonjpj/pen/jVpreB The border displays correctly on Firefox and iPhones, but not on my device. Trou ...