将div对齐到页面中心,而它的位置是绝对的? [英] Aligning a div to center of page while its position is absolute?

查看:72
本文介绍了将div对齐到页面中心,而它的位置是绝对的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的对齐 a DIV 到我的页面中心,而其<位置如何是绝对?如果可能 without using javascript。

How can I align a DIV to the center of my page while its position is absolute? If possible without using javascript.

推荐答案

你知道你想居中的 DIV 的宽度。

You can do this if you know the width of the DIV you want to centre.

CSS:

div
{
    position: absolute;
    top: 50%;
    left: 50%;
    width: 400px;
    height: 300px;
    margin-top: -150px;
    margin-left: -200px;
}

您将左上角定位在中间位置,然后使用负边距是宽度的一半来居中。

You position the top left corner in the centre, and then use negative margins which are half of the width to centre it.

这篇关于将div对齐到页面中心,而它的位置是绝对的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆