Adjust the height of the flex column to match the height of the tallest element

I've been experimenting with different approaches, but I just can't seem to solve this issue.

Currently, I am utilizing Bootstrap 4.3.1 and working with the following div structure:

    <div class="d-flex flex-row align-items-center">
        <div class="col-8">
            <h3>A delightful heading</h3>
            <ul>
                <li>Content</li>
                <li>Content</li>
            </ul>
        </div>
        <div class="col-4 img-wip">
            <p>Hello</p>
        </div>
    </div>

The challenge lies in having one column at col-8 width and another at col-4 width. The second column has a background image specified by the img-wip class. Although the content "Hello" is nicely centered, I'm struggling to make the background image cover the same height as the content within the left col-8 column. The right col-4 box simply won't expand accordingly.

I have attempted using height:100%, flex:1;, flex-grow:1;, among other things.

However, the only approach that seemed to impact the background image's height around <p>hello</p> was specifying a specific numerical height, which isn't my desired solution. Ideally, I would like the height to be determined by the col-8 column height.

Is it feasible to achieve this goal, or am I misunderstanding how flex operates?

The CSS for the .img-wip class is as follows:

.img-fill {
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    background-image: url(../images/resin_wip.jpg);
}

Answer №1

You've grasped the concept of flexbox quite well, but there are some misunderstandings present. By default, flex-items stretch in the cross-axis. In this particular case, no content is being placed within the div with the img-wip class (

<div class="col-4 img-wip">... </div>
).

The issue arises from setting a background-image in your CSS. The height of the flex-container is determined by the tallest flex-item (this behavior is standard in Flexbox), and in your code, it's the first column -

<div class="col-8">
.

Adding more content to the col-8 div will gradually reveal the image, or setting the height of the col-4 div to match the image's height will also adjust the height of the col-8 div accordingly.

In an alternative scenario, directly inserting the img tag into the col-4 div results in displaying the full image, with the col-8 div appropriately adjusting its height based on col-4 (as expected in flex-box).

This behavior can be seen in the provided CODE SNIPPET:

.img-wip {
  background-image: url("https://res.cloudinary.com/diqqf3eq2/image/upload/v1583368215/phone-2_ohtt5s.png");
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>JS Bin</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <!-- Required meta tags -->
        <meta charset="utf-8" />
        <meta
          name="viewport"
          content="width=device-width, initial-scale=1, shrink-to-fit=no"
        />

        <!-- Bootstrap CSS -->
        <link
          rel="stylesheet"
          href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
          integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
          crossorigin="anonymous"
        />

        <title>Hello, world!</title>
      </head>
      <body>
        <div class="d-flex flex-row align-items-center">
          <div class="col-8">
            <h3>A lovely header</h3>
            <ul>
              <li>Content</li>
              <li>Content</li>
            </ul>
          </div>
          <div class="col-4 img-wip">
            <p>Hello</p>
            <img
              src=" https://res.cloudinary.com/diqqf3eq2/image/upload/v1583368215/phone-2_ohtt5s.png "
              alt=""
            />
          </div>
        </div>

        <!-- Optional JavaScript -->
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script
          src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
          integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
          crossorigin="anonymous"
        ></script>
        <script
          src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
          integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
          crossorigin="anonymous"
        ></script>
        <script
          src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
          integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
          crossorigin="anonymous"
        ></script>
      </body>
    </html>
  </body>
</html>

CODE FOR THE ALTERNATIVE SCENARIO: Keep the HTML code as is and append the following CSS.

CSS:

.img-wip {
 background-image: 
 url("https://res.cloudinary.com/diqqf3eq2/image/upload/v1583368215/phone-2_ohtt5s.png");
 background-position: center;
 background-repeat: no-repeat;
 background-size: cover;
 min-height: 60vh;
}

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

Challenges with KendoUI integration

In one of my web pages, I have a combination box and a checkbox. To enhance the functionality, I decided to utilize the KendoUI library. The combo box allows users to choose between two options - 0% and 100%. If the checkbox is selected, the combo box shou ...

What is the reason that textContent assignment does not function properly when used with HTML tags?

I have been attempting to insert additional text into an existing element using the textContent property. However, when I use the += operator for addition assignment, it seems to strip away the formatting of pre-existing tags like b. I expected that the ne ...

The PDF file was unable to appear on the mobile web browser in Laravel 7

Perhaps this question has been asked several times, but I am struggling with displaying a PDF Blob on a mobile screen web browser. I have attempted to use both the embed tag and iframe tag, however, they are not functioning properly on mobile screens, alth ...

"Troubleshooting the Undefined Object Dilemma

I previously asked a similar question, but I have since made some progress. Currently, I have two JavaScript files for a menu bar and an additional two for another object on the page. This is my code that is currently causing an issue: <html xmlns="h ...

Alter the preceding element within the DOM upon hovering with CSS

Could there be a CSS solution to this dilemma or would we need the assistance of JavaScript to navigate the DOM? Here's the HTML snippet in question: <div class="thumb_ctn"> <div class="overlay"> <div id="link_text">SOME ...

Voice recognition feature in the latest version of Chrome on Android devices

When using a PC with Chrome 25 (non beta), I see a microphone icon that prompts for input when clicked. After speaking, my alert call is successfully executed. However, on a Galaxy Note smartphone with Chrome 25 and an Android 4.04 operating system, the sa ...

Inserting images into a MySQL database using Ajax and file uploads

I am using JQuery Mobile, PHP, and Ajax to upload an image and insert it into a database, but I am facing an issue with inserting the image's name in the database. The result in the database shows "C:fakepathLighthouse.jpg", but I only want to insert ...

How to implement a delay for submenu dropdown in Bootstrap 3

I am trying to implement a delay on a submenu that I have created, rather than the entire bootstrap3 dropdown menu. The goal is to allow users to easily move their cursor to the submenu without it closing unexpectedly. Although the jquery code should still ...

Carousel with Bootstrap: Heading positioned over the content

My goal is to have the caption of Bootstrap Carousel displayed above the content, but I'm encountering issues with it. When clicking on the chevrons, you may notice the < Item 1 > bouncing... (This behavior is a bug, and logically speaking, I ex ...

Guide on placing a featured image in a div element within genesis theme

I'm encountering a minor issue when trying to work with the genesis framework. My objective is to have each post's featured image inside a div with the same class. I aim to be able to set a background image for the outer div so that when a user h ...

Is there a reason why using margin:auto doesn't work for vertically centering items?

I'm having trouble understanding how margin auto functions. In the scenario I'm dealing with, I have a parent div and a child div nested within it, both with specified widths and heights. I attempted to use margin auto in order to center the inne ...

Having trouble with clearing floats in Twitter Bootstrap CSS - any help appreciated!

Today, in an effort to learn CSS and apply it practically by creating simple themes, I decided to delve into the proper ways of clearing floats in CSS. My research led me to explore how Twitter handles this issue, so I dug into Bootstrap's CSS file an ...

Exclusive Checkbox Update

My code is causing me some trouble. When I click the update button, I want to only display the checked fields from the checkboxes on the redirected page. Below, I will share the code from the initial page, followed by the code from the second page where t ...

Steps to incorporate a nested table in HTML with merged columns

A number of university groups have utilized my HTML table applications. Each university may have various groups, with each group currently consisting of only one student. The HTML code I am working with includes AngularJS as shown below: <table&g ...

How about this: "Leveraging the power of #IEroot to achieve a

Has anyone successfully implemented the IE CSS hack #IEroot? I came across it while reading this informative article, but unfortunately, it doesn't seem to be working for me. I'm currently trying to resolve an inline styling bug in order to achi ...

What could be the reason for the empty space between the upper and middle cuts?

I am working on these two web pages: http://slacklog.heroku.com/ http://slacklog.heroku.com/signup/date As you can see, there is a gap between the top and middle images as well as the bottom and middle images on both pages. Below is my HTML code: <d ...

What is the best way to pass a bind value in an Angular function controller?

I am new to working with AngularJS and I'm trying to pass a model bind value into a function declared in the controller. However, when I try to access that value from the controller, it returns undefined. Here is the code snippet: HTML: <div> ...

Tips on avoiding HTML escaping in Razor (standalone)

I am working with a Model that has a property called Content which stores HTML as a string. var model = new { Content = ... } Razor.Parse(templateBody, model) I am trying to figure out how to render this HTML string using standalone Razor. I attempted: ...

What is the best way to add pre-filled text to a text area within a form submission?

<form action='newPagePHP.php' method='post'> <p><input type='text' name='Name' placeholder='Full Name' /></p> <p><input type='text' name='PhoneNumber& ...

Using div tags as interactive buttons, as well as incorporating dynamic functionality such as delete and report spam options

In this practice test scenario, the objective is to log in to Gmail, click on all checkboxes in a dynamic web table, and delete the mails. Below is the code that I have created for this task. The issue arises when checking if the delete button is visible. ...