Position item in the middle of the screen after scrolling

My page is 2x the height of the screen (the size depends on another element on the page). I want to show an absolutely positioned SPAN in the middle of the screen, regardless of the scroll position.

I apply the following style when I click the button, however, if I scroll all the way down, the element is displayed at the top of the page, as it counts 50% of the top of the whole page.

.Centered {
    width: 100%;
    position: absolute;
    top: 50%;
    left: 45%;
}

How to place an element in the middle of the page based on the scroll potion at the click of a button?

Thanks.

+3
source share
3 answers

Are you sure you do not want fixed positioning ?

, . , . , .

.Centered
{
  width:100%;
  position:fixed;
  top:50%;
  left:45%;
}

.

+19
.Centered 
{
    position:absolute;   
    width:100%;
    top:50%;
    left:50%;
    margin-height: -200px;
    max-height: 400px;
}

, max-height.

0

Try using the jQuery Center plugin .

-1
source

Source: https://habr.com/ru/post/1703875/


All Articles