Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CSS, how do you anchor an element to a baseline and size it based on another "pinned" element?

Here's an image of what I'm referring to:

enter image description here

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?

like image 462
mxiong Avatar asked Aug 17 '16 18:08

mxiong


People also ask

How do you anchor an element to the bottom?

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.

How do you anchor to the bottom in CSS?

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.

How do I keep my div position fixed?

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.

How do you make a div always visible on scrolling?

You need to put position: fixed; on the div element. That will anchor it to the viewport.


1 Answers

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>
like image 105
kukkuz Avatar answered Oct 06 '22 13:10

kukkuz