Skip to content

CSS 使用 align-content:center 实现 div 居中

以前实现 div 居中比较方便的方法有:

css
display: flex; 
justify-content: center; 
align-items: center;
css
display: grid; 
place-content: center;

新实现的属性 align-content: center; ,浏览器支持查看🔗

css
align-content: center;

示例代码:

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div style="display: flex; justify-content: center; align-items: center; height: 200px; background-color:aqua;">
        <div style="background-color:antiquewhite; height: 100px;width: 200px;">
            display: flex
        </div>
    </div>
    <div style="display: grid; place-content: center; height: 200px; background-color: aquamarine;">
        <div style="background-color: antiquewhite; height: 100px; width: 200px;">
            display: grid
        </div>
    </div>
    <div style="align-content: center; height: 200px; background-color:blueviolet;">
        <div style="background-color:antiquewhite; height: 100px; width: 200px;  margin: 0 auto;">
            align-content: center
        </div>
    </div>
</body>
</html>

示例效果:

参考:

https://build-your-own.org/blog/20240813_css_vertical_center/

https://www.bilibili.com/video/BV1V7iNeWE9m

亲手创建自己所需的软件,是程序员的幸运。