Ensure child images in a flex row are adjusted to have a height equivalent to 100% of the surrounding content

Consider this example:

.row {
  background: #F88;
  margin-top: 20px;
}

.cont {
  display: block;
  height: 82px;
  width: 82px;
  background-color: #0AF;
}

.txt {
  background-color: #0C0;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row no-gutters">
    <div class="col-auto"><div class="cont">82px</div></div>
    <div class="col-auto"><img src="https://i.imgur.com/WIxmfZb.png" /></div>
    <div class="col"><div class="txt">Text content</div></div>
  </div>  
</div>

I am looking to resize a 100x100 image to match the height of the adjacent column while preserving its aspect ratio.

I have tried various combinations of CSS properties like flex-basis and flex-shrink without success. One approach involved setting the image's style to height: 0; min-height: 100%;, but it caused issues in Chrome and did not work in IE.

  • The initial width of the image became 0 in Chrome until window resizing.
  • The method did not function properly in IE.
  • It seemed like an overly complicated solution that would confuse future developers.

After spending hours trying different solutions, I still haven't found one that meets all the requirements:

  • The image fills 100% of the container's height.
  • The aspect ratio remains unchanged.
  • No overflow occurs.
  • Absolute positioning is avoided.
  • The image size is not explicitly set.

The desired outcome is illustrated here:

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

Any assistance on achieving this goal would be greatly appreciated.

Answer №1

To ensure the image aligns with the first column's height, I constrained its width to match:

.align-with-col-1 {
  max-width: 82px;
}

I wasn't certain if the third column could be taller, but restricting it to the same height seemed to work well, although practical application may vary.

In any case, the image appears to fulfill the criteria as it occupies 100% of the flex height, maintains its aspect ratio, does not overflow, and is not absolutely positioned.

.row {
  background: #F88;
  margin-top: 20px;
}

.cont {
  display: block;
  height: 82px;
  width: 82px;
  background-color: #0AF;
}

.align-with-col-1 {
  max-width: 82px;
}

.txt {
  background-color: #0C0;
  max-height: 82px;
  overflow-y: scroll;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row no-gutters flex-row flex-nowrap">
    <div class="col-auto">
      <div class="cont">
        82px
      </div>
    </div>
    <div class="col-auto">
      <img class="align-with-col-1" src="https://i.imgur.com/XyzabCd.png" />
    </div>
    <div class="col">
      <div class="txt">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer at mollis turpis. Nullam finibus orci augue, in tincidunt nibh rutrum eget. Vestibulum euismod ligula a sapien consequat vestibulum.<br>Morbi varius feugiat est sed dignissim.
          Curabitur tristique ante dui, ut laoreet diam commodo ac. Fusce dictum imperdiet est quis vulputate. Aliquam erat volutpat.<br>Duis fringilla felis nec felis cursus, id laoreet purus condimentum.<br>Donec tortor urna, ullamcorper vel eros
          sed, vehicula pellentesque orci.</p>

        <p>Sed non arcu facilisis, posuere urna et, efficitur justo. Praesent vitae leo viverra, scelerisque libero ac, malesuada risus. Phasellus suscipit quam eu tellus molestie fermentum. Aliquam elementum urna nec arcu eleifend, ut bibendum nunc
          ultrices. Nunc sodales tempus pretium. Cras convallis accumsan lacus, quis placerat odio maximus vitae. Nam id ante venenatis, hendrerit turpis in, faucibus sem.</p>
      </div>
    </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

Unable to conceal tab content upon clicking the identical tab

I recently implemented a tab system with panels that seems to be working well when switching between tabs. However, I'm facing an issue where clicking on the same tab does not hide the corresponding panel and reset the aria attributes as intended. I&a ...

Why do div and span display differently across browsers and how can we standardize their appearance?

My CSS file is currently displaying correctly in FF12, but the layout is different in Safari and Chrome. If images are not loading, you can view a live demo at This is how it should appear and does in FF: However, this is what happens in Safari and Chro ...

How to position Bootstrap 4 pills in the center for optimal alignment on smaller screens

Are there any special Bootstrap 4 features that will align these three items at the center in a row on a large screen, with a small space between them, and then stack them on top of each other on a small screen? <h4 class="h4">Follow Social Media< ...

Ensure that the navigation menu is consistently displayed properly, without any of its contents extending beyond the

I am experiencing an issue with the navigation menu on my website. Everything looks great in normal view, but when I resize the browser window to a smaller width, the menu ends up overflowing below the main content. I want the navigation menu to always dis ...

Enhance your iPhone web app with high-resolution retina images!

As I develop a mobile web application accessible on any smartphone, I have the index.html file available for review: The app includes 2 jQuery functions. One detects if the user is using an iPhone and displays a bubble with instructions to add it to the h ...

What is the best way to incorporate media queries in order to reduce the padding around my image?

Currently, I am working on a small gallery and have successfully implemented Salvattore (similar to Masonry). However, a challenge has arisen when resizing the web page - the padding remains at 10px instead of reducing to 5px as desired. The goal is to e ...

Switching the color of the nav-link in Bootstrap

Struggling to change the color of my Bootstrap's nav-link to white with no success. Here is my code snippet: <nav class="navbar navbar-light" style="background-color: #5E9DFE;"> <a class="navbar-brand" href=& ...

What is the best way to set the exact pixel width for a sidebarPanel?

Is there a way to define the precise width of a sidebarPanel in pixels instead of using the width argument? I have found that the width option is not precise enough for my needs. library(shiny) ui <- fluidPage( sidebarLayout( sidebarPanel(widt ...

Learn the steps to create a vertically tiled background using a clipped image

My goal is to create a vertically tiled background by clipping the top 50px of a background image and using only that portion. However, the code I have below isn't working as expected. Even though I've given a higher z-index to the <div class= ...

Tips for retrieving selected items in an array object

Within my UI, I have a select field that, on change, populates corresponding data in a div with nested ul and li elements. What I am attempting to achieve is to convert the selected items within the list (which include checkboxes) into an object of arrays ...

Dynamically load WordPress content using ajax and incorporate CSS3 animations for added visual appeal

It's time for a new challenge to push my learning curve. I've never ventured into the world of ajax before, but it seems like a skill I need to acquire. What better opportunity to learn than by implementing it on a fresh portfolio site. The main ...

Which is better for React: Bootstrap or Material UI?

In my experience working on various projects, I have encountered the situation where I need to incorporate a Material UI component into a bootstrap component. Surprisingly, this integration has worked seamlessly and the user interface appears as expected ...

Tips for enhancing the presentation of JSON information

I recently ventured into the world of JSON and JS, managing to create a JSON file to showcase data using .onclick event. While I have successfully generated and displayed the JSON data on screen, my next goal is to present it in a table format for better c ...

Unable to modify the height and color of tabs in Angular 6

I am in the process of learning Angular, CSS, and HTML. I have been using MatTabsModule to create tabs, but I am having trouble adjusting the height, background, and other properties of the tabs. I am struggling to override the default properties of MatTab ...

Revamp the Bootstrap 4 Form Check: Rearrange Options from left-to-right and top-to-bottom to top-to-bottom and left-to-right

I am currently using a Bootstrap form that displays a checkbox with 13 different options, including: Urban Plans Commercial Entity Cultural Approval Education Sector Hospitality Industrial Design Interiors Art Leisure/ Sporting Residential Care Retail Spa ...

How to Turn Off Hover Effect in Essential Grid for Mobile Devices

My website, tyloz.com, features four panels on the homepage that act as landing pages. These panels have a hover effect that also works on mobile devices. However, I am looking to disable the hover and double tap functionality specifically on phones. Is th ...

HR | extends all the way down the table, creating a vertical boundary

I am currently working on developing an Email Signature Generator. My goal is to extend the blue line (visible in the program) all the way down without affecting the layout of other elements. I suspect that because everything is contained within a table, a ...

"Utilizing JavaScript, you can remove an element by clicking on the outer element within the

I need the functionality where, when a user clicks on an input type="checkbox", a corresponding textarea is displayed. Then, if the user clicks anywhere except for that specific textarea, it should be hidden. Can someone assist me with implementing this fe ...

Organize objects neatly in a grid formation

I have a .test div with grid-template-columns: auto auto auto;. The column width is set to vary based on the element width, and I also used justify-content: start; to align the columns to the left. I chose to use auto instead of 1fr to prevent the columns ...

Tips for aligning content vertically in bootstrap 4

Looking to vertically center content in Bootstrap 4? <div class="container"> <div class="content"> <div class="row"> <div class="col-6 align-middle" ...