开发者

How to display a div over other content and in the center of webpage using CSS?

开发者 https://www.devze.com 2023-03-21 00:08 出处:网络
How do I display a div over other content of webpage? This div must be centered vertically and horizontally as in this example:

How do I display a div over other content of webpage? This div must be centered vertically and horizontally as in this example:

How to display a div over other content and in the center of webpage using CSS?

I tried this:

<div style = "position: absolute; top: 50%; left: 50%;">DIV content</div>

In this example only the left corner is centered and not div 开发者_如何学运维itself.


The following should work.

div{
    position:fixed;
    width:500px;
    height:600px;
    margin:-300px auto auto -250px;
    top:50%;
    left:50%;
    text-align:center;
}

The -300 pixels and -250 pixels are the negative half of the height/width of the div respectively.

There may be a better way of doing this, but this is a method I used a while back.

0

精彩评论

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