Unable to apply the flex-grow property to a column flex box that has a specified minimum height

I am currently working on designing a website layout that harnesses the power of flexbox to fill the entire page with content, even when the content does not fully occupy the length of the page.

My approach involves creating a large flexbox container with flex-direction: column;. However, I encountered an issue when setting the last child to flex-grow: 1; and the rest of the children to flex-grow: unset;, causing the flex-grow property to no longer affect the children's content when using min-height.

You can view an example of this on JSFiddle here.

The .should-strech class is meant to represent the content within one of the children that should fill the entire height. When I apply height: 100px, everything behaves as expected (refer to the first box where the yellow area fills the space).

However, once I switch to using min-height: 100px;, the yellow area no longer fills the entire height.

I attempted to place another flexbox inside the child element, but this method is not ideal for my scenario since this component is intended to be generic and may have an unknown or dynamic number of "contents" in each child.


 <div class="container height">
  <div class="child">
    1
  </div>
  <div class="child">
    <div class="should-strech">
      2
    </div>
  </div>
</div>
<br/>
<div class="container min-height">
  <div class="child">
    1
  </div>
  <div class="child">
    <div class="should-strech">
      2
    </div>
  </div>
</div>
.min-height {
   min-height: 100px;
}

.height {
   height: 100px;
}

.container {
  width: 100px;
  display: flex;
  flex-direction: column;
  background-color: aqua;
}

.child {
  background-color: red;
}

.child:last-child {
  background-color: green;
  flex-grow: 1;
}

.should-strech {
  background-color: yellow;
  height: 100%;
}

Answer №1

Consider using a nested flexbox structure as shown below:

.min-height {
   min-height: 100px;
}

.height {
   height: 100px;
}

.container {
  width: 100px;
  display: flex;
  flex-direction: column;
  background-color: aqua;
}

.child {
  background-color: red;
  display: flex;
  flex-direction: column;
}

.child:last-child {
  background-color: green;
  flex-grow: 1;
}

.should-strech {
  background-color: yellow;
  flex-grow: 1;
}
<div class="container height">
  <div class="child">
    1
  </div>
  <div class="child">
    <div class="should-strech">
      2
    </div>
  </div>
</div>
<br/>
<div class="container min-height">
  <div class="child">
    1
  </div>
  <div class="child">
    <div class="should-strech">
      2
    </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

The collapse component from bootstrap is not visible due to an overlapping button

I am currently working on a responsive calendar featuring full-width buttons for events, accompanied by additional information displayed using the collapse component below. However, I am facing an issue where the button overlaps with other elements, preven ...

Error in Next.js - missing property "children" in type {}

When using react and nextjs, I encountered an issue with my code that resulted in an error regarding the children prop. The error message states "Property children does not exist on type {}". Below is the snippet of code causing the error: import { NextPag ...

JavaScript: Understanding the concept of closure variables

I am currently working on an HTML/JavaScript program that involves running two counters. The issue I am facing is with the reset counter functionality. The 'initCounter' method initializes two counters with a given initial value. Pressing the &a ...

I created a customized version of jQuery tabs, but I am looking for an external link to display the tab content and to style the original navigation

I've been experimenting with modifying a tabs script in jQuery that I came across. It seems like my attempt to enhance it has made things more complex than necessary. While I know creating tabs with jQuery is simple, I wanted to create my own version ...

Ensuring the authenticity of user login credentials

I have a form in my HTML where the user needs to input their name and password. <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> Password: <input type="text" name="password ...

I am looking to sort users based on their chosen names

I have a task where I need to filter users from a list based on the name selected from another field called Select Name. The data associated with the selected name should display only the users related to that data in a field called username. Currently, wh ...

Utilizing Javascript to Extract Data from Twitter Json Files

Can someone provide assistance with parsing JSON feed text retrieved from Twitter? I am looking to access and apply style tags to elements like the link, created date, and other information. Any tips on how I can achieve this task successfully would be g ...

Is it possible to determine the specific type of props being passed from a parent element in TypeScript?

Currently, I am working on a mobile app using TypeScript along with React Native. In order to maintain the scroll position of the previous screen, I have created a variable and used useRef() to manage the scroll functionality. I am facing an issue regardi ...

Retrieving information through a loop in Next.js

My goal is to utilize the data retrieved from one endpoint to make a call to another endpoint, filtered by ID. I intend to fetch both calls using getServerSideProps and then pass the data to a different component. The initial call will result in an array ...

Version 5 of Material UI has a bug where the Grid component does not properly stretch

How can I make the Grid component stretch when one of the Card components contains extra text? You can view the sample code here. Changing the alignItems property to "flex-end" or "center" works, but when using alignItems: "stretch" it does not work. I ...

When clicking on a link in React, initiate the download of a text file

Usually, I can use the following line to initiate the download of files: <a href={require("../path/to/file.pdf")} download="myFile">Download file</a> However, when dealing with plain text files like a .txt file, clicking on ...

Is it possible to apply capital spacing in CSS for OpenType fonts without using font-feature-settings?

Is it possible to adjust capital spacing (cpsp) for an element without impacting any other applied OpenType features? Unfortunately, utilizing the font-feature-settings is not a viable solution. For example, if we use font-feature-settings: 'cpsp&apo ...

Tips for dynamically loading a multitude of icons in Next.js

Is it worth loading these icons dynamically? import { faCheck, faCogs, faCoins, faCouch, faCreditCard, faGhost, faGift, faPuzzlePiece, faQrcode, faTasks, faVideo, faChild, faCubes, } from "@fortawesome/free-solid-svg-icons&qu ...

What is the process for implementing an image as the background instead of a color for each column in a Google chart?

I'm currently working with Google Chart and I have set up a column chart. However, I am looking to display a background image instead of a background color in each column. Is this achievable with Google Chart? If so, how can I accomplish this? If anyo ...

Do we really need to create our own action creators in Redux Toolkit?

As I delve into implementing the redux toolkit in my react projects, I've come up with a structure for writing slices using redux-thunk to handle API requests. import { createSlice } from "@reduxjs/toolkit"; import axios from "axios&quo ...

Is there a way to prevent the contents of my div from overflowing beyond my other div?

Hey there! I'm fairly new to this whole web design thing, only a few months in. Currently, I'm in the process of creating a website for my school. However, I've hit a bit of a snag with different div containers holding various parts of my ...

Update the styling for the second list item within a specified unordered list class instantaneously

Looking to emphasize the second list item (li) within a selected dropdown list with a designated unordered list class of "chosen-results". <div class="chosen-container chosen-container-single select-or-other-select form-select required chosen-proc ...

How to Customize the Style of Material UI's Tooltip

Is there a way to customize the background color and text color of Material UI's Tooltip? I have attempted to do so using the code snippet below without success. import { createMuiTheme } from '@material-ui/core/styles'; export const theme ...

React Material Select: The error is caused by the property 'renderValue' with an expected type, as declared within the 'IntrinsicAttributes & SelectProps' type

Encountering an error with React Material Select, specifically on the Render Value function. How can I resolve this issue while using TypeScript? What should be added to the renderValue line? Error: Type '(selected: Array<number>) => string& ...

How does the use of nodejs, a server-side scripting language, tie into ReactJs or other front-end languages?

Can Node, being a server-side scripting language, be effectively utilized in the development of front-end applications such as npx create-react-app or npx create-nuxt-app? ...