What is the best way to ensure an image fits perfectly within a div container

I'm attempting to design a straightforward card layout where the image spans the entire width of the container div.
My vision is for it to resemble something like the example in the linked image below.
https://i.sstatic.net/X5oJD.png

I experimented with using object-fit:cover and 100% width but it didn't yield the desired result.

This is how it currently appears:

https://i.sstatic.net/rMlzB.png

Here's the relevant code snippet and associated sandbox link
html/jsx

import React from "react";
import styles from "./Card.module.css";

function App() {
  return (
    <div className={styles.cardWrapper}>
      <img
        src="https://i.ebayimg.com/00/s/MTAyNFg1NzY=/z/qWYAAOSwm1Vcc6F4/$_72.JPG"
        alt="description"
      />
      <h3 style={{ fontSize: "15px" }}>title</h3>
      <div className={styles.description}>description</div>
    </div>
  );
}

export default App;

and css

.cardWrapper {
  width: 165px;
  height: 192px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-shadow: 0 1px 2px 0 rgb(0 0 0 / 25%);
  text-align: center;
  margin: 0 auto;
  align-items: center;
}

.description {
  font-size: 12px;
  color: #808080;
  margin-top: 20px;
  font-weight: 400;
}

.cardWrapper img {
  height: 100%;
  width: 100%;
  object-fit: contain;
  width: auto;
  height: auto;
}

Any assistance would be greatly appreciated

Answer №1

Instead of specifying width: auto and height: auto in your CSS for .cardWrapper img, it is recommended to remove them:

.cardWrapper {
  width: 165px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-shadow: 0 1px 2px 0 rgb(0 0 0 / 25%);
  text-align: center;
  margin: 0 auto;
  align-items: center;
  padding-bottom: 15px;
}

.description {
  font-size: 12px;
  color: #808080;
  margin-top: 20px;
  font-weight: 400;
}

.cardWrapper img {
  height: 100%;
  width: 100%;
}
<div class="cardWrapper">
  <img
    src="https://i.ebayimg.com/00/s/MTAyNFg1NzY=/z/qWYAAOSwm1Vcc6F4/$_72.JPG"
    alt="description"
  />
  <h3>title</h3>
  <div class="description">description</div>
</div>

You had already set the rules to 100% for both height and width, so there was no need to overwrite them. Also, removing unnecessary styles such as object-fit is advised.

@louielyl also pointed out in the comments that removing the absolute height attribute from .cardWrapper is essential to prevent the description from overflowing outside of the wrapper.

Answer №2

Your width:100% and height:100% not taking effect may be due to the subsequent css properties width: auto; and height: auto; overriding them.

To resolve this issue, you can try the following code to ensure your image expands as desired:

.cardWrapper img {
  height: 100%;
  width: 100%;
  object-fit: contain;
}

This behavior occurs because CSS prioritizes the latter styling with the same property, more information on this can be found in the article on Specificity.

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

What is the best way to assign unique IDs to automatically generated buttons in Angular?

Displayed below is a snippet of source code from an IONIC page dedicated to shapes and their information. Each shape on the page has two buttons associated with it: shape-properties-button and material-information-button. Is it possible to assign different ...

Disable, Hide, or Remove Specific Options in a Single Dropdown Selection

A challenge I am facing involves creating a form with multiple select options that need to be ranked by the user from 1-8. However, I am encountering some difficulties in hiding, removing, or disabling certain select options. Below is an excerpt from my f ...

Tips for making markers act responsively

Recently, I put together a collection of rain radar images that can quickly show whether rain is on the way or not. As you resize the URL window, the two pictures positioned side by side will also shrink. For an illustration, check it out here ... Each p ...

Update the HTML page when switching between tabs

Currently, I am experiencing an issue with tab navigation in HTML. Within my code, I have two tabs named #tab1 and #tab2, each containing a form. The problem arises when I input data into #tab1, switch to #tab2, and then return to #tab1 - the information I ...

Angular ng-repeat failing to display data as expected

I've been attempting to create a table from a JSON structure, but I'm having trouble getting it to display correctly. The output is not appearing as expected for the first two cells; "Partial" is empty and only filling the last one. You can see ...

Obtaining select options in jQuery by utilizing the $(this) selector

Looking for a way to loop through and log each option available in a select dropdown box, but struggling with how to use $(this). This is the code I have so far: $('select').each(function(){ $(this).find('options').each(function() ...

Personalizing the structure of a joomla template

Having some trouble customizing my Helix Framework template. I'm trying to adjust the width of the page to be 1140 px. When I make changes in the framework menu, it only adjusts the text field width and not the graphics. How can I achieve the layout s ...

Leveraging ternary operators within HTML elements

Currently, I am utilizing the Vue.js framework to create a dynamic list that updates based on two different list objects. My main objective is to adjust the border of the list cards depending on a specific condition. Below are the defined cards: <li ...

The design template is failing to be implemented on my personal server utilizing node.js

I've encountered an issue while developing the signup page on my local server using Bootstrap 4. The CSS is not getting applied properly when I run it locally, although it displays correctly in the browser. Can someone explain why this is happening? ...

Trouble accessing data with AJAX

I am trying to retrieve content from a web page and display it on my own webpage using AJAX. However, I keep encountering an "Access denied" error before the specified page is fetched. It's important to note that the target page does not require any l ...

What content appears post-iframe?

After researching for a while, I've only come across information about text displayed after a broken iframe on this topic. However, what I actually want to do is display text below an iframe set at 90% height, like so: https://i.sstatic.net/KHXI3.png ...

CSS - Is there a way to alter the arrangement of DIVs depending on the size of the screen?

My inquiry pertains to a CSS layout. I currently have 3 divs positioned in a horizontal line next to each other, as depicted below... div1 div2 div3 I am aiming for the divs to reorganize themselves as the screen size decreases. In one scenario, they sho ...

Clickable list element with a button on top

Within my web application, there is a list displaying options for the user. Each 'li' element within this list is clickable, allowing the user to navigate to their selected option. Additionally, every 'li' element contains two buttons - ...

Creating a conditional statement in jQuery that will append text to a specific DIV element after a form has been successfully

I currently have a form set up that is functioning properly, but I am looking to make some changes. Instead of redirecting the user to a new page with a success message upon submitting the form, I want the success message to be displayed in a div next to t ...

Tips for positioning a div between two vertically aligned input elements

I'm trying to figure out how to place a div between two inputs with the same height and vertically aligned. It seems like a simple task, but for some reason, the code below isn't working: input{ width: 35%; border: 1px solid #A7A7A7; ...

Is there a way to eliminate the horizontal line in my Table design?

Is there a way to eliminate the horizontal line from the table? css, html <table border="1" style="width:100%; border-collapse: collapse"> <tr> <th>Prepared By:</th> <th>Released ...

Maintaining a consistent style input until it is modified

I'm currently dealing with this code (continuing from a previous question): input[type=submit]:focus { background-color: yellow; outline: none; } The issue I'm facing is that when I click anywhere else on the screen, the background color go ...

What is the reason for the 'scrollHeight' being considered 'undefined' in this particular scenario, and why did the iframe encounter an error when switching to the html-file?

This is my first time asking a question, so please forgive any mistakes... I am attempting to create a website using HTML, CSS, and JavaScript on my Raspberry Pi with Apache2. I have written a script for an automatic iframe height function based on the co ...

Imitate the act of pasting content into a Textarea using JavaScript

When data is copied from the clipboard and pasted into an html textarea, there is some interesting parsing that occurs to maintain the formatting, especially with newlines. For instance, suppose I have the following code on a page that I copy (highlightin ...

``A problem with the background image of the left panel in Framework 7

After setting a background image for the side panel and blurring it using CSS, I encountered an issue where the text and icons within the side panel also became blurred. Despite attempting to isolate the background image in a separate class, the problem ...