Aspect ratio of images becomes distorted when setting them to occupy 100% height/width of the container

Check out my current style:

.album-photos-container {
  position: relative;
  display: flex;
  flex-flow: wrap;
  justify-content: center;

  .image-card-small {
    width: 48%;
    margin: 4px;
    filter: brightness(0.8);
    img {
      width: 100%;
      height: 100%;
      max-height: 400px;
    }
  }
}

This is how it appears with distorted images.

https://i.sstatic.net/13vd0.jpg

I've been exploring display: flex; and flex-flow: wrap; and it seems like they're causing the issue. I might need a different approach.

I attempted to increase the max-height: 400px; to 2000, resulting in this:

https://i.sstatic.net/Fgpsx.jpg

Please give me some advice: I want the images to fill the box regardless of their dimensions!

UPDATE

Here's the component code:

import React from 'react'

const ClickablePhoto = (props) => {
  return (
    <div onClick={props.onClick} className="image-card-small">
      <img  src={props.src} id={props.id}  alt="album" />
    </div>
  )
}

export default ClickablePhoto

I also tried this alternative approach:

  return (
    <div onClick={props.onClick} style={{backgroundImage: 'url(' + require(`${props.src}`) + ') background-size: cover'} } >
      <img  src={props.src} id={props.id}  alt="album" />
    </div>
  )

However, the image source from the props looks like this:

/static/media/0_sJ1A5jGwSm66KCdV.e2451a3a.png
, which causes issues with the background-image: url.

Answer №1

When it comes to cropping images, the <img> element may not be the best option. Instead, you can achieve a similar effect by using a div with a background image:

<div class="image-container" style="background-image: url(...); background-size: cover;" />

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

The column on the right does not extend all the way to the bottom of the page

Hello, I currently have a website with the following design: I'm looking to extend the right column all the way down to the bottom of the body, even if there's not enough content to push it down. How can I accomplish this using CSS? Here is the ...

Issue with flickering scroll-snap in React component with useState

I've encountered an unusual flickering glitch that only occurs when using the combination of: css scroll-snap useState Sub-Components But it's strange because the issue only arises when these three are combined! https://i.sstatic.net/TvwyW.gif ...

Preventing jQuery from Delaying Hover Effects

I am currently working on a navigation menu where two images are stacked on top of each other, and jQuery is used to create a fade effect on hover. However, I have encountered an issue where the effects buffer if the mouse cursor is moved back and forth ov ...

Troubleshooting: Bootstrap menu bar malfunctioning when collapsed

On small devices, my website's menu collapses as expected. However, when clicking on the collapsed menu icon, the menu fails to appear. Instead, there is a slight adjustment in the vertical height of the page below the menu, and the last menu item par ...

From where was this color of the navigation bar derived?

As I delve into a site previously worked on by someone else, my task is to recreate the navigation they implemented. However, I am unable to locate in the source code what was used for the navigation background. The challenge lies in the fact that after m ...

What is the best way to eliminate the margin from the final li element using only CSS and ensuring compatibility with IE 6

Is there a way to achieve not applying margin-right to the last li, in this specific condition? I am looking for a pure CSS solution that is compatible with IE6 and 7, without altering the existing HTML. Can this be done? ul li {display:inline;margin-rig ...

What is the best way to align a set of input fields with their corresponding labels, along with a single button?

https://i.sstatic.net/xSwSH.png I am facing a specific alignment challenge in my project, especially due to the presence of labels. When I group inputs and a button together and use align items center, it works perfectly. However, the labels affect the he ...

Setting a customized background color for a designated section of a component

I am trying to create a hover effect at the position of a cursor, similar to what is shown in this example: For reference, I have also created a code snippet: https://jsfiddle.net/onnmwyhd/ Below is the code I am using: HTML <div id="container&q ...

Header Stretch: (1) will maintain its original size on iPhone devices (2) ways to define maximum width for desktop view

I'm currently tackling a challenge with a CSS tumblr theme header that can be found at grandneue.tumblr.com. (1) Stretch: I need the header to have the capability to stretch, but only up to a maximum size of around 800px as it becomes too pixelated b ...

Creating a single row with five columns using HTML and CSS

I'm currently working on setting up a layout consisting of 1 row and 5 columns placed side by side. These columns are going to showcase the projects that I intend to feature on my online Portfolio. To achieve this layout, I am utilizing the Bootstrap ...

Unlimited Rotation with CSS3

I'm attempting to create a spinning image effect that rotates 360 degrees infinitely on hover. However, it seems that the background element I've used to display the image might be causing issues. I'm not sure how else to display the image w ...

Opting for the `.toggle()` method over using a class to display content

I'm currently developing a website for use at a conference, and I'm seeking assistance in comprehending the implications of two different strategies to achieve a certain effect: Option 1: Utilizing .toggle() to show or hide content I initially ...

Elegant Bootstrap 4 Carousel featuring a glimpse of the upcoming slide alongside the primary carousel item

I am in search of a straightforward Bootstrap 4 carousel that showcases a glimpse of the next slide on the right. Despite exploring similar questions, I have not found a suitable solution. The links to those questions are: 1)Bootstrap carousel reveal part ...

Dropdown of ui-select in AngularJS is positioned behind a div element, causing

The ui-select-match and choices component seems to be hidden behind the div/form structure. This particular ui-select component is enclosed within several parent elements, including some divs and forms. It eventually renders as follows: > <ui-select ...

The jQuery UI dialog stubbornly refuses to close when tapped, yet opens without hesitation

I'm struggling to understand why my dialog boxes are not closing when I tap outside of them. I've tried using different selectors such as document, window, .ikon_picmap, but nothing seems to be working. What am I missing here? Check out the code ...

Tips for picking out React subcomponents using CSS

Currently, I am creating a SCSS file for my React application that incorporates React Semantic UI (). I am encountering difficulty selecting subcomponents within the CSS. Specifically, how can I target elements like <Item.Group> tag? I attempted usi ...

Personalized text hues for your WordPress product page

As someone who is new to all of this, please keep that in mind :/ I decided to remove the "add to cart" button from my theme (blocksy) so I could insert my own buttons within the product page under the "short description" text. I needed two "download" but ...

Creating a responsive design with HTML and CSS that expands an image to hold additional text

I have a blue bubble image that I want to overlay text on. The amount of text will vary, so I need the image to expand vertically to accommodate more text. I'm hoping for some guidance on how to make the image adjust its size based on the amount of ...

The angular view is lacking a CSS class

index.html: <div ng-view="myview.html"></div> myview.html: <table class="table table-striped table-hover"> <tr> <td>Name</td> <td>Product</td> </tr> <tr ng-repeat="deal in deals" class="clickableR ...

Customize the default page type on your WordPress website by using a page-columnist

Hey there, I'm having trouble setting the default column type in page-columnist. It keeps going back to WordPress - Next Page as the default, but I want it to be Ordinary Plain Page instead. I went through the code and found an array called 'pag ...