CSS水平垂直居中解决方案(6种)
前端  /  管理员 发布于 7年前   372
准备
创建元素
<div class="parent"> <div class="child">child</div></div>
垂直水平居中方案一:知道宽度的情况下 absolute+margin负值
.parent { width:400px; height:400px; background: red; position: relative;}.child { position: absolute; left:50%; top:50%; background: yellow; width:50px; height:50px; margin-left:-25px; margin-top:-25px;}
垂直水平居中方案二:不知道宽高的情况下 absolute+transform
.parent { width:400px; height:400px; background: red; position: relative;}.child { position: absolute; left:50%; top:50%; transform: translate(-50%,-50%);}
垂直居中方案三:position+margin:auto
.parent { position:relative; width:200px; height:200px; background: red;}.child { width:80px; height:40px; background: yellow; position: absolute; left:0; top:0; right:0 ; bottom:0; margin:auto;}
垂直居中方案四:+ 多行文本的垂直居中 :table-cell+vertical-align:middle;
.parent { height: 300px; width:400px; border: 1px solid red; display: table-cell; vertical-align: middle; text-align: center;}.child { display: inline-block; width:50px; height:50px; background: blue;}/* 或者 */.parent { width: 400px; height: 300px; display: table-cell; vertical-align: middle; border: 1px solid red; text-align: center;}.child { display: inline-block; vertical-align: middle; background: blue;}
垂直居中方案五:display: flex
.parent { width:400px; height:200px; background:red; display: flex; justify-content:center; align-items:center;}.child { height:100px; width:100px; background:green;}
垂直居中方案六:伪元素
.parent { width:200px; height:200px; background:red; text-align: center;}.child { height:100px; width:100px; background:yellow; display: inline-block; vertical-align: middle;}.parent:before { content:""; height:100%; vertical-align: middle; display: inline-block;}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号