Child elements should adjust their height based on the root parent container by utilizing flex properties, even if there is a potential top offset present

I am attempting to nest multiple "blocks" within each other, utilizing flex for this specific case. The structure I am aiming for is as follows:

  • Parent (highlighted in blue in JSFIDDLE)
    • Container: margin-top: 32px, (highlighted in red)
      • Row 1: colored orange (150 px height, width: 100%)
      • Row 2: colored violet (with width: 100%, and taking up the remaining height)

This layout is demonstrated in the code sample provided:

Example 1 on jsfiddle -- issue present

I desire for all embedded divs to either adhere to the specified height (such as 150px for the orange row) or occupy the remaining available height within their parent container. However, the jsfiddle demonstrates that this setup is not functioning as intended: row 2 does not have the required height.

I am aware that it only takes the full height when the parent's height is set to 100%. If I implement this on the container, due to the container's margin-top being set to 32px, both the container and row 2 assume the height of the parent, protruding beyond the parent's bottom boundary. This issue can be observed in this version of the jsfiddle:

Example 2 on jsfiddle -- still not functioning with height:100% on container

How can I resolve this? What would be the correct approach in this scenario?

Answer №1

Two important things to note here are removing the height: 100% property from .board_divider_bottom and replacing it with flex-grow: 1, which will allow it to take up the available space.

html, body {
  margin: 0;
}

.parent {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  box-sizing: border-box;
  border: 3px solid blue;
}

.board_container {
  display: flex;
  flex-direction: column;
  border: 2px solid red;
  box-sizing: border-box;
  width: 100%;
  margin-top: 32px;
  height: calc(100% - 32px);          /*  changed, reduce with margin-top  */
}

.board_divider_top {
  border: 2px solid orange;
  box-sizing: border-box;
  width: 100%;
  height: 150px;
}

.board_divider_bottom {
  flex-grow: 1;                     /*  added, fill remaining space  */
  border: 2px solid violet;
  box-sizing: border-box;
  width: 100%;
}
<div class="parent">
  <div class="board_container">
    <div class="board_divider_top"></div>
    <div class="board_divider_bottom">I want this to take all the remaining height</div>
  </div>
</div>
<!-- parent -->

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 create a page that moves on release using HTML/CSS?

I'm working on a website where I want to make an interactive feature. When users click on an image on the left side of the page, I want the page to move backward. Similarly, when they click on an image on the right side, I want the page to move forwar ...

Some Android devices experience unusual behavior with inlined icon links

Check out this small jsFiddle: <html> <head> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"> <style> .navButton { font-size:30px; height:43px; ...

I'm keen on creating a marquee animation using Tailwind and Next.js

I'm working on creating a marquee animation using Tailwind CSS and Next.js, and I've made some progress. However, the issue I'm facing is that the items are not displaying one after the other smoothly; there seems to be a delay when the seco ...

Iterate through the MongoDB database array by using the @foreach loop in an Express application

Hey there, I've got a database that I can view using HTML and Express layout. I believe @each and {{}} are JavaScript syntax. {{posts[0].detail}} <----------------This is working However, I'm looking to display all the contents of each pos ...

Utilize PHP and a dynamic web framework like Spry to showcase data from two MySQL tables in an

My goal is to effectively showcase data from two separate tables in mysql using PHP and spry. Initially, I considered implementing spry tabbed panels, but I am uncertain of its feasibility. Essentially, I envision the tab names being populated from one tab ...

The issue with rendering CSS styles from <style></style> tags in Visual Studio while debugging in VB is causing a problem

<style> #ctrtable{width:600px;margin-left:auto;margin-right:auto;} </style> For the purpose of centering a table on the viewport, the above style block is added to the web page.   While using Visual Studio 2013 Express in debug mode, t ...

The text-overflow property with ellipsis functionality appears to be malfunctioning in Internet Explorer when applied to input elements, yet functions

Having an issue with the MS IE (version 11) when using the text-overflow: ellipsis; property for an input field. It appears that this specific property is not working properly in this browser. If anyone has any suggestions or solutions, it would be greatl ...

The spinning wheel goes round and round while executing the location.reload() function

Currently, I am performing actions in my JavaScript function and then triggering a page refresh using location.reload(); I'm wondering if there is a straightforward method with jQuery to display a spinning wheel from the moment the page initiates the ...

How do I make a div's height equal to 100% of the height of its parent

I am trying to make a button stick to the bottom of a card no matter how much content is on it. I used the Bootstrap class mt-auto, which worked well. However, when I set the height of all divs to 100%, the row in the card body overflows from the lg breakp ...

Drop-down menu in a table cell overflowing with lengthy text, causing the table to collapse

I'm facing an issue with my table where I have inputs and selects inside <td>. The problem arises when the text in the options of the <select> element is too long, causing the <td> to expand and disrupt the overall layout of the tabl ...

Navigate to a PDF file from an HTML5 application using PhoneGap by accessing an external link

Can anyone help me figure out what I'm doing wrong here? Essentially, I am trying to open a PDF in Google Viewer: <a id="pdflink" href="https://docs.google.com/viewer?url=http://pdpdpd.pdf" target="_blank">Pdf</a> When it opens in the vi ...

Activate the JavaScript loader while data is being fetched

I'm currently working on incorporating a loader into my website that should remain visible throughout the entire loading process. For example, if the load time is around 6.8 seconds (with 6.3 seconds of waiting and 0.4 seconds of downloading), I want ...

A unique technique for creating a stunning visual effect with images using

Can anyone help me with this issue: Check out this animated GIF The images in my project are overlapping when scrolling! How can I achieve a similar effect for my images? Is there a tutorial or plugin available for this? Here is the current code sn ...

Ways to eliminate the vertical scroll feature in a kendo chart

I am encountering an issue while loading a kendo chart inside grid-stack.js where I am unable to resize the height properly. Whenever I decrease the height, a vertical scroll appears which should not happen. I have tried setting the height to auto and refr ...

Troubleshooting Issue: ASP.NET UpdatePanel Not Responding to jQuery

I am having difficulties invoking jQuery functions within an "asp:UpdatePanel". Despite the code provided below, my attempts to add a class to the div element ".popup-body" are unsuccessful. Interestingly, the "alert();" function works without any issues. ...

HTML: What is the best way to position one <a> element underneath another <a> element?

My website contains a <div> that serves as a link to address1. Adjacent to this div is a drop-down menu with a link to address2. Strangely, the second link seems to override the first one, making Link1 non-clickable. https://i.sstatic.net/eBAEM.png ...

Scrolls down to the bottom of the Material-UI popover menu

Trying to incorporate a material-ui popover with a scrollable menu <Popover {...popoverProps}> <Menu className="ui-dropdown-menu">{items}</Menu> </Popover> Custom styles applied: popoverStyle: { height ...

Tips for adding a class to the output of a jQuery ajax request?

I've been searching for a solution to this issue without any luck. Below is the code I have: jQuery("#categories_parent_id").change(function() { id = jQuery("#categories_parent_id").val(); jQuery.ajax({ type: ...

Is there a way I can decrease the width of the input field on the left side?

body { background: white; font-family: 'Ubuntu', sans-serif; } .cas { display: inline-block; border: 2px solid #0C8346; background: #0C8346; font-weight: 300; font-size: 20px; padding: 5px; width: 200px; text-align: center; ...

What is the best way to vertically center all content, including borders, within the columns?

To view the codepen for reference, please click on the following link: http://codepen.io/rezasan/pen/apvMOR I am attempting to align all the content within the columns (Date, Title, and Button, including the separator) vertically. I have tried using displ ...