如何使div垂直居中,以及水平居中

详见
https://stackoverflow.com/questions/2743989/how-to-vertically-center-divs

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        /*使div1垂直居中,且水平居中*/
        html, body {
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center
        }

        /*使div1中嵌套的div2垂直居中,且水平居中*/
        #div1 {
            display: flex;
            align-items: center;
            justify-content: center
        }

        /*使div2中的内容垂直居中,且水平居中*/
        #div2 {
            display: flex;
            align-items: center;
            justify-content: center
        }
    </style>
</head>
<body>
    <div id="div1" style="width: 500px;height: 300px;border: solid 1px red;">
        <div id="div2" style="width: 60%;height: 30%;border: solid 1px green;">11</div>
    </div>
</body>
</html>

顺便再说一下:stackoverflow上的问答真的很专业牛掰,推荐大家在遇到问题时首先去stackoverflow。
英文不好也没关系,可以借助翻译软件。
我英文也不好,所以以前遇到问题时,基本不会去stackoverflow,但后来慢慢发现,大部分的问题都能在stackoverflow上找到答案。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值