How to Set the Height of an Outer Div to Always Be Equal to a Particular Inner Div

How can you set the height of an outer div to always be equal to a particular inner div?

An idea is to make some of the content out of the flow using absolute position so it won't have any impact on the height and then use flexbox for the main layout:

.container {  display: flex;  border: 1px solid;  padding: 5px;  box-sizing: border-box;}
.container>div { flex: 1; margin: 5px; border: 1px solid; box-sizing: border-box;}div.inner { border-color:red;}
.inner-alt { position: relative; overflow: auto;}
.inner-alt>div { position: absolute; top: 0; right: 0; left: 0; bottom: 0;}
<div class="container">  <div class="inner">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec dignissim ipsum orci, </div>  <div class="inner-alt">    <div>ut volutpat ligula finibus a. Maecenas ut pharetra ante. Nunc volutpat est eu odio vestibulum, eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate. Vivamus ac tincidu</div>  </div>  <div class="inner-alt">    <div>eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate. Vivamus ac tincidu</div>  </div></div><div class="container">  <div class="inner">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec dignissim ipsum orci, io vestibulum, eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate</div>  <div class="inner-alt">    <div>ut volutpat ligula finibus a. Maecenas ut pharetra ante. Nunc volutpat est eu odio vestibulum, eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate. Vivamus ac tincidu</div>  </div>  <div class="inner-alt">    <div>eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate. Vivamus ac tincidu</div>  </div></div><div class="container">  <div class="inner">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec dignissim ipsum orci, io vestibulum, eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate , eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate</div>  <div class="inner-alt">    <div>ut volutpat ligula finibus a. Maecenas ut pharetra ante. Nunc volutpat est eu odio vestibulum, eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate. Vivamus ac tincidu</div>  </div>  <div class="inner-alt">    <div>eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate. Vivamus ac tincidu</div>  </div></div>

Make Height of One Div Fill Remaining Space, But Not Go Past Another Div

Aside element is scrollable and height matches the one of Main:

* {
box-sizing: border-box;
margin: 0;
}

#app {
display: flex;
}

#aside {
position: relative;
overflow: auto;
width: 200px;
background: #ffc;
}

#main {
flex: 1;
background: #fcc;
}

.full {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div id="app">

<aside id="aside">
<div class="full">ASIDE<br>Lorem...</div>
</aside>

<main id="main">
MAIN
</main>

</div>

Make the width of outer div to fit inner divs automatically

Your outer div is a block-level element. You need to make it an inline-level element. Inline elements automatically take the size of the content it contains. In terms of the question you've asked, just setting :

display: inline-block

on your outer div will do the trick. See the code snippet below for the demo :

  body {      font-size: 0;    }    #outer {      border: 1px solid black;        display: inline-block;    }    .inner {      font-size: 12px;      display: inline-block;      border: 1px solid red;    }
<div id='outer'>
<div class='inner'> text1 </div> <div class='inner'> text2 </div>
</div>

Why isn't width and height of inner div's equal to outer div?

Your divs have borders. Using the default box sizing model, the border is not included into the width, so each div is actually wider than 20px. Either set border box model to your div CSS:

.grid div{
/* ... */
box-sizing: border-box;
}

Or make sure you are not forgetting the border; e.g.:

.grid div {
width: 16px;
height: 16px;
background-color: blue;
border-style: solid;
border-width: 2px;
}

(2 + 16 + 2 = 20)

Divs in row - one's height set by content, the other's set to match

According to this you should remove your height property and set align-items:stretch on your wrapper (but it happens to be the default, so just remove the height and it will work).

.wrapper {
display: flex;
flex-direction: row;
border: 1px solid black;
align-items: stretch;
}

.image {
background-image: url(https://source.unsplash.com/random/600x600);
width: 300px;
border: 1px solid yellow;
/* Needed to make the image show */
/* height: 200px; */
}

.content {
width: 100%;
padding: 20px; /* Height is sum of text height and padding */
border: 1px solid blue;
}
<div class="wrapper">
<div class="image">
</div>
<div class="content">
<p>This could go on and on and on, and should determine the height of the set of divs, sitting next to each other. I do NOT want to program a set height, because that could clip long text.</p>
</div>
</div>

How to make a static div the same height as a dynamic div?

A nice solution to this problem is the grid layout. For example, in the following snippet, elements are inserted into the first child of parent, and the second div's height changes to match.

<div class="parent-container">
<div id="left">
left
</div>

<div id="content">
<!-- for example, fill it with a list -->
<!-- this data can be anything though -->
<ul></ul>
</div>

<div id="right">
right
</div>
</div>

<style>
.parent-container {
background-color: rebeccapurple;
display: grid;

/* adjust these columns to change widths */
grid-template-columns: 10rem auto 10rem;
}

/* just to make things look a bit nicer */
#left, #right {
padding: 1rem;
background-color: paleturquoise;
}
</style>


<!-- script to simulate dynamic data -->
<script>
setInterval(() => {
const parent = document.querySelector('ul');
const entry = document.createElement('li');
entry.innerText = 'data';
parent.appendChild(entry);
}, 1000);
</script>

How to make inner divs of two unrelated outer divs equal in height?

With jQuery: https://codepen.io/anon/pen/gGVGwj

// Create two arrays for the left element heights and the right element heights
var leftArray = [];
var rightArray = [];

// Get the height of each .line element and save it to the appropriate array
$('#left .line').each(function(i) {
leftArray.push($(this).height());
});
$('#right .line').each(function(i) {
rightArray.push($(this).height());
});

// For each element on the left, if its height is smaller than the
// corresponding element on the right, set its height via css to
// equal the right-side element, and vice versa. Nothing to do if
// the heights are equal.
for (var i = 0; i < leftArray.length; i++) {
if (leftArray[i] < rightArray[i]) {
$('#left .line').eq(i).css('height', rightArray[i])
} else if (leftArray[i] > rightArray[i]) {
$('#right .line').eq(i).css('height', leftArray[i])
}
}

I think this will be fairly fast, at least faster than setting a height on every single element. There's absolutely some work to do for responsive or mobile environments, so this isn't an ideal scenario by any means, but hopefully this does what you want.

Html element controlling height of its parent or sibling

There is no way to affect a parent in css (there is a sibling selector, although I don't see how would it be useful in this case), but this would be a way to do what you want, letting the .right div to determine the height of the container and the .left one adapting to it:

body {
margin: 0;
overflow: hidden;
}
.container {
position: relative;
}
.container > * {
padding: .2rem;
}
.container p {
margin: 0;
}
.left {
background-color: cornflowerblue;
position: absolute;
overflow-y: auto;
top: 0;
left: 0;
bottom: 0;
right: 50%;
}
.right {
position: relative;
background-color: aquamarine;
left: 50%;
}
<div class="container">
<div class="left">
Potentially<br>long<br>content<br>that<br>might<br>need<br>a<br>scrollbar<br>1<br>2<br>3<br>4<br>5 Fusce pulvinar sit amet massa in vulputate. Cras eros ligula, condimentum non lacus nec, maximus lacinia orci. Potentially<br>long<br>content<br>that<br>might<br>need<br>a<br>scrollbar<br>1<br>2<br>3<br>4<br>5 Fusce pulvinar sit amet massa in vulputate. Cras eros ligula, condimentum non lacus nec, maximus lacinia orci. Potentially<br>long<br>content<br>that<br>might<br>need<br>a<br>scrollbar<br>1<br>2<br>3<br>4<br>5 Fusce pulvinar sit amet massa in vulputate. Cras eros ligula, condimentum non lacus nec, maximus lacinia orci. Potentially<br>long<br>content<br>that<br>might<br>need<br>a<br>scrollbar<br>1<br>2<br>3<br>4<br>5 Fusce pulvinar sit amet massa in vulputate. Cras eros ligula, condimentum non lacus nec, maximus lacinia orci.
</div>
<div class="right">
<p>The text that<br>does not change<br>and must always<br>be displayed<br>in its entirety<br>without scrollbar.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam euismod nibh ex, sit amet vulputate ipsum luctus non.</p>
</div>
</div>


Related Topics



Leave a reply



Submit