Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force div element to stay in same place, when page is scrolled

Tags:

html

css

I created a div element, that I placed all the way on the right of my site. The only problem is that its at the top of the site, so if i scroll down it remains there.

How can I force it to remain in the same part of the page, when page is being scrolled?

This is, what I've managed to figure out myself so far:

<div style="width: 200px; background-color: #999; z-index: 10; position: absolute; right: 0; top: 0; height: 83px;"> </div> 
like image 394
soniccool Avatar asked Sep 11 '13 16:09

soniccool


People also ask

How do you make a div stay in a fixed position?

Use position fixed. Position fixed makes it so that an element stays at it's specified place even if the user scrolls up or down.

How do you force an element to stay in a div?

You can force the content of the HTML <div> element stay on the same line by using a little CSS. Use the overflow property, as well as the white-space property set to “nowrap”.

Which position will keep the element in the same place regardless of scrolling?

A fixed position element is positioned relative to the viewport, or the browser window itself. The viewport doesn't change when the window is scrolled, so a fixed positioned element will stay right where it is when the page is scrolled.


2 Answers

Change position:absolute to position:fixed;.

Example can be found in this jsFiddle.

like image 118
Anton D Avatar answered Sep 21 '22 16:09

Anton D


Use position: fixed instead of position: absolute.

See here.

like image 35
user2769260 Avatar answered Sep 23 '22 16:09

user2769260