Adjust the size of the image within a designated container to decrease its proportions when there is an excess of text present

I am working on a project where I need to resize an image based on the available space within its container. When there is text in the description, the image should adjust its size accordingly without pushing the text upwards. I am using React for this project.

Current result when the description is small:https://i.stack.imgur.com/tw9jK.jpg Undesired result when the description is large:https://i.stack.imgur.com/AEuAZ.png Desired result when the description is large:https://i.stack.imgur.com/ASMWM.png

This is my code snippet:

<div className={classes["main-post-container"]}>
      <img className={classes["main-image"]} src={test} alt="test" />
      <p className={classes["main-description"]}>
        Lorem ipsum dolor sit, amet consectetur adipisicing elit. Minus illo
        enim magnam neque fugit rem praesentium voluptate quod architecto aut
        non, id fugiat modi tempora hic sed amet, eligendi delectus. Lorem ipsum
        dolor sit, amet consectetur adipisicing elit. Temporibus obcaecati
        dolore mollitia animi praesentium laudantium sit cumque exercitationem.
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Eos minima
        pariatur quae id et officiis repellat doloribus, beatae ducimus quaerat
        repellendus nesciunt aliquid dolorem recusandae vero officia! Pariatur,
        voluptatem fugiat. .
      </p>
    </div>

CSS:

.main-post-container {
  width: 70%;
  height: 80%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
  padding: 3rem;
}
.main-image {
  max-height: 450px;
  max-width: 800px;
  object-fit: fill;
  flex-grow: 1;
}
.main-description {
  font-weight: 900;
  font-size: 1rem;
  flex-grow: 3;
}

Answer №1

Here is a suggestion for a CSS class you can use:

.featured-photo {
  max-height: 450px;
  max-width: 800px;
  object-fit: fill;
  flex-grow: 1;
  height: auto
}

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

Utilize the power of JavaScript to recursively map object keys

I am working with an array of objects that have varying depths. My goal is to output the names in a specific format: Top Level Top Level > Sub 1 Top Level > Sub 1 > Sub 1-2 Top Level > Sub 2 Unfortunately, I am only able to get the name of th ...

Angular 6 tutorial: Creating a dynamic side navigation bar with swipe and drag functionality using Angular Material/Bootstrap

I am currently working on implementing a vertical swipeable/stretchable side nav-bar with angular-material in angular 6. However, I have encountered an issue with mouse interactions for stretching the nav-bar. Below is the code snippet: Here is the HTML c ...

Implementing a custom error page in Nextjs for failed api requests

I have implemented a custom error page in Next.js to be displayed when an API call fails. However, the issue I am encountering is that even when the API call fails, it still shows the same page as if the route was successful. For instance, I have a route " ...

Is there a simple method to submit to a URL without relying on ajax?

When it comes to using jQuery, the $.ajax() function is typically used for POST requests to a URL. However, in my particular situation, I am unable to use this function. I need the client to send a POST request to a URL and have the server redirect the use ...

Instructions on how to invoke a function defined in a JavaScript module (type=module) from an HTML document

I've been facing difficulties with using JavaScript modules... I have an HTML file and a JS module. In the javascript file, I have a function defined that I want to call from my HTML page. Here is my code: index.html <html> <head> < ...

Mobile Image Sizing with Bootstrap 4 Cards

I am currently developing a responsive web application using Bootstrap 4 and incorporating cards with images on one of the pages. The layout functions properly when resizing the window on desktop view; however, upon viewing it on mobile devices, I noticed ...

What is the best way to utilize variable fonts with Google Fonts?

Traditional fonts typically require a separate file for each weight variant, such as 300, 400, and 500. However, with variable fonts, all weight combinations can be accessed through a single compact font file. This is extremely advantageous for web design. ...

The angular application fails to load the page properly and keeps refreshing itself

I'm currently working on an Angular app that searches for a Github user based on their username and then displays the list of repositories. When a user clicks on a repo name, it should show the open issues and contributors associated with that reposit ...

Position the Bootstrap Button on the right side of the navbar

Hey there, I'm completely new to the world of HTML/CSS and this is my first project ever. I welcome any constructive criticism that can help me improve in the future. Now onto my current dilemma: I've decided to use Bootstrap because it seems u ...

Troubleshoot: Bootbox Confirm Functionality Issues

Can anyone help me troubleshoot this issue? I'm trying to implement code that uses bootbox.confirm when deleting a file, but it's not functioning correctly. $('#fileupload').fileupload({ destroy: function (e, data) { var that = $(th ...

Using Plotly.js within a deleted Vue.js component may result in displaying an incorrect chart

Issue I am facing a problem with deleting one of the components that contains a chart. Even after deleting the component, the chart remains visible. To see an example, check out this jsfiddle: https://jsfiddle.net/m4ywp5fc/4/ Solution Attempted I attem ...

What is the process for adding submitted data to an already-existing local JSON file?

I have a new Angular assignment that requires me to push form data into an existing JSON file locally. The task is to develop an Angular application where users can create new tasks and view them on a separate page. Initially, I attempted using http.post ...

Retrieving the total count of data entries from the JSON server endpoint

Working on a practice application with the JSON server serving as the backend, I have a question. Is there a way to determine the total number of records at an endpoint without actually loading all the records? For example, if my db.json file contains da ...

Send the output of an asynchronous ajax request to the designated function within the jQuery Validation plugin

Utilizing the jQuery Validation plugin (JVP) for server-side validations via ajax has been seamless, except for one hitch: JVP struggles with performing remote validation on blank fields. While it's easy to use its default required: true rule for sim ...

Update the color of the text depending on the background color

When hovering over my CTA, a sliding effect occurs. However, I am facing an issue with the text being difficult to read depending on the background color. To better understand what I'm trying to achieve, you can view the demo here: Demo on CodePen T ...

Generate a blank image using the canvas.toDataURL() method

I've been attempting to take a screenshot of this game, but every time I try, the result is just a blank image. var data = document.getElementsByTagName("canvas")[0].toDataURL('image/png'); var out = document.createElement('im ...

Executing a Shortcode Using a Button in Visual Composer for Wordpress

It should be easy to do this. I've got a great plugin with a modal newsletter signup form that offers various launch options, including manual launching with the following codes. https://i.stack.imgur.com/IGbsp.png My theme utilizes Visual Composer. ...

Enhance the functionality of jQuery within a React application by creating custom chained jQuery functions

Before criticizing me, please hear me out: I understand that React can replace a majority of the functionalities provided by jQuery. However, I need specific interactions to occur when users hover over items, and as far as I know, React does not support th ...

Should I use a separate webpack configuration for server-side rendering in React npm package?

I am the developer of a small npm package and I am currently facing challenges while trying to implement it in a Gatsby site. Despite my efforts, I am struggling to ensure that my npm package is ssr safe due to my limited knowledge of webpack. When attemp ...

Angular 1.6 limits the ability to include multiple custom components on a single page

I'm currently working with angular 1.6 and have encountered an issue with two components, config-list and request-dates. Both components function correctly individually on a page, but when I attempt to add both components to the same page, only the s ...