打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
css常见的水平垂直居中
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>水平垂直居中</title>
</head>
<style>
.outBox {
position: relative;
width: 300px;
height: 300px;
background: #ff8300;
border: 1pxsolid#ff8300;
border-radius: 10px;
margin: auto
}
.innerBox {
position: absolute;
width: 100px;
height: 100px;
background: rgba(0,0,0, .7);
border: 1pxsolid#ff8300;
border-radius: 6px;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}
</style>

<body>
<div class="outBox">
<div class="innerBox">

</div>
</div>
</body>

</html>


外层相对定位,内层绝对定位。看要居中的物体大小top,left统统50%,然后margin-left,margin-top 负的物体大小的一半,
比如一个内层100px*100px的,那就是margin-left:-50px;margin-top:-50px

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
移动Web开发必备基础(flex容器属性)
CSS盒子居中的几种方法
HTML第五课——css盒子模型【2】
移动Web开发必备基础(flex项目属性)
标准流,非标准流,盒子模型
css中border-sizing属性详解和应用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服