开发者

CSS to keep element at "fixed" position on screen

开发者 https://www.devze.com 2023-04-09 09:34 出处:网络
I\'m looking fo开发者_StackOverflow中文版r 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 th

I'm looking fo开发者_StackOverflow中文版r 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.


You may be looking for position: fixed.

Works everywhere except IE6 and many mobile devices.


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)


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..


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


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


Try this one:

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


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.


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!


#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.


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


HTML

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

CSS

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


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 : )

0

精彩评论

暂无评论...
验证码 换一张
取 消