How to make a fixed div element with css ?

2019-09-17 06:22发布

问题:

I need to create div element that would always be at the top right corner above all other elements

Here is what I tried

<div id="prompt" style=" height:50px;  width:50px;  background: #F00;  position: fixed;  top: 150px;    margin-left: 948px; z-index: 9999">Test</div>

I need it to work with IE8 , but it is not working.

回答1:

You need to set left: 0; or use right without margin-left.



回答2:

For positioning in the top-right corner: <div id="prompt" style="height:50px; width:50px; background: #F00; position: fixed; top: 150px; right: 0; z-index: 9999">Test</div>

Change the right value as you see fit (i.e. if you want the element an equal distance from the top as it is from the right, make the right value 150px)



回答3:

By removing margin-left: 948px; and adding right: 0px;, your #prompt div will position itself on the right side of the screen in IE 8 and in major browsers as well I believe (Firefox confirmed).