首页 > 其他 > 详细

实现任意元素居中

时间:2018-12-27 01:11:00      阅读:154      评论:0      收藏:0      [点我收藏+]
<style>
*{
padding: 0;
margin: 0;
}
.box{
width: 400px;
height: 400px;
border-radius: 200px;
background-color: #ccc;
margin:100px auto;
position: relative;
}
.rec{
width: 200px;
height: 200px;
background-color: red;
position: absolute;
/*定位的百分比是参照父容器的宽高*/
left: 50%;
top: 50%;
/*使用transform实现元素的居中 百分比是参照元素本身的宽高*/
transform: translate(-50%,-50%);
/*transform: translate(-50px,-50px);*/
}
</style>
</head>
<body>
<div class="box">
<div class="rec"></div>
</div>

实现任意元素居中

原文:https://www.cnblogs.com/lujieting/p/10182624.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!