How to position an element just off the top of the

2019-09-07 16:03发布

问题:

I want an absolutely-positioned element to be just out of the browser window - just off the top of the browser viewport. Note that I cannot provide an exact height of the specified element.

Can this be done? If not, using jQuery is fine too.

回答1:

CSS:

#theElement {
    position: fixed;
    bottom: 100%;
}

jQuery:

var $el = $('#theElement');

$el.css({
    position: 'fixed',
    top: '-' + $el.outerHeight()
});


回答2:

If your element is at body level this should work:

#element {
  position: absolute;
  top: -100%;
}