子元素在父元素中垂直水平居中的方法总结

居中方法

1.margin:auto居中法
  • 父元素相对定位
  • 子元素绝对定位
    top,bottom,left,right均设0.
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<style type="text/css">
		.father{
			width: 200px;
			height: 200px;
			background: red;
			position: relative;
		}
		.childer{
			background: blue;
			width: 50px;
			height: 50px;
			margin: auto;
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
		}
	</style>
	<body>
		<div class="father">
			<div class="childer">
				123
			</div>
		</div>
	</body>
</html>
2.负值居中法
  • 父元素相对定位

  • 子元素宽高固定

  • 子元素绝对定位,left: 50%;top:50%

  • 子元素设置margin-left,margin-top.数值为子元素宽高的一半.

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8" />
    		<title></title>
    	</head>
    	<style type="text/css">
    		.father{
    			width: 200px;
    			height: 200px;
    			background: red;
    			position: relative;
    		}
    		.childer{
    			background: blue;
    			width: 50px;
    			height: 50px;
    			margin: auto;
    			position: absolute;
    			left:50%;
    			top: 50%;
    			margin-left: -25px;
    			margin-top: -25px;
    		}
    	</style>
    	<body>
    		<div class="father">
    			<div class="childer">
    				123
    			</div>
    		</div>
    	</body>
    </html>
    
3.table-cell居中法
  • 父元素设为table-cell

  • 子元素设为inline-block,或margin:auto

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8" />
    		<title></title>
    	</head>
    	<style type="text/css">
    		.father{
    			width: 200px;
    			height: 200px;
    			background: red;
    			display: table-cell;
    			vertical-align: middle;
    			text-align: center;
    		}
    		.childer{
    			background: blue;
    			width: 50px;
    			height: 50px;
    			display: inline-block;//或者不同display而是使用 margin:auto
    			/* margin: auto; */
    		}
    	</style>
    	<body>
    		<div class="father">
    			<div class="childer">
    				123
    			</div>
    		</div>
    	</body>
    </html>
    
4.flex居中法
  • 父元素设为flex,并进行配套设置

  • 子元素无要求

  • 该方法极度好用,但是兼容性不好(ie10)

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8" />
    		<title></title>
    	</head>
    	<style type="text/css">
    		.father{
    			width: 200px;
    			height: 200px;
    			background: red;
    			display: flex;
    			justify-content: center;
    			align-items: center;
    		}
    		.childer{
    			background: blue;
    			width: 50px;
    			height: 50px;
    		}
    	</style>
    	<body>
    		<div class="father">
    			<div class="childer">
    				123
    			</div>
    		</div>
    	</body>
    </html>
    
5.vertical-align:middle居中法
  • 父元素设置text-align,line-height.

  • 子元素设置为inline-block,vertical-align

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8" />
    		<title></title>
    	</head>
    	<style type="text/css">
    		.father{
    			width: 200px;
    			height: 200px;
    			background: red;
    			text-align: center;
    			line-height: 200px;
    		}
    		.childer{
    			background: blue;
    			width: 50px;
    			height: 50px;
    			display: inline-block;
    			vertical-align: middle;
    		}
    	</style>
    	<body>
    		<div class="father">
    			<div class="childer">
    				123
    			</div>
    		</div>
    	</body>
    </html>
    
    
总结
  • 前两种方法为常用方法.
  • 尽量少用display:inline-block. 因为不咋符合w3c规定.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值