10

To vertically and horizontally center a div with flexbox I simply apply these rules to it's parent

display: flex;
align-items: center;
justify-content: center;

So that it will be displayed like so:

enter image description here

However when there's more content in the div then there is height in the viewport, it makes the content overflow the webpage so it isn't visible, and impossible to scroll up- like so: jsfiddle.net/xk1z6wpa/2

enter image description here

I need the div to not overflow the webpage from the top, rather stopped before reaching it thus only overflowing from the bottom- like so:

enter image description here

How can I accomplish this?

uneducatedguy
  • 1,331
  • 1
  • 10
  • 27

1 Answers1

-1

I think your only option is to throw a little JS in.

$(".content").css({
  'margin-top': Math.max($(".content").height() - $(window).height(), 0)
})

This pushes the content to the top of the page unless it would result in a negative margin, at which point the expression evaluates to 0.

Jack Guy
  • 8,346
  • 8
  • 55
  • 86