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

What is the best way to arrange cards in a specific order with breaks in between each flip

I am currently working on creating flip cards using HTML and CSS. I have successfully implemented the hover effect which flips the cards when hovered over. Now, my goal is to make the cards automatically flip one after the other at specific time intervals ...

Deactivate a function within Bootstrap JavaScript

Is there a way to temporarily disable a function within Bootstrap after a click event? <a href="#"><span class="glyphicon glyphicon-triangle-right" id="btn04" onclick="myfun();"></span></a> Additionally, I am looking for a soluti ...

Am I utilizing the htmlspecialchars function properly?

My main objective is to protect the user from malicious code and prevent XSS attacks. I have implemented a series of checks to filter the user's input before storing it in the database. The user input is stored in the $post variable. $post = htmlspec ...

`inline-block elements are centered when displayed in Firefox`

I have a question regarding formatting h2 and h3 elements to display as inline-block. Below is the code snippet: <div id="content" class="content-width"> <h2 class="headline"> My Headline </h2> <h3 class="subheadline"> My S ...

Using JSON parsing to extract an integer as the key in JavaScript

I've been searching for almost 2 hours now, but I can't seem to find anyone using an integer as the key in their json object. The structure of my json object is as follows: {"342227492064425": {"added":"2020-10-04T23: ...

Conflicting behavior between jQuery focus and blur functions and retrieving the 'variable' parameter via the $_GET method

There is a simple focus/blur functionality here. The default value shown in the 'Name of Venue' input field changes when the user clicks on it (focus) and then clicks away(blur). If there is no text entered, the default value reappears. Input fi ...

Design a personalized button with a hand-shaped image using CSS

I am aiming to design a unique custom button that resembles the shape of a hand featured in this image, with the intention of adding some functionality to it later. My attempts so far have involved using an SVG path created in GIMP within a button, but all ...

When clicking on the side-bar, it does not respond as expected

My website has a menu layout that features a logo on the left and an icon for the menu on the right side. When the icon is clicked, the menu slides in from the right side of the window, and when clicked again, it slides out. However, I am facing two issues ...

Resizing columns in HTML table remains effective even after the page is refreshed

I've created HTML pages with tables that have multiple columns, but I'm experiencing an issue. The columns are not resizable until I refresh the page. Could someone assist me in fixing this problem and explaining why it's happening? ...

Problem with Agile Carousel and thumbnail component

I have incorporated the Agile Carousel plugin into my website to create multiple slideshows. The first slideshow at the top is working fine, but I am experiencing an issue with the carousel in the "About" section at the bottom. While I have successfully se ...

The use of a Bootstrap row is leading to incorrect dimensions for FullPageJS

Within the body tag, I have included the following code snippet: <div id="container"> <div class="section profile"> <div class="row"> <div class="col-sm-6"> A </div> ...

Adjust the size of the text within the div element as needed

Here is my code on jsFiddle where I am dynamically changing the text font to fit in the div. Everything is working perfectly fine. Currently, the text is within a span element, but I would like to remove the span and have the same functionality. However, w ...

MailChimp consistently applies the "color: inherit !important" style to links, causing them to appear blue when viewed in Outlook

No matter how hard I try, MailChimp insists on styling any <a> tag with an href attribute to have color: inherit !important; text-decoration: inherit !important. This is causing my links to appear blue and underlined in Outlook, which is quite frustr ...

What is the process for adding the multiple attribute to a select dropdown in a form?

I implemented the selectric plugin for dropdowns on my website. I included the multiple attribute in the select tag, but it doesn't seem to be functioning properly based on what is shown on the index page of the selectric plugin located at: This is t ...

JQuery Challenge: Solving Dynamic Modal Issues

I'm in the process of creating a webpage that features multiple divs, each with its own unique image and title. Within each div, there's a button that, when clicked, should grab the specific image and title and display them in a modal. However, I ...

The alignment of Div elements is off in IE8

I have been struggling to align two divs side by side without any empty spaces on IE 8. Everything looks perfect on Chrome Version 35.0.1916.153 m and Firefox 30.0, but IE 8 seems to be causing some issues. All I want is a main div with two child divs ali ...

What is the reason that setting the margin of 0 on the body does not eliminate the margin on an h1 element?

I believe the body element should apply styles to all elements contained within it. In this scenario, I am attempting to give my h1 element a margin of 0. body { font: 15px/1.5 Arial, Helvetica, sans-serif; padding: 0; margin: 0; background ...

Error encountered while building Dart WebUI

I am encountering issues when attempting to integrate web_ui into my existing Dart application. I have not yet incorporated any of the webui-specific code into my HTML file, I am simply trying to build. I updated the pubspec.yaml file with the web_ui pac ...

Ways to retrieve the chosen option in a dropdown list without specifying the dropdown's name, id,

Custom dropdown, Model-View-Controller Code @foreach (var attribute in Model) { string controlId = string.Format("product_attribute_{0}_{1}_{2}", attribute.ProductId, attribute.ProductAttributeId, attribute.Id); @switch (attribute.AttributeControl ...

"jQuery's .each() method is only iterating through the last element in

I am encountering an issue with this function not operating correctly... only the last Element shows the box. NOTES: <aside> is set to position: fixed; and I understand that this is not the "correct" use of <article> tags, but it helps me dist ...