块级元素的水平垂直居中的方法

块级元素的水平垂直居中的方法

  **首先我们要考虑两种情况,定宽高和不定宽高**;

1.有没有宽高都可以
下面两种方法都是需要父相子绝(父块相对定位,子块绝对定位),在子块中设置

方法一 margin:auto

   <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
			
.box1{
    width:100%;
    height:100%;				
    border:1px solid red;					
}
.box2{
    width:400px;
    height:400px;
    background-color:skyblue;
    position: relative;						
}
.box3{
    width:100px;/*1.width:90%也可以 2.不设置宽也可以*/
    height:100px;/*1.height:90%也可以 2.不设置高也可以*/
    background-color: gray;
    position: absolute;
    top:0;
    right:0;
    bottom: 0;
    left: 0;
    margin:auto;			
}			
</style>
</head>
<body>
<div class="box1">
    <div class="box2">div2<div class="box3">div3</div></div> 
<div>	
</body>
</html>

方法二 transform:translate:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
			
.box1{
    width:100%;
    height:100%;				
    border:1px solid red;					
}
.box2{
    width:400px;
    height:400px;
    background-color:skyblue;
    position: relative;						
}
.box3{
    width:100px;/*width:90%也可以 但 margin-top margin-left 也需要改成百分形式*/
    height:100px;/*height:90%也可以 但 margin-top margin-left 也需要改成百分形式*/
    background-color: gray;
    position: absolute;
    top:50%;
    left: 50%;
    transform: translate(-50%,-50%);			
}
</style>
</head>
<body>
<div class="box1">
    <div class="box2">div2<div class="box3">div3</div></div> 
<div>
</body>
</html>

方法三 :display:flex;/将其定义为弹性容器/
align-items: center;/垂直居中对齐/
justify-content: center;/水平居中对齐/

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
			
.box1{
    width:100%;
    height:100%;				
    border:1px solid red;					
}
.box2{
    width:400px;
    height:400px;
    background-color:skyblue;				
    display:flex;/*将其定义为弹性容器*/
    align-items: center;/*垂直居中对齐*/
    justify-content: center;/*水平居中对齐*/
}
.box3{
    /* 宽高可以不设置
    width:100px;
    height:100px;*/				
    background-color: gray;																		
}
</style>
</head>
<body>
<div class="box1">
	<div class="box2"><div class="box3">div3</div></div> 
<div>
</body>
</html>

2.必须要有宽高
方法一 margin-top margin-left

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
			
.box1{
    width:100%;
    height:100%;				
    border:1px solid red;					
}
.box2{
    width:400px;
    height:400px;
    background-color:skyblue;
    position: relative;						
}
.box3{
    width:100px;/*width:90%也可以 但 margin-top margin-left 也需要改成百分形式*/
    height:100px;/*height:90%也可以 但 margin-top margin-left 也需要改成百分形式*/
    background-color: gray;
    position: absolute;
    top:50%;
    left: 50%;
    margin-top: -50px;
    margin-left: -50px;				
}
</style>
</head>
<body>
<div class="box1">
    <div class="box2">div2<div class="box3">div3</div></div> 
<div>		
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值