5

I have a container with known height (height is sometimes longer then screen).

This container has a vertically and horizontally centered element (via flex).

I was hoping to keep this element always on the screen vertically centered in the block in the visible portion of the container.

What I tried:

  • position:sticky; top:50% - however this only keeps it centered on the bottom half of container.
  • position:sticky; bottom:50% - however this only keeps it centered on top half
  • position:sticky; top: 50%; bottom:50%; - it seems top overrides bottom so this is just like my first try with top:50% only
  • I tried setting negative values but it didn't work

Here is a demo:

.container {
  height: 1200px;
  background-color: rgba(0, 0, 0, .7);
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
}
.center-piece {
  background-color: green;
  position: sticky;
  top: 50%;
}
.center-piece2 {
  background-color: steelblue;
  position: sticky;
  bottom: 50%;
}
<div class="container">
  <div class="center-piece">
    #1
  </div>
  <div class="center-piece2">
    #2
  </div>
</div>

Is there anyway to keep it perfectly centered, while "always on screen", in the visible porition of container, both top and bottom?

Here is a screencast of my application: https://www.youtube.com/watch?v=CwYaBgolNHU

The "rawr" will be the controls for the image behind it.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Noitidart
  • 35,443
  • 37
  • 154
  • 323

3 Answers3

1

NOTE: Compatibility varies across browsers..

According to caniuse.com, position: sticky is supported on current major browsers (Not IE).

jsFiddle

.container {
  height: 1200px;
  background-color: rgba(0, 0, 0, .7);
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
}
.center-piece {
  background-color: green;
  position: sticky;
  top: 10px;                         /* 1 */
  bottom: 50%;
  left: 50%;
  transform: translate(-50%,50%);    /* 2 */
}
.center-piece2 {
  background-color: steelblue;
  position: sticky;
  bottom: 10px;                      /* 3 */
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
}
<div class="container">
  <div class="center-piece">#1</div>
  <div class="center-piece2">#2</div>
</div>

NOTES:

  1. position: sticky kicks in when element reaches top: 10px of viewport
  2. How centering works with CSS positioning and transform properties
  3. position: sticky kicks in when element reaches bottom: 10px of viewport
Agu V
  • 2,091
  • 4
  • 25
  • 40
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • Thanks very much @Michael_B I will test this out now! – Noitidart Aug 07 '16 at 21:01
  • It didn't work :( Thanks so much for your effort to give me a solution I've been strugglign with this for a couple days - here is a screencast showing it is not working using your solution - https://www.youtube.com/watch?v=EJAiYyjJVUE – Noitidart Aug 07 '16 at 21:06
  • 1
    It works in the snippet and the fiddle demo on Firefox. Your screencast doesn't match the behavior in this answer. There's a conflict or something else going on. – Michael Benjamin Aug 07 '16 at 21:09
  • Thanks for such a fast reply, I'm toying with it now and will keep you updated on how it goes. :) – Noitidart Aug 07 '16 at 21:10
1

I may have misunderstood your question, but can you use:

position: fixed;
top: 50vh;

?

.container {
  height: 1200px;
  background-color: rgba(0, 0, 0, .7);
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
}
.center-piece {
  background-color: green;
  position: fixed;
  top: 50vh;
  left: 50vw;
  transform: translate(-50%,50%);
}
<div class="container">
  <div class="center-piece">#1</div>
</div>
Rounin
  • 27,134
  • 9
  • 83
  • 108
  • 1
    Thanks Rounin for trying to help! It doesn't work :( Please see this new screencast which uses your method - https://www.youtube.com/watch?v=84j2gaQ26yw – Noitidart Aug 07 '16 at 21:00
0

Just use postition:absolute; or position:fixed; instead of position:sticky;

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 28 '23 at 11:36