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

Any tips on how to export the index file from Firebase after switching from using require statements to import statements?

As I transition from using requires to importing only in my code, I encountered a challenge when adding "type": "module". After resolving several issues, one problem remains. Initially, I had exports.expressApi = functions.https.onReque ...

Guide to displaying Content components beneath the AppBar in Material-UI version 5

My current project involves creating an app bar with the app bar component from MUI v5. However, I have encountered a problem where another content component overlaps with the app bar. In MUI v4, I was able to resolve this issue using theme.mixins.toolba ...

Retrieve content from inner tag using Selenium

Check out this snippet of HTML code: <label class="abc"> <span class="bcd">Text1</span> Text2 </label> Is there a more efficient method to extract only Text2 using a selenium script? I currently extract Text2 by accessing the ...

Dynamic TextField sizing

I am currently facing an issue while displaying data on a webpage using TextField component from @material-ui. Each record of data has varying lengths, making most values appear unattractive (occupying only 10% of the textfield width). Even though I am ut ...

What is the best way to block users from directly accessing my React app's page by typing in the URL, and instead redirect them to the login page?

I am currently in the process of developing a react-based application. This app serves as a platform for managing storage data, and my main focus right now is implementing security measures to prevent users from directly accessing certain routes by simply ...

Caution: The prop `className` did not align when utilizing @font-face alongside styled-components in NextJS

https://i.stack.imgur.com/ZLtue.png Greetings, everyone. I've recently crafted a GlobalStyle in which I'm defining my fonts using font-face. import { createGlobalStyle } from 'styled-components'; export const GlobalStyle = createGloba ...

Is there a way to display the JavaScript widget exclusively on the homepage or main page

How can I limit the display of a Twitter widget on my blog page to only show on the main page and hide it when visitors navigate to sub-pages or individual blog entries? ...

Tips for positioning and resizing an HTML slide

Once again I am faced with my favorite HTML/CSS dilemma and am feeling frustrated. Seeking advice from the experts on SO this time around to solve my problem. Here it is... Imagine you want to create a slideshow in a browser. The requirements are simple: ...

React BrowserRouter causing issues with "invalid hook calls"

Just starting out with React and I am working on setting up paths using BrowserRouter, Route, and Routes in my code. import React from "react" import "./App.css"; import { BrowserRouter as Router, Route, Routes } from 'react-router ...

An absence of data causes the function to malfunction

One of the components in my application is responsible for rendering select elements on the screen, allowing users to dynamically order data. The initial state is as follows: state = { sortBy: [ { author: 'asc' } ] }; In this s ...

"CSS - Struggling with header alignment in a table display element - need help with positioning

During my HTML layout creation process, I encountered a peculiar positioning issue that proved difficult to resolve. Consider the following HTML structure: <div class="outer-wrap"> <div class="header"> I am a Header </div> <div c ...

What is the rationale behind using both useMemo and createSelector in this code?

This example from the React-Redux documentation showcases how a selector can be utilized in multiple component instances while depending on the component's props. import React, { useMemo } from 'react' import { useSelector } from 'reac ...

Tips for hiding a soft keyboard with jQuery

Here is a text button: <input type="text" class="field" id="something" name="something" placeholder="text" autofocus /> I want to prevent the android soft keyboard from popping up and keep the focus on this field. I only want to do this for this ...

How can I position a vertically centered image to the left of a block paragraph using inline styles?

I am faced with the challenge of aligning a 100x100 vertically-centered image to the left of a text block that extends beyond its height. It is important that the text does not wrap underneath the image but stays in a single block on the right side. I must ...

Align images to the bottom of the gallery only if their heights differ

I'm currently working on an image gallery project and have encountered a problem. When all the image labels are of the same length, they align perfectly. However, if the label is longer than one line, it causes the image to move up instead of creating ...

show information from json onto an html page with the help of jquery

I'm looking to showcase buttons from a JSON file within a simple block. Here's the JSON data for movies: { "movies": [ { "title": "Mena", "movieid": "1", ...

Creating a basic jQuery button to switch an element's background color is a breeze

I'm new to this, so I hope this is a straightforward question. I'd like to use the bgtoggle class as a button to switch the background color of the metro class. I know I'm doing something wrong because it's not working as expected. Any ...

Guide on creating a square within an element using JavaScript

After conducting thorough research, I find myself unsure of the best course of action. My situation involves a Kendo Grid (table) with 3 rows and 3 columns. Initially, the table displays only the first column, populated upon the page's initial load. S ...

Changing the type of value in a React select onChange

<Select options={options} value={selectedBusinessList} isMulti placeholder="Select Business" onChange={(value: any) => setSelectedBusinessList(value)} onInputChange={query => { if ...

Rendering ReactJs on the server side versus the client side

I'm utilizing react-router for managing both server-side rendering and client-side rendering in my React application. However, I've noticed that the entry point of my app contains the following code: Router.run(routes, Router.HistoryLocat ...