div垂直居中方式

1、flex弹性布局

css 复制代码
 		.father {
            width: 300px;
            height: 300px;
			display:flex;
			justify-content:center;
			align-items:center; 
        }

        .box {
            width: 100px;
            height: 100px;
        }

2、table-cell+vertical-align 类似td表格单元

css 复制代码
 		.father {
            width: 300px;
            height: 300px;
			display:table-cell;
			vertical-align:middle;
			text-align:center;
        }

        .box {
            width: 100px;
            height: 100px;
			display:line-block;
        }

3、line-height+vertical-align

css 复制代码
 		.father {
            width: 300px;
            height: 300px;
			line-height:300px;
			text-align:center;
        }

        .box {
            width: 100px;
            height: 100px;
			vertical-align:middle;
			display:line-block;
        }

4、absolute+transform

css 复制代码
 		.father {
            width: 300px;
            height: 300px;
            position: relative;
        }

        .box {
            width: 100px;
            height: 100px;
            position: absolute;
            left: 50%;
            top: 50%;
            /*平移*/
            transform: translate(-50%,-50%);
        }

5、absolute+负margin

css 复制代码
 		.father {
            width: 300px;
            height: 300px;
            position: relative;
        }

        .box {
            width: 100px;
            height: 100px;
            position: absolute;
            left: 50%;
            top: 50%;
            /*宽高的一半*/
            margin-left: -50px;
            margin-top: -50px;
        }

6、absolute+margin

css 复制代码
 		.father {
            width: 300px;
            height: 300px;
            position: relative;
        }

        .box {
            width: 100px;
            height: 100px;
            position: absolute;
			left: 0;
            top: 0;
            bottom:0;
            right:0;
            margin:auto;;
        }

7、grid

css 复制代码
 		.father {
            width: 300px;
            height: 300px;
            display: grid;
        }

        .box {
            width: 100px;
            height: 100px;
            align-self: center;
            justify-self: center;
        }
相关推荐
温宇飞1 小时前
浏览器路由系统的一种实践
前端
老前端的功夫1 小时前
Vue 插槽深度解析:从基础到高级架构设计
前端
pre_lee1 小时前
vue2响应式原理
前端
小奶包他干奶奶1 小时前
如何使用vscode和express开发node.js
前端·node.js
庞囧1 小时前
通俗易懂讲 React 原理-第二集:Fiber
前端
beckyye1 小时前
给web增加简单的ai对话功能
前端·ai·通义千问·qwen
Gee Explorer2 小时前
第一篇使用HTML写一个随机点名网页
css·html5·1024程序员节
青衫码上行2 小时前
【Java Web学习 | 第1篇】前端 - HTML
java·前端·学习
元直数字电路验证2 小时前
HTML 标签及推荐嵌套结构
前端·javascript·html