What is the best way to restrict the height of an image to match the height of another flexbox column

I am working with a flexbox layout that has two columns. The first column contains an image, and the aspect ratio of the image is unknown at design time. My goal is to display the full image while preserving its aspect ratio. The second column consists of multiple lines of text, the length of which is also unknown during design. It is important that the container does not have a fixed height.

My question is, how can I utilize the space in the first column for the image without allowing the height of the image to exceed the height of the text in the second column?

In the code below, I want to ensure that the black image's height is either less than or equal to the height of the right column with a gray background; it should never be taller.

.row {
  display: flex;
  padding: 5px;
  margin: 5px;
  align-items: flex-start;
  background-color: blue;
}
.col1 {
  flex-grow: 1;
  background-color: yellow;
  align-self: stretch;
}
.col2 {
  background-color: lightgray;
  padding: 10px;
  min-width: 300px;
}
img {
  width: 100%;
  height: 100%;
  max-height: 100%;
  object-fit: contain;
}
<div class="container">
  <div class="row">
    <div class="col1">
      <img src="https://dummyimage.com/256x256/000/fff">
    </div>
    <div class="col2">
      <p>Here's some text</p>
      <p>And some more text</p>
      <p>And some more text</p>
    </div>
  </div>
</div>

Answer №1

One way to achieve an average result is by using height:0; along with min-height:100%, although the parent's width may not be easily updated.

.row {
  display: flex;
  padding: 5px;
  margin: 5px;
   background-color: blue;
   justify-content:start;
}
.col1 {
  flex: 1 1 auto;
  background-color: yellow;
  align-self: stretch; 
}
.col2 {
  background-color: lightgray;
  padding: 10px;
  flex:1 0 auto; 
}
img {  
  height:0;
  min-height: 100%; 
  min-width:100%;
  object-fit: contain;
}
<div class="container">
  <div class="row">
    <div class="col1">
      <img src="https://dummyimage.com/256x256/000/fff">
    </div>
    <div class="col2">
      <p>Here's some text</p>
      <p>And some more text</p>
      <p>And some more text</p> 
    </div>
  </div>
</div>

Answer №2

In order to achieve a similar outcome, it is necessary to set the width of at least one column as fixed. In this particular scenario, I have opted to fix the width of the image at 256px.

.row {
  display: flex;
  padding: 5px;
  margin: 5px;
  align-items: flex-start;
  background-color: blue;
}
.col1 {
  flex: 0 0 256px;
  background-color: yellow;
}
.col1 > img {
    width: 256px;
}
.col2 {
  flex: 1;
  background-color: lightgray;
  padding: 10px;
}
<div class="container">
  <div class="row">
    <div class="col1">
      <img src="https://dummyimage.com/600x600/000/fff">
    </div>
    <div class="col2">
      <p>Here's some text</p>
      <p>And some more text</p>
      <p>And some more text</p>
      <p>And some more text</p>
      <p>And some more text</p>
      <p>And some more text</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

Fade between two elements using jQuery

Can someone assist me with adding a fade effect between elements in my code? Any advice or suggestions would be greatly appreciated! $("#example p:first").css("display", "block"); jQuery.fn.timer = function() { if(!$(this).children("p:last-child").i ...

Adjust the size of the child div to fit the remaining space within the parent div

I'm dealing with a situation where there are two child divs inside a parent div. The first child div takes up 32% of the width, while the second child div takes up 68% of the width. If I were to set the display of the first child div to "none", how ca ...

Unable to retrieve all form properties when using enctype='multipart/form-data'

Recently, my NodeJS server has been successfully uploading files to Amazon using a secret form. However, in an effort to enhance security measures, I decided to introduce a password field to the form. Unfortunately, upon doing so, I encountered an issue wh ...

Generating an HTML representation of an Excel file

I have a text file with a .xls extension that contains the following markup (see below). It opens in Excel 2003 without any issues. Now, I am curious what technology allows for creating an Excel file using HTML markup? I need to address this issue so tha ...

Browsing with PHP/HTML on Safari from Mac Os 10.6.5 seems to run at a sluggish pace

This is the program: <?php require_once 'swift-mailer/lib/swift_required.php'; $name = $_POST['name']; $email = $_POST['email']; $form_subject = $_POST['form_subject']; $form_message = $_POST['form_message ...

Finding the label that is linked to the current DIV or textarea by its "for" property

I am working on a project that involves two elements - a textarea and a div. Each element has a label attached to it using the "for" attribute in HTML. <textarea id="txta_1" class="txta" cols="40" rows="3" onkeyup ...

What is the best way to present progress bar numbers in Bootstrap beneath the bar?

I have successfully implemented a progress bar using Bootstrap. Now, I am looking to display numerical values below the progress bar, similar to this: https://i.sstatic.net/3QG8d.png Is there a way to achieve this without utilizing JavaScript graphing l ...

Creating Structured Lists with HTML Columns

Can you assist me in achieving a single column layout for all the bullet points, as I would like to experiment with various designs? Below is what my current display looks like and how I envision it to be presented: https://i.stack.imgur.com/NhMCC.jpg Pl ...

Limit the character input in AngularJS/Ionic to a specific number

I have a text input field that needs to restrict the user from entering more than a specific number of characters. Let's say I only want the user to type 10 characters, and if they try to enter more, the text field should not accept it. It should stop ...

Avoid placing dropdown items within the navbar container in Bootstrap 4

When collapsing the screen, I encountered an issue with the dropdown items being inside the navbar. I positioned the dropdown menu as absolute, but some items were getting cut off, and I didn't want the scroll bar to appear. Despite trying to use medi ...

What is the best way to remove the empty space on the right side of a webpage while ensuring it remains responsive?

I noticed that there seems to be some extra space on the right side of the layout. I'm not sure how to adjust the Bootstrap code or CSS to fix this issue, or if I need to apply margin 0 or padding 0 somewhere in the code. * { box-sizing: border-b ...

Tips for accessing various ejs files in an html document efficiently to reduce code verbosity

How can I streamline navigation to multiple ejs files in my HTML without repetitive code in my server.js file? I am using Express with hidden file extensions. Below is an example of my current setup. HTML <a href = "/contact"> Contact Us & ...

Troubleshooting: Custom checkbox using CSS ::before does not display properly on Firefox and Edge browsers

Encountering a frustrating CSS challenge while working on a project that needs to be compatible across various browsers. Specifically, I'm having issues with custom styling for checkboxes. Despite following recommendations to use a ::before pseudo-ele ...

Displaying a username with HTML and Node.js: A step-by-step guide

Hey there! I'm currently working on a simple login page and I'm looking to display the username once a successful login occurs. However, I'm unsure how to achieve this using just HTML. While I've come across various resources on tools l ...

Steps for triggering an event based on a user-provided time input

I'm developing an AC controlling application that allows users to specify a time for all building AC units to be automatically turned off using HTML, jQuery, and CSS. I need to implement a feature where the system compares the user-entered time with t ...

Guidelines for providing the image file path in an external JavaScript file for creating a Flask API

I am looking to modify the attribute value in the index.html file from an external JavaScript file that is linked to the HTML file. The structure of my folders is as follows: folder structure When I try to specify the path from the HTML file using: {{url ...

Arrangement of items in a grid with various sizes

I have encountered a challenge that I cannot seem to overcome. In my grid system, there are 18 identical items/boxes. I am looking to remove 4 of those items/boxes and combine them into one large item/box. Refer to the wireframe below for guidance on how ...

How to dynamically set a radio button as checked or active based on the current state value

Currently, I have a header that rotates through 4 images every 5 seconds. Users can manually cycle through these images using 5 radio buttons. The styling for the active/checked radio button is only applied when users click on it themselves. However, if t ...

Canceling out the overlay opacity of divs

Is there a way to ensure that when overlaying two divs with semi-transparent backgrounds, the opacity of each div does not add to one another? In simple terms, I want to maintain consistent color between the two divs even when they overlap. I have created ...

Using z-index to arrange elements in front of each other is not effective

To demonstrate my issue, I have created a fiddle: http://jsfiddle.net/AVxbd/ The problem lies in a menu (#container) with links (#text1, #text2). An element called #highlighter is used to highlight the link of the current page. Here is the HTML: <div ...