0

I need to align a (nested) element to the vertical center of its container element, but when the nested element becomes taller than the container, the container needs to scroll starting from the top of the nested content (without it extending up past the top).

I was looking for a quick solution to this, but I'm encountering a lot of noise in my search results, no matter where I look.

Ideally the solution would use Flexbox, but any and all solutions are welcome, as long as they work in ALL of the target browsers.

This needs to work in IE 11, Edge, and current versions of Firefox, Chrome, and Safari.

Please, before you mark this as a duplicate, ensure that the similar question has a clear solution for the same target browsers. Once I read comments on some of the solutions I've seen, I see they don't fit my use case.

Here's one thing I have tried, but the nested content stays centered, and extends past the upper and lower bounds of the container, instead of scrolling, when too tall:

<div class="float-container">
  <div class="float-content">
    <!-- Some content that changes height -->
  </div>
</div>

.float-container {
  margin-top: 50px;
  height: calc(100vh - 100px);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-y: scroll;
}
BBaysinger
  • 6,614
  • 13
  • 63
  • 132
  • 1
    show us what you tried so far – לבני מלכה Jul 09 '18 at 05:06
  • In order for us to help you better, can you please update your question so that it shows your relevant code in a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) in the **question itself**. External sites may be malicious, flagged by filter systems, or removed at a later stage (rendering your question useless). It would also be helpful if you could let us know what you have tried so far to solve your problem. For further information, please refer to the help article regarding [how to ask good questions](http://stackoverflow.com/help/how-to-ask). – caiovisk Jul 09 '18 at 05:07
  • Question updated. – BBaysinger Jul 09 '18 at 05:12

2 Answers2

1

you can try margin:auto

.float-content {
    margin: auto;
    overflow:auto;
}

https://jsfiddle.net/m4tsudc7/3/

lalit bhakuni
  • 607
  • 5
  • 15
0

Try code bellow:

.float-container{
  display: flex;
  justify-content: center;
  margin-top: 50px;
  height: calc(100vh - 100px);
  overflow-y:auto;
  }
.float-content{
margin:auto
}
<div class="float-container">
  <div class="float-content">
    Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page.
<!-- Some content that changes height -->
  </div>
</div>

You can use transform: translate(-50%,-50%);

    .float-container{
      height: calc(100vh - 100px);
      overflow-y:auto;
      }
     .float-content  { 
    width:100%
  transform: translate(-50%,-50%);
  }
<div class="float-container">
      <div class="float-content">
        Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page. Try to scroll down the page.
    <!-- Some content that changes height -->
      </div>
    </div>
לבני מלכה
  • 15,925
  • 2
  • 23
  • 47