简体   繁体   中英

How to place element in the middle of visible part of div?

I have a that contains other data and button with position "absolute". This height might be bigger, than window height. When I scroll page, I want button always to be in the middle of visible part of the .

Currently I added event listener onScroll, and I want to change "top" of , based on visible part of the

If you have a DOM that looks something like this:

<body class="scrollable-area">
  ...main content
  <div class="overlay">
    <div class="wrapper-for-your-data-and-button">
      ... your data and button
    </div>
  </div>
</body>

Then you can set the following css:

.overlay {
  width: 100vw;
  height: 100vh;
  position: absolute;
  top: 0px;
  left: 0px;
  display: flex;
  justify-content: center;
  align-items: center;
}

See this jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM