I have a div B that overlays another div A when an action is triggered, such that when A is shown it fully covers B and when B is show it fully covers A.
I would like the height/width of the B div to take up as much space as it needs to show it's content or the height/width of A whichever is larger.
And vice versa I would like the height/width of the A div to take up as much space as it needs to show it's content or the height/width of B whichever is larger.
Use case is a popover that shows some content in a panel then clicking a button slides in another panel over it whose content may have larger dimensions and I want the initial popover size to be the largest width/height of either so it size stays constant and not grow and shrink depending which panel is showing.
.parentToBoth {
position: relative;
max-height: 400px;
margin-top: 50px;
}
.A {
position: absolute;
top: 0;
left: 0;
z-index: 100;
background-color: pink;
}
.B {
position: absolute;
top: 0;
left: 0;
z-inde: 90;
background-color: yellow;
}
function setTopBottom(top, bottom) {
if (top) {
top.setAttribute("style", "z-index: 100");
}
if (bottom) {
bottom.setAttribute("style", "z-index: 50");
}
}
function getDivs() {
let A = document.getElementsByClassName('A');
let B = document.getElementsByClassName('B');
return {
A: A[0],
B: B[0]
}
}
function showA() {
let elements = getDivs();
setTopBottom(elements.A, elements.B);
}
function showB() {
let elements = getDivs();
setTopBottom(elements.B, elements.A);
}
.parentToBoth {
position: relative;
max-height: 400px;
margin-top: 50px;
}
.A {
position: absolute;
top: 0;
left: 0;
z-index: 100;
background-color: pink;
}
.B {
position: absolute;
top: 0;
left: 0;
z-inde: 90;
background-color: yellow;
}
<button class=showA onClick="showA()">
Show A
</button>
<button class=showB onClick="showB()">
Show B
</button>
<div class=parentToBoth>
<div class=A>
this is the A div
<p>
Some more text in A a is wider
</p>
</div>
<div class=B>
this is the B div
<p>
Here Bs content
</p>
<p>
bottom is longer
</p>
</div>
</div>