开发者

How to align div vertically in the center?

I开发者_如何学JAVAs there a way to align a div vertically in the center of my browser only with CSS usage?


Here's one way to do it:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            html {
                display: table;
                width: 100%;
                height: 100%
            }
            body {
                display: table-row;
                margin: 0;
                height: 100%
            }
            #foo {
                display: table-cell;
                height: 100%;
                vertical-align: middle
            }
            #test {
                margin: auto;
                background-color: #ccc
            }
        </style>
    </head>
    <body>
        <div id="foo">
            <div id="test">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
        </div>
    </body>
</html>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜