水平垂直居中

<div class="father">
	<div class="son"></div>
</div>

1:margin:anto

.father {
    background-color: aquamarine;
    width: 400px;
    height: 400px;
    position: relative;
}
.son {
    background-color: brown;
    width: 200px;
    height: 200px;
    position: absolute;
    margin: auto;
    top: 0px;
    bottom: 0px;
    right: 0px;
    left: 0px;
}
  1. 负margin法
.father {
    background-color: aquamarine;
    width: 400px;
    height: 400px;
    position: relative;
}
.son {
    background-color: brown;
    width: 200px;
    height: 200px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -100px; /* 负height的一半 */
    margin-top: -100px;  /* 负width的一半 */
}
  1. transform: translate(-50%, -50%)
.father {
    background-color: rgb(183, 201, 195);
    width: 400px;
    height: 400px;
    position: relative;
}
.son {
    background-color: rgb(233, 173, 173);
    width: 200px;
    height: 200px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
} 
  1. 弹性盒子:display:flex— CSS3的新属性

align-items:Y轴居中;justify-content:X轴居中

.father {
    background-color: rgb(183, 201, 195);
    width: 400px;
    height: 400px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.son {
    background-color: rgb(233, 173, 173);
    width: 200px;
    height: 200px;
}
  1. table-cell
table-cell + text-algin
.father {
    background-color: rgb(183, 201, 195);
    width: 400px;
    height: 400px;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
.son {
    background-color: rgb(230, 132, 132);
    width: 200px;
    height: 200px;
    display: inline-block;
} 


table-cell + margin
.father {
    background-color: rgb(183, 201, 195);
    width: 400px;
    height: 400px;
    display: table-cell;
    vertical-align: middle;
}
.son {
    background-color: rgb(230, 132, 132);
    width: 200px;
    height: 200px;
    margin: 0 autto;
} 
  1. vertical-align: middle ---- 位于父元素的中部:可水平垂直居中文字
.father {
    background-color: rgb(183, 201, 195);
    width: 400px;
    height: 400px;
    display: table-cell;
    vertical-align: middle; 
    text-align: center;
}

<div class="father">
	<p>我是儿子</p>
</div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值