Ensure that a div element expands to 100% of its parent's height when its child's height is less than 100%

I am struggling with a flex div that contains 2 elements. I want both elements to take up the full parent height while their content remains at auto height, but I keep getting the inner content height.

Here is my code:

<html lang="en">
<head>
<meta charset="utf-8>
<title></title>
<style>
    html, body {
        height: 100%;
        margin: 0px;
    }
  .parent {
    background: gray;
    display:flex;
    align-items: center;
  }
  .child {
    height: 100%;
    background: blue;
    border: 1px solid white;
  }
</style>
</head>
<body>
  <div class="parent"><br>
    <div class="child"><br>
      <div><br>
        <div>abc</div><br>
        <div>abc</div><br>
    </div><br>
    </div><br>
     <div class="child"><br>
      <div>abc</div><br>
    </div><br>
  </div><br>
</body><br>
</html><br>

The second "child" div should also stretch to the parent height, while keeping its inner content at auto height. How can I achieve this?

Answer №1

Consider using align-self: stretch in place of height: 100% for the elements with the child class.

Answer №2

Make sure to maintain the default alignment and remove height: 100% so that the children are automatically stretched.

Then, apply display: flex to the children with the desired alignment.

   html, body {
    height: 100%;
    margin: 0px;
  }
  
  .parent {
    background: gray;
    display:flex;
  }
  
  .child {
    background: blue;
    border: 1px solid white;
    display: flex;
    align-items:center;
  }
  
   <div class="parent">
    <div class="child">
      <div>
        <div>abc</div>
        <div>abc</div>
    </div>
    </div>
     <div class="child">
      <div>abc</div>
    </div>
  </div>

Answer №3

To ensure proper formatting, set a specific height for the .parent div, such as: height: 300px; or height: 100%;

html,
body {
  height: 100%;
  margin: 0px;
}
.parent {
  background: gray;
  display: flex;
  align-items: center;
  height: 200px;
}
.child {
  height: 100%;
  background: blue;
  border: 1px solid white;
}
<body>
  <div class="parent">
    <div class="child">
      <div>
        <div>abc</div>
        <div>abc</div>
      </div>
    </div>
    <div class="child">
      <div>abc</div>
    </div>
  </div>
</body>

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

The CSS does not display properly on iPad or mobile devices

Recently, I had the opportunity to design a website for a local music school. This project was a great learning experience that has shown me the importance of continuous improvement and practice. Check out the site here Initially, I wanted to make the w ...

Pressing the tab key while using Angular will halt any additional processes from running

I recently integrated a search bar into my Angular application. When I enter text in the input field, a customized dropdown menu appears below it. However, when I press the tab key, I expect the dropdown to close and for the focus to shift to the next inpu ...

Using Python to manipulate HTML for printing purposes

As I develop an application in Python, one of the features requires printing PDF files using a Xerox printer. I am currently considering two options: First option: figuring out a way to communicate with the printer driver and send commands to the printer ...

Is there a way to rigorously validate my HTML, CSS, and JavaScript files against specific standards?

Can modern browsers suppress errors in HTML, CSS, and JS sources? Is there a method to uncover all mistakes, no matter how small they may be? ...

Modifying HTML Email to Achieve Consistent Rendering Across Various Email Platforms

While creating an HTML based email for our subscribers, I noticed that it renders differently in various email clients. The main issue arises in Outlook 2003 where the main picture is offset to the left and the grid boxes vary in height depending on the co ...

Clicking will cause my background content to blur

Is there a way to implement a menu button that, when clicked, reveals a menu with blurred content in the background? And when clicked again, the content returns to normal? Here's the current HTML structure: <div class="menu"> <div class=" ...

Customizing CSS float labels for individual inputs

For my project, I've implemented CSS code to create a floating label effect for form inputs. If you need the original code reference, it can be accessed here. However, since there are multiple examples in the original codebase, I have extracted and m ...

What is the best way to replicate tags in selenium when an element has multiple class names?

Extracting information from a webpage where a significant amount of text was concealed behind the "see more" tab. Using selenium to click on all such buttons and then extracting data with beautifulsoup. However, some of these buttons have additional space ...

Unable to Pause Video with Javascript

I have a project with a video that plays in a continuous loop. Below is the HTML code for the video tag: <video playsinline autoplay muted loop id="myVid"> <source src="River.mp4" type="video/mp4"> </video> My goal is to make the vi ...

Calculating available balance by subtracting the entered amount from the existing balance in Python Django

In the user profile model within my Django project, there are fields for available balance and current balance. I am looking to deduct an input amount from the current balance in order to display the updated available balance on the user's web page. ...

Best placement for transition effects in a dropdown menu animation

<div class="dropdown"> <button class="dropbtn">ALAT</button> <div class="dropdown-content"> <a href="index2.php"><img src="home.jpg" class="home" width="7 ...

When the mouse hovers over the text inside a div, the link does not work in Internet Explorer

Here is some code for demonstration. I'm facing an issue where the link spans over the entire div, which works perfectly in Firefox. However, in IE, when I hover over the text, the cursor changes to a text cursor and the link doesn't work there. ...

Issue with Vue v-for not updating data model variable

I am attempting to render a page with dynamic properties using the following code: <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="root"> <div v-for="current in 2&quo ...

What is the best way to display two radio buttons side by side in HTML?

Looking at my HTML form in this JSFiddle link, you'll see that when the PROCESS button is clicked, a form with two radio buttons appears. Currently, they are displayed vertically, with the female radio button appearing below the male radio button. I& ...

Verifying completed fields before submitting

I'm in the process of designing a web form for users to complete. I want all fields to be filled out before they can click on the submit button. The submit button will remain disabled until the required fields are completed. However, even after settin ...

Guide to incorporating flash into a form similar to the method used on http://www.mediafire.com

I'm looking to integrate a flash upload feature into my index file, but I'm not sure how to do it. Here is the link to the flash uploader: . I want it to work similar to how uploading works on when you click on UPLOAD. Thank you for any assistan ...

What is the best way to toggle and slide two forms within a single page?

In this scenario, the goal is to display multiple forms on a single page, like a Login Form and Reset Password Form shown below. One form is initially hidden until a link is clicked, triggering an effect. The current setup includes a .toggle() effect that ...

What is the best way to retrieve a table from an HTML page and convert it into a data frame using XML and Rcurl in R programming?

Can someone help me convert a table on the Forbes website into a data.frame? Click here to access the table ...

Enhancing 2D video viewing with Threejs interactivity

I want to create an interactive 2D video using three.js and maintain the aspect ratio of the video when resizing the browser window. Here is the code I am currently using: var camera, scene, renderer; var texture_placeholder, distance = 500; init() ...

Tips on moving down the `<li><span>` elements with the jQuery `.slidedown()` method

I'm struggling to articulate my question clearly, so please bear with me. Essentially, I have a jquery script that reveals content within a div nested in an li. When I click on the header, the content drops down and visually pushes the next header do ...