Make Children Divs the Height of Tallest Child

Make children divs the height of tallest child

One solution is to change the display value from inline-block to table-cell for the descendant div.item elements:

.row {
border: 1px solid red;
display: table;
border-spacing: 20px; /* For controlling spacing between cells... */
}
.item {
background: rgba(0,0,0,0.1);
display: table-cell;
}

Example

How do I set parent div height based on height of tallest child?

Use display:flex in row. if give flex to parent then there is no need to give float property to child.

.row {  background: yellow;  display: flex;}
.column1 {
width: 50%;}.column2 {
width: 50%; display: flex; align-items: center; justify-content: center; flex-direction: column; background: aqua;}
img { width: 100%;}
<div class="row">  <div class="column1">    <img src="https://hypb.imgix.net/image/2017/12/kith-spongebob-squarepants-teaser-1.jpg?q=75&w=800&fit=max&auto=compress%2Cformat">  </div>  <div class="column2">    <p>This column should match the height of the other column</p>  </div></div>

Parent Div to Keep the Height of Tallest Child

Hope this is what you want:

  1. Use flex display to arrange the child as column display.
  2. Set their opacity as 0 so that the child element still remain in the parent div, this will set the parent height the same as the tallest child.
  3. Created a new class visible to style the visible child, and set default children (which are hidden) width: 0px while visible child will have width: 100%

function show(element) {  $('.parent div').removeClass('visible');  $('.' + element).addClass('visible');}
.parent {  border: 1px dashed #000;  display: flex;}
.child1 { height: 60px; background: red;}
.child2 { height: 80px; background: yellow;}
.child3 { height: 100px; background: orange;}
.parent div { opacity: 0; width: 0px;}
.parent div.visible { opacity: 1; width: 100%;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div class="parent">  <div class="child1 visible">I'm 60px tall and visible; my parent is 100px tall</div>  <div class="child2">I'm 80px tall and hidden until needed</div>  <div class="child3">I'm 100px tall and hidden until needed</div></div>
<br/><button onclick="show('child1')">Child 1</button><button onclick="show('child2')">Child 2</button><button onclick="show('child3')">Child 3</button>

How to make child divs same height as flex parent

The child elements of a display: flex; element will take up the height of the parent by default. So no need for height: 100%; or similar.

div#filterRow {  min-height: 80px;  display: flex;  border: 2px solid red;}
div#filterRow div.selectHolder { border: 2px solid blue; border-right: 0; padding: 10px;}
div#filterRow div.selectHolder div { vertical-align: top;}
div#filterRow div.selectHolder:last-child { border-right: 1px solid lightgray;}
div#filterRow div h1 { margin: 0; margin-bottom: 10px;}
div#filterRow div div { margin-right: 5px;}
div#filterRow div#filters { width: 800px;}
div#filterRow div#highlight { width: 300px;}
div#filterRow div#granularity { width: 400px;}
div.fullwidth { width: 100%;}
<div class="dispTR" id="filterRow">
<div class="selectHolder" id="filters"> <h2>Filters longer to make tall</h2> <p> Yo-ho-ho bilge topmast Spanish Main lugger starboard grog blossom crow's nest hang the jib spirits chase guns ballast. Letter of Marque come about bucko schooner Jolly Roger Chain Shot boom topsail case shot salmagundi marooned piracy. American Main hornswaggle topgallant reef rigging schooner quarterdeck sutler coffer long boat jury mast Davy Jones' Locker. Sloop pressgang capstan black spot cackle fruit Nelsons folly cable main sheet plunder ye gibbet parrel. </p> </div> <div class="selectHolder" id="highlight"> <h2>Highlight</h2> </div> <div class="selectHolder" id="granularity"> <h2>Group</h2> </div> </div>

How to force child div to be 100% of parent div's height without specifying parent's height?

NOTE: This answer is applicable to legacy browsers without support for the Flexbox standard. For a modern approach, see: https://stackoverflow.com/a/23300532/1155721


I suggest you take a look at Equal Height Columns with Cross-Browser CSS and No Hacks.

Basically, doing this with CSS in a browser compatible way is not trivial (but trivial with tables) so find yourself an appropriate pre-packaged solution.

Also, the answer varies on whether you want 100% height or equal height. Usually it's equal height. If it's 100% height the answer is slightly different.

How to make all children in a flex-container to have the same height

To do this you just need to wrap your inner div's in another div and set align-items: center on container element.

.container {  display: flex;  border: 1px solid black;  align-items: center;  width: 500px;  height: 150px;}div > div {  display: flex;}.container div div:not(:first-child) {  background: green;  margin-left: 20px;}.container div div:first-child {  height: 100px;  background: red;  margin-left: 20px;  text-align: center;}
<div class="container">  <div>    <div>      item-1    </div>    <div>      item-2    </div>    <div>      item-3    </div>  </div></div>

React - Dynamically set height of child div to be equal to that of tallest neighbour

In order to solve this problem, I wrap my dynamic div C in the ReactHeight component:

<ReactHeight onHeightReady={this.reportCardHeight}>
<CDiv cardHeight={cardHeight}>
...children
</CDiv>
</ReactHeight>

I pass the cardHeight prop down to it and use it as follows to set the height (using StyledComponents):

const CDiv = styled.div`
height: ${props => props.cardHeight && `${props.cardHeight}px`};
`;

Next, my reportCardHeight function dispatches an action which updates my state:

reportCardHeight = height => {
const { setCardHeight} = this.props;
setCardHeight(height);
};

This is used by my reducer as follows:

const cardHeightReducer = (state = initialState, action) => {
switch (action.type) {
case SET_CARD_HEIGHT:
return {
...state,
cardHeight: Math.max(
state.cardHeight || 0,
action.height
),
};
default:
return state;
}
};

Therefore when the content is loaded into CDiv the greatest height is stored in the state, all the other CDiv in turn use this value from the state in order to update their own height.

make child div the same height as parent div

you can use display instead float:

  • flex

.propertydetailbox2 {  display: flex;  border: 1px black solid;}
.propertydetailtextbox { flex: 1; border: 1px solid #cccccc; border-radius: 8px; margin: 0 auto; margin-right: 1%; padding: 20px; padding-left: 50px; height: auto; text-align: left;}
.propertydetailagentbox { flex: 1; border: 1px solid #cccccc; border-radius: 8px; margin: 0 auto; margin-left: 1%; padding: 20px;}
<div class="propertydetailbox2">  <div class="propertydetailtextbox">1<br>2<br>3<br>4<br> </div>  <div class="propertydetailagentbox">1<br>2</div></div>

How to make all children items the same height in the flexbox, as the one that changes its height?

by making the css property align-items set to stretch
align-items:stretch
, which is the default value of that property and if the flex direction is column just set the width of the child element to 100%, and if the direction is row set height of child to 100%



Related Topics



Leave a reply



Submit