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;
        }
相关推荐
cnxy1881 分钟前
Python Web开发新时代:FastAPI vs Django性能对比
前端·python·fastapi
神仙姐姐QAQ2 分钟前
vue3更改.el-dialog__header样式不生效
前端·javascript·vue.js
脾气有点小暴3 分钟前
uniapp真机调试无法连接
前端·uni-app
AI_56785 分钟前
Vue.js 深度开发指南:从数据绑定到状态管理的最佳实践
前端·javascript·vue.js
Irene19915 分钟前
Sass常用语法总结
前端·sass
程序员爱钓鱼6 分钟前
Node.js 博客系统实战(一):项目需求分析
前端·后端·node.js
阿星AI工作室6 分钟前
魔改豆包输入法变电脑版,立即拥有千元AI语音输入法typeless平替
前端·人工智能
前端-文龙刚8 分钟前
浅记Vue3中 ref 和 reactive 是两种主要的响应式数据声明方式,它们有以下主要区别
前端·javascript·vue.js
小先生8129 分钟前
关于vue-element-plus-admin的mini分支踩坑集锦
前端·vue.js·前端框架·c#
hhcccchh10 分钟前
学习vue第十天 V-Model学习指南:双向绑定的魔法师
前端·vue.js·学习