且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何垂直和水平居中未知高度的div

更新时间:2022-10-16 09:12:59

Using a flexbox, here's all the code you need:

HTML

<div class="container">
    <div class="content">
        <div class="title-class">Hello there</div>
    </div>
</div>

CSS

html, body { height: 100%; }

.container {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: violet;
    height: 100%;
}

.content {
    background-color: violet;
    width: 50%;
    text-align: center;
    box-shadow: 0px 1px 7px 1px rgba(0, 0, 0, 1);

}

DEMO

As an alternative, here's the table method:

相关阅读

技术问答最新文章