开发者

How can I have a div element anywhere over other elements on a page?

How can I position a div anywhere on a page? I'm trying to treat it as a popup box. B开发者_如何学Pythonelow is what happens when I use the following code:

<div style="width: 100px;padding: 5px; background-color: #CCCCCC;top: 73px; left: 0px">TEST</div>


Use absolute positioning:

style="position:absolute; top:10px; left:10px"

the top and left are the x,y coordinates of the top left edge of your div.


giv the body an position: relative and the div a position: absolute

This way the div will can be positioned anywhere relative to the body, using top and left properties.

In cases like these it makes more sense to use the css in the head or external file.


You need to apply position: absolute to your div element's CSS.


You need to know more about CSS Positioning.

In your case you can use either position:absolute; or position:fixed; based on your needs.


position: absolute; will not work as you expect when the user has scrolled down on the page. You'll want to also look at position:fixed, which will position an element relative to the "viewport" (the browser window, no matter where the user has scrolled).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜