How to maintain aspect ratio when setting max-height on React strap/Bootstrap CardImg?

Currently working on integrating the front-end with a MERN stack application and facing challenges with Reactstrap Cards and CardImgs. I am specifically trying to ensure that the images do not exceed a certain max height, while also keeping all the content within the Card (this includes CardTitle, CardText, etc).

I have explored different suggestions on StackOverflow, such as setting max-height on the image and width:auto, but the image ends up being vertically squeezed - it follows the max-height but does not adjust the width leading to a distorted appearance. After spending an hour trying out various solutions, I haven't been successful. It seems like a simple issue, but I'm unable to find the right solution. Any assistance would be greatly appreciated!

Please refer to the attached image for a visual representation of the issue.

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

EDIT: As requested, here is the HTML and CSS. The following code works relatively well, but taller images get distorted when their height is limited. The HTML (from JSX) is as follows:

.project__card {
  border: solid 1px grey;
  max-width: 50vw;
  margin-bottom: 3em;
  padding: 3em;
}

.project__card .project__cardImage {
  width: auto;
  height: auto;
  display: block;
  max-height: 50vh;
}

.project__card .project__cardTitle {
  font-size: 1.2em;
}

.project__card .project__cardSubTitle a:active,
.project__card .project__cardSubTitle a:link {
  text-decoration: none;
  color: yellow;
}

.project__card .project__cardSubTitle a:hover,
.project__card .project__cardSubTitle a:active {
  text-decoration: underline;
  color: darkred;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<div class="projectGroup__project col-12 col-md-8">
  <div class="project__card card">
    <img width="100%" src="https://via.placeholder.com/550x350" alt="" class="project__cardImage card-img-top">
    <div class="project__cardBody card-body">
      <h5 class="project__cardTitle card-title">A title</h5>
      <h6 class="project__cardSubTitle card-subtitle"></h6>
      <p class="card-text">Some text</p>
      <p class="project__cardTechnologies card-text"><span>Roles:  </span>Design, Client Contact
      </p>
    </div>
  </div>
</div>

Answer №1

At the moment, both height and width are being set to auto. To achieve the desired effect, you should set one of them to 100% and the other to auto, depending on which measurement you want to control the scale.

.project__card {
  border: solid 1px grey;
  max-width: 50vw;
  margin-bottom: 3em;
  padding: 3em;
}

.project__card .project__cardImage {
  width: 100%;
  height: auto;
  display: block;
}

.project__card .project__cardTitle {
  font-size: 1.2em;
}

.project__card .project__cardSubTitle a:active,
.project__card .project__cardSubTitle a:link {
  text-decoration: none;
  color: yellow;
}

.project__card .project__cardSubTitle a:hover,
.project__card .project__cardSubTitle a:active {
  text-decoration: underline;
  color: darkred;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<div class="projectGroup__project col-12 col-md-8">
  <div class="project__card card">
    <img width="100%" src="https://via.placeholder.com/550x350" alt="" class="project__cardImage card-img-top">
    <div class="project__cardBody card-body">
      <h5 class="project__cardTitle card-title">A title</h5>
      <h6 class="project__cardSubTitle card-subtitle"></h6>
      <p class="card-text">Some text</p>
      <p class="project__cardTechnologies card-text"><span>Roles:  </span>Design, Client Contact
      </p>
    </div>
  </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

Opening Tags in Selenium for HTML

Is it possible for Selenium with XPath to properly work with HTML that is not in the form of XML, and contains open tags like <col> and <input>? I'm curious if anyone can confirm this. I ask because our automation testing team has noticed ...

Confusion arises when using Materialize CSS for selecting elements

Currently, I am working on an app using Cordova. Within this project, I have incorporated jQuery Mobile's CSS and JS along with Materialize CSS and JS. However, I recently encountered an issue after integrating the Materialize files. Specifically, whe ...

Differences in rendering with Google Chrome when zooming in or out

After creating some code, I observed a peculiar issue. At 500% zoom, the DOWNLOAD button aligns perfectly with the left wall without any gap. However, upon reducing the zoom to 250%, a green background segment becomes visible. To witness this anomaly, refe ...

Creating responsive images using Bootstrap CSS framework

I have been focusing on optimizing the banner below the navigation menu on this website. My goal is to make it responsive so that the player in the banner remains visible even when scaling down to iPhone size portrait. You can find the code for this secti ...

The iconbar feature in the mobile menu is experiencing functionality issues

I am currently experimenting with the iconbar property of mmenu (here: ) Unfortunately, I am encountering an issue. The menu opens as expected when I run the code. However, upon closing the menu, it first closes completely and then the container slides sl ...

Filtering elements upon loading

I have successfully implemented data filtering on my website using data-filter. While everything is working great, I am interested in displaying items with specific attributes upon loading. For instance, I want to load only green objects by default inste ...

Python scripts are unable to perform file uploads through an HTTP form, unlike PHP scripts which are compatible

Currently, I am working on a Python project where I need to upload a text file through HTTP to my server. Initially, I used an HTTP form for convenience and the upload process was successful. However, now I want to automate the file selection process by ha ...

calculating the duration between successive PHP form submissions

I am trying to calculate the duration between when a user submits a PHP form and when they submit it again. The form reloads on the same page, essentially refreshing it. Additionally, the user may enter the same data again. I want the timer to start runnin ...

Update background to full screen and include text when hovering over DIV

Currently, I am utilizing Bootstrap and have a layout with 6 columns containing icons/text within. I desire to implement a hover effect where each column triggers a background change in transition for the entire section. In addition, text and button elemen ...

Having difficulty getting the sign operator to show up in a text field

Whenever the ADD div is clicked, "+" should be displayed on the textbox. The same goes for SUBTRACT, MULTIPLY, and DIVIDE. However, I am struggling to make the operators show on the textbox. Here is what I have managed to come up with so far. <!D ...

Tips for integrating CSS with Material UI TableContainer

I have utilized Material UI Grid to display data in a chart. However, the appearance of the chart does not match my expectations. Instead of resembling the desired "dense table," it looks different: Actual Look of the Chart Below is the code snippet I ha ...

Implementing automatic dark mode activation during nighttime with jQuery or JavaScript

I'm looking to implement an automatic dark mode feature on my website that switches on at night and off during the day or morning. Currently, my website has a dark mode toggle code that allows users to switch between dark and light modes using local ...

CSS animations are not functioning properly on disabled Bootstrap buttons

One issue I'm facing is with a BootStrap button that I'm rendering in React. The button has a property or a condition that determines if it's disabled: <Button bsClass="fill" disabled={!props.purchaseble} onClick={conso ...

The Challenge of CSS Transition and Checkbox Label Discrepancies

I'm looking to adjust the position of my label when a checkbox is checked. Everything works smoothly if I don't include a transition for the top offset property in the CSS of my label. However, when this transition is added (as seen in the commen ...

What are the best practices for utilizing *ngIf?

In my Angular project, I am facing a challenge with using *ngIf. My app.component.html file handles both the login page and the dashboard. I want to hide the dashboard until the user logs in. To achieve this, I decided to use *ngIf. Here is how I implement ...

Creating circular patterns with looping on a canvas

My goal is to draw circles in a loop, but when I execute my code, I am encountering an unexpected result: The intention is to simply draw 3 circles in random positions. Here is my current code: for (var i = 0; i < iloscU; i++) { ctx.strokeStyle = ...

Running of code in <script> tag

At what point does the code inside script tags start executing? Does it happen in order? <html> <head> <title>Canvas tutorial</title> <script type="text/javascript"> function draw(){ var canvas = document.getElementById ...

What is the best way to calculate the number of div elements with a specific class that are contained within parent div elements with another specific class?

Is there a way to count the number of elements with the class '.child' in each container and then add a sentence containing that count inside each container? <div class='container'> <div class='child'></di ...

show tab focus outline only

In search of a straightforward and effective method for focusable elements to display an outline only when the tab key is pressed, without showing it when using a mouse in React applications. (looking for something similar to :focus-visible that function ...

What is the best way to extract the content within a nested <p> element from an external HTML document using the HTML Agility Pack?

I'm currently working on retrieving text from an external website that is nested within a paragraph tag. The enclosing div element has been assigned a class value. Take a look at the HTML snippet: <div class="discription"><p>this is the ...