user773804
user773804

Reputation: 143

How do you place an element a fixed distance from the top of the browser?

I want to position an element so that it is always some fixed distance from the top of the browser so that even if the user scrolls down, the image will always be the same distance from the top of the screen. How do I do this?

Upvotes: 2

Views: 6898

Answers (1)

Blender
Blender

Reputation: 298046

No jQuery needed here. Pure CSS will suffice:

#element {
   position: fixed;
   top: 100px;
}

But if you insist on a jQuery solution, the code below sets the CSS via jQuery:

$('#element').css({'position': 'fixed', 'top': '100px'});

Upvotes: 8

Related Questions