爱意满满的作品展示区。
disinfeqt

Perfect centering with pure CSS

  •  
  •   disinfeqt · Jul 24, 2010 · 6794 views
    This topic created in 5778 days ago, the information mentioned may be changed or developed.
    有时为了做一个很酷的演示页面,会需要用到绝对居中(水平+垂直)。
    如果是像这样写,会有很大的局限性。
    最近开始用这个自己摸索出来的方法来居中:

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Perfect Centering</title>
    <style>
    html {
    display:table;
    height:100%;
    width:100%;
    }
    body {
    display:table-cell;
    height:105px;
    text-align:center;
    vertical-align:middle;
    width:418px;
    }
    </style>
    </head>
    <body>
    <h1>I should've been centered and I'm cool with it.</h1>
    </body>
    </html>

    What about IE? Just fuck off.
    4 replies    1970-01-01 08:00:00 +08:00
    disinfeqt
        1
    disinfeqt  
    OP
       Jul 24, 2010
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Perfect Centering</title>
    <style>
    html {
    display:table;
    height:100%;
    width:100%;
    }
    body {
    display:table-cell;
    text-align:center;
    vertical-align:middle;
    }
    </style>
    </head>
    <body>
    <h1>I should've been centered and I'm cool with it.</h1>
    </body>
    </html>

    Sorry sorry,哪里来的高度宽度... 囧
    airwolf
        2
    airwolf  
       Jul 24, 2010 via iPod
    Wow, 正好可以用到的,看起来很犀利啊!

    Thanks :)
    disinfeqt
        3
    disinfeqt  
    OP
       Jul 24, 2010
    @airwolf Glad to help :)
    lianghai
        4
    lianghai  
       Jul 24, 2010
    哇靠,感慨,原来“display:”这么牛。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1094 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 18:34 · PVG 02:34 · LAX 11:34 · JFK 14:34
    ♥ Do have faith in what you're doing.