Here's an image of what I'm referring to:
If you have some fixed height h
from the baseline that the pin lies, and the green element is dynamically sized, how can you make the orange element take the space between the two?
Just set the parent div with position:relative . Then, the inner item you want to stick to the bottom, just use position:absolute to stick it to the bottom of the item. Save this answer.
Set the position of div at the bottom of its container can be done using bottom, and position property. Set position value to absolute and bottom value to zero to placed a div at the bottom of container.
A pinned-down menu. The interesting rule here is the ' position: fixed ', that makes the DIV stay fixed on the screen. The ' top: 50% ' and ' right: 0 ' determine where the DIV is displayed, in this case: 50% down from the top of the window, and a constant 0px from the right.
You need to put position: fixed; on the div element. That will anchor it to the viewport.
Have exactly what you need in this case using a flexbox
.
The pin approximately stays at the same height above the baseline give or take 1px.
How it works: When the green element grows say 10px
the pin is elevated by 5px
. But the flex setup means the dummy
and the orange
box reduces 5px
each thus keeping the pin at a contant height.
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
}
.wrapper {
display: flex;
flex-direction: column;
align-items: center;
height: 100px;
border-bottom: 1px solid black;
}
.dummy {
flex: 1;
}
.top {
height: 20px;
width: 20px;
border: 1px solid green;
position: relative;
}
.top div {
position: absolute;
height: 3px;
width: 3px;
background: #880015;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.bottom {
width: 50px;
border: 1px solid orange;
flex: 1;
}
<div class="wrapper">
<div class="dummy"></div>
<div class="top">
<div></div>
</div>
<div class="bottom"></div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With