I have two 50% width divs and want to make a div(which is inside one of them) to be position fixed and center.
I have created a codepen. Please have a look at it to get a better idea about the problem. Also I have embed my code here as well.
<div class="parent">
<div class="first">
<p>...</p>
</div>
<div class="second">
<p>...</p>
<div class="floating"></div>
</div>
</div>
.parent {
display: flex;
width: 100%
}
p {
color: #fff;
padding: 20px;
}
.first {
width: 50%;
height: 100vh;
background-color: #000;
}
.second {
width: 50%;
height: 100vh;
background-color: #ddd;
}
.floating {
width: 100px;
height: 100px;
background-color: #fff;
position: fixed;
}
PS I know its possible with position sticky but I don't want to use it. You can add the below code in .floating
class and it will be in center.
position: sticky;
margin: 0 auto;