Search code examples
javascriptjquerycsshtmlcenter

Link to element`s id and centering screen


I have link to section like this:

<a href="#section3">Section 3</a>

How i can to center (vertically) the screen, and section to be in the middle of screen not to the top.

Thank`s


Solution

  • I don't really think that what you want is possible without using Javascript.

    If you are able to use js, you could use this code:

    function scrollToElement(selector, time, verticalOffset) {
    time = typeof(time) != 'undefined' ? time : 1000;
    verticalOffset = typeof(verticalOffset) != 'undefined' ? verticalOffset : 0;
    element = $(selector);
    offset = element.offset();
    offsetTop = offset.top + verticalOffset;
    $('html, body').animate({
        scrollTop: offsetTop
    }, time);
    

    }