垂直居中的方法和技巧

简单的情况text-align:center; vertical-align: middle等,下面是复杂一点的:
// 方法一:已知元素的高宽


CSS:
.content{
background-color:#6699FF;
width:200px;
height:200px;
position: absolute;
top: 50%;
left: 50%;
/* 二分之一的负height,width */
margin-top:-100px ;
margin-left: -100px;
}
//方法二:未知元素的高宽

CSS:
.content{
background-color:#6699FF;
width:200px;
height:200px;
position: absolute;
/* transform: translate(-50%,-50%) 或 margin:auto与四边距相等 */
margin:auto; / 无这个就只有top、left起作用/
top: 0;
left: 0;
bottom:0;
right: 0;
}
//3,两个按钮在div中居中,并相距一定距离

div{width: 600px;height: 500px;background-color: rgba(240,240,240,0.9);text-align:
center;}
.button{ width:100px;height: 90px;background-color: #f00;position: relative;top: 50%;
transform: translate(0,-50%) }
.button1{margin-left: 0;margin-right: 100px;}
.button2{margin-right: 0;}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值