9

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?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
mxiong
  • 251
  • 1
  • 9
  • Would flex-box work well for this? Otherwise if this is a *very particular* sizing scenario, it could make sense to have JavaScript that resizes the element on each browser resize event. – Katana314 Aug 17 '16 at 18:29
  • Flex _almost_ works but I can't quite get it to match this behavior. If the top element had the top border anchored in place it would be perfect. – mxiong Aug 17 '16 at 18:44

2 Answers2

3

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>
kukkuz
  • 41,512
  • 6
  • 59
  • 95
  • Not quite what I need, unfortunately. The pin needs to remain in place as the size of the green element changes. In your case, the total size of the container would need to change as well. – mxiong Aug 17 '16 at 18:50
  • @mxiong check it out now- the pin remains in place now. :) – kukkuz Aug 18 '16 at 02:31
  • 1
    Very clever. Awesome use of an invisible dummy box! Just gotta make sure I don't overflow and make scrollbars with it, but works great. – mxiong Aug 18 '16 at 18:07
0

You can use table properties for this. Table cells fill its parent width. If one cell is short, the other one will expand to fill its parent. The trick here is to rotate 90º your "table" and it's done. To change the 'height" of your pinned item you will actually be changing its width. The anchor element will resize accordingly.

Be aware of this though: http://caniuse.com/#search=transform

.baseline{
  height: 100px;
  width: 100px;
  border: 3px solid black;
  display: table;
  transform: rotate(90deg);
  }

.pinned,.anchored{
  display: table-cell;
  }

.pinned{
  width: 30px;
  border: 3px solid green;
  }

.anchored{
  border: 3px solid orange;
  }
<div class="baseline">
  <div class="pinned">
    </div>
  <div class="anchored">
    </div>
</div>
Carlos Roso
  • 1,435
  • 1
  • 11
  • 14
  • Unfortunately this has the same issue has kukkuz's suggestion. The center of the pinned element needs to remain a fixed distance from the baseline. In this case the overall container size would need to change. – mxiong Aug 17 '16 at 18:58