Check out the snippet below.
I am looking to vertically center an element, 'modal', over another div, 'element', regardless of its position or margins. However, I cannot place the 'modal' div inside the 'element' div due to restrictions with the react component I am working on. I am wondering if there is a way to position one div relative to another without nesting them. Should I use javascript to detect the position of the 'element' div or are there any pure CSS solutions available? Thank you.
.container {
border: 2px solid red;
margin: 5rem;
height: 20rem;
width: 20rem;
position: relative;
}
.element {
background: pink;
height: 10rem;
width: 10rem;
margin: 3rem;
}
.modal {
position: absolute;
background: green;
height: 2rem;
width: 2rem;
}
<div class="container">
<div class="modal" />
<div class="element" />
</div>