Why is the image not resizing based on the dimensions of its container?

I have enclosed an image tag within a div tag.

<div className="product-details-container">
        <div className="product-img-wrapper">
            <ListingCardImage imgAlt={product.title} imgSrc={product.image}/>
        </div>
</div>

Despite setting width and height properties for product-img-wrapper, the image size does not adjust to fit the screen view.

Here is the CSS:

.product-details-container {
  box-sizing: border-box;
}

.product-img-wrapper {
  max-width: 60vw;
  max-height: 50vh;
}

.product-img-wrapper img{
  max-width: 90%;
  max-height: 70%;
  object-fit: contain;
}

The rendered image size is 555x617px, while the dimensions of div.product-img-wrapper are 616.8x364.8. I am interested in understanding why the image is not shrinking and if this is the correct way to wrap an image?

Code for ListingCardImage:

return <>
        <img src="https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg" alt={imgAlt} />
    </>

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

Answer №1

Usually, it is recommended that your img element takes up 100% of the width of its parent container for better responsiveness. To create spacing around the image, you can utilize either padding or margin.

.product-details-container {
  box-sizing: border-box;
}

.product-img-wrapper {
  border: 1px solid red;
  max-width: 60vw;
  max-height: 50vh;
}

.product-img-wrapper img {
  width: 100%;
}
<div class="product-details-container">
  <div class="product-img-wrapper">
    <img src="https://picsum.photos/556/617">
  </div>
</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

Expanding a div with CSS and Angular: A comprehensive guide

For my project, I am working on a specific scenario: When the user clicks on the "Animals" tile, it should expand while the other tiles replace it. Similarly, clicking on the "Plants" tile should expand it and reorder the other tiles. ===========1st View ...

Is there a way to manually trigger a re-render of all React components on a page generated using array.map?

Within my parent component (Game), I am rendering child components (Card) from an array. Additionally, there is a Menu component that triggers a callback to Game in order to change its state. When switching levels (via a button click on the Menu), I want a ...

Unusual functionality when utilizing angular-ui-bootstrap date in an angular-ui-modal

I'm currently working on my debut Angular app and I've hit a roadblock. While using ui-bootstrap date picker in various sections, everything worked fine. However, when I tried integrating it into a modal, an issue arose. After selecting a date c ...

Preventing Continuous Scrolling When a Popup Appears

I am struggling with keeping the scrolling disabled when a popup div is visible in the center of the screen. I have attempted to use an overlay in the browser, but it doesn't seem to be working. Any suggestions on how to achieve this using jQuery and ...

Angular - CSS Grid - Positioning columns based on their index values

My goal is to create a CSS grid with 4 columns and infinite rows. Specifically, I want the text-align property on the first column to be 'start', the middle two columns to be 'center', and the last column to be 'end'. The cont ...

Table Spacing Problem

Check out my website here: I am trying to introduce some spacing between each column element where the coupon ends and the following business name is displayed. It seems like adding a line break is causing issues with the layout. Here is the code snippet ...

Issue with overlapping sticky dropdown and sticky navigation in Bootstrap 4

My issue revolves around getting the dropdown button menu to display in front of the vertical menu. When I click on the dropdown, the vertical (blue) menu takes precedence. This problem arose suddenly after applying the sticky-top class to both menus. Whil ...

Change the color of the text in a shiny dashboard

While exploring shiny dashboard, I came across this link that explains how to modify colors in the sidebar and header. However, I'm struggling to change the font color for sidebar items. The default white font color becomes unreadable when using light ...

Tips for utilizing the two-function in jQuery to showcase one DIV following another DIVNote: When using the two-function

Apologies in advance for my poor English skills, but I have been contemplating whether to write here and if you will be able to understand me. I am currently working on a jQuery script: $(function() { $('#div1').hover(function() { $ ...

The percentage of occurrences for each result with the specific id

In my database, I have a table that looks like this: Currently, in my PHP code, I am performing a SUM operation like this: function calculate_percentage() { $sql = "SELECT (sum(distance))/(25000)*(100) as totaldistance1 FROM axle WHERE train_id = ...

Issues arise when attempting to submit an HTML form using Node.js

I have built a basic registration and login system using node.js + express + SQLite database. I managed to save user information from register.html into the SQLite database successfully, but I am encountering an issue with the login page. I attempted to cr ...

What's the issue with my jQuery AJAX script?

I am experiencing an issue with my ajax pages using jQuery to retrieve content. Only one page seems to be working properly, even though I have multiple pages set up. How can I resolve this problem? $(document).ready(function() { $('.lazy_content& ...

Creating personalized images using HTML

Looking to create a website where you can purchase personalized shoes? One unique feature is the ability to customize your pair of shoes by selecting stickers and choosing their color. These options are conveniently displayed in a panel next to the shoe im ...

Stop menu items from shifting when focused

I've developed a mobile hamburger menu and attempted to adjust the padding to create space between the text and the borders. Here's the HTML: #menu-solutions:hover .menu-item-text, #menu-solutions:focus .menu-item-text, #menu-solutions:ac ...

What is the best way to center align tabs in a Bootstrap 4 list?

I am working with tabs structured like this: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist"> <li class="nav-item"> ...

Quirky rendering problem in AngularJS

My issue involves creating blocks using data from a JSON file, which includes a background-image path for a div. Here is an example snippet: [ .. { .. .. "smallimg": "../assets/images/dummy/dummy-intro-1.jpg", .. ] On my page, I hav ...

Troubleshooting problems with encoding in Python Selenium's get_attribute method

Currently, I am utilizing Selenium with Python to crawl the drop-down menu of this particular page. By employing the find_elements_by_css_selector function, I have successfully obtained all the data from the second drop-down menu. However, when attempting ...

What is the reason behind the immediate firing of the animate callback function when a CSS transition is present?

I am facing an issue where the callback function fires immediately, disregarding the linear ease type and defaulting to the swing ease in CSS transitions. Is there a way to ensure that the animate function works correctly with the transition? The reason fo ...

How can you apply a class to a different element by hovering over one element?

Is there a way to darken the rest of the page when a user hovers over the menu bar on my website? I've been playing around with jQuery but can't seem to get it right. Any suggestions? I'm looking to add a class '.darken' to #conte ...

Make a div come alive with animation when hovered over

I'm trying to achieve a rotating effect on a div when the cursor hovers over it, similar to what is shown in this video. My preference is to utilize keyframes for this animation as they offer more customization options. div.myElement { ... a ...