如何中心div? [英] How to center div?

查看:110
本文介绍了如何中心div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML(垂直和水平)中居中div的问题。我的代码看起来像这样:

I have a problem with centering div in HTML (vertical & horizontal). My code looks something like this:

<div id="container">SOME HTML</div>

#container{
    width: 366px;
    height: 274px;
    margin: 50%;
    top: -137px;
    left: -188px;
    position:absolute;
}

只有chrome将这个div居中置于屏幕中间。

Only chrome center this div in to the middle of the screen.

推荐答案

这个技巧(垂直和水平):

This does the trick (vertical & horizontal):

#container{
    position: absolute;
    width: 366px;
    height: 274px;
    left: 50%;
    top: 50%;
    margin-left: -183px; /* half width */
    margin-top: -137px; /* half height */
}

这篇关于如何中心div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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