Mike
Mike

Reputation: 60831

CSS to keep element at "fixed" position on screen

I'm looking for a trick to create a "fixed" HTML object on the browser screen using CSS. I want it to stay in the same position all the time, even when the user scrolls through the document. I'm not sure what the proper term for this is.

It would be like the chat button on Facebook or the Feedback button that is on some websites that follows you throughout the page.

In my situation, I want to keep a div at the absolute bottom-right corner of the screen at all times. Sample CSS appreciated.

Upvotes: 80

Views: 316056

Answers (12)

Ayden Cheong
Ayden Cheong

Reputation: 11

You can try this code:

<div style="top: 0; position: sticky;">your code</div>

or you can add class/id like this:

html:

<div class="FixedPosition">your code</div>

css:

.FixedPosition{
 position: sticky;
 top: 0;
}

you may change the "top" if you want it to not be on top of the screen and don't delete the top else it won't work.

I hope this help : )

Upvotes: 1

Altaf Patel
Altaf Patel

Reputation: 1410

Try this one:

p.pos_fixed {
    position:fixed;
    top:30px;
    right:5px;
}

Upvotes: 3

realshadow
realshadow

Reputation: 2585

You can do like this:

#mydiv {
    position: fixed; 
    height: 30px; 
    top: 0; 
    left: 0; 
    width: 100%; 
}

This will create a div, that will be fixed on top of your screen. - fixed

Upvotes: 1

Srinivas08
Srinivas08

Reputation: 1062

Make sure your content is kept in a div, say divfix.

<div id="divfix">Your Code goes here</div>

CSS :

#divfix {
       bottom: 0;
       right: 0;
       position: fixed;
       z-index: 3000;
}

Hope ,It will help you..

Upvotes: 18

Chambah Russell
Chambah Russell

Reputation: 71

#fixedbutton {
    position: fixed;
    bottom: 0px;
    right: 0px; 
    z-index: 1000;
}

The z-index is added to overshadow any element with a greater property you might not know about.

Upvotes: 1

Hermes Nguyen
Hermes Nguyen

Reputation: 107

position: sticky;

The sticky element sticks on top of the page (top: 0) when you reach its scroll position.

See example: https://www.w3schools.com/css/tryit.asp?filename=trycss_position_sticky

Upvotes: 9

dgsinclair
dgsinclair

Reputation: 21

In order to keep floating text in the same location over an image when changing browser zoom, I used this CSS:

position: absolute;
margin-top: -18%

I think the % instead of fixed pixels is what does it. Cheers!

Upvotes: 2

Rana Aalamgeer
Rana Aalamgeer

Reputation: 712

HTML

<div id="fixedbtn"><button type="button" value="Delete"></button></div>

CSS

#fixedbtn{
 position: fixed;
 margin: 0px 10px 0px 10px;
 width: 10%;
}

Upvotes: 0

Robert F Wainblat III
Robert F Wainblat III

Reputation: 29

The tweak:

position:fixed; 

works, but it breaks certain options....for example a scrollable menu that is tagged with a fixed position will not expand with the browser window anymore...wish there was another way to pin something on top/always visible

Upvotes: 3

Pekka
Pekka

Reputation: 449783

You may be looking for position: fixed.

Works everywhere except IE6 and many mobile devices.

Upvotes: 95

mreq
mreq

Reputation: 6542

The easiest way is to use position: fixed:

.element {
  position: fixed;
  bottom: 0;
  right: 0;
}

http://www.w3.org/TR/CSS21/visuren.html#choose-position

(note that position fixed is buggy / doesn't work on ios and android browsers)

Upvotes: 53

sg3s
sg3s

Reputation: 9567

position: fixed;

Will make this happen.

It handles like position:absolute; with the exception that it will scroll with the window as the user scrolls down the content.

Upvotes: 2

Related Questions