ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

小盒子在大盒子里水平垂直居中的几种方式

2021-06-26 10:02:43  阅读:196  来源: 互联网

标签:居中 盒子 500px 小盒子 height width background margin 200px


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
/*方法一、纯margin + overflow:hidden;*/
/* .boss{
width: 500px;
height: 500px;
background: orange;
overflow: hidden;
}
.box{
width: 200px;
height: 200px;
background: green;
margin-left: 150px;
margin-top: 150px;
} */
/*方法二、纯padding*/
/* .boss{
width: 350px;
height: 350px;
background: orange;
padding-left: 150px;
padding-top: 150px;
}
.box{
width: 200px;
height: 200px;
background: green;
} */
/*方法三、纯定位*/
/* .boss{
width: 500px;
height: 500px;
background: orange;
position: relative;
}
.box{
width: 200px;
height: 200px;
background: green;
position: absolute;
left: 150px;
top: 150px;
} */
/*方法四、display:inline-block + text-align:center + vertical-align: middle*/
/* .boss{
width: 500px;
height: 500px;
background: orange;
text-align: center;
line-height: 500px;
}
.box{
width: 200px;
height: 200px;
background: green;
display: inline-block;
vertical-align: middle;
} */
/*方法五、定位 + margin取负值的方法*/
/* .boss{
width: 500px;
height: 500px;
background: orange;
position: relative;
}
.box{
width: 200px;
height: 200px;
background: green;
position: absolute; */
/* left: 50%; *//*向右移动父元素宽度的一半*/
/* margin-left: -100px; *//*向左移动自身宽度的一半*/
/* top: 50%; *//*向下移动父元素高度的一半*/
/* margin-top: -100px; *//*向上移动自身高度的一半*/
/* } */
/*方法六 定位 + margin:auto;*/
.boss{
width: 500px;
height: 500px;
background: orange;
position: relative;
}
.box{
width: 200px;
height: 200px;
background: green;
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<body>
<!--
需求:一个宽高为200*200的小盒子在一个宽高500*500大盒子里面做水平垂直居中,请写出你知道的实现方法?(6种)
-->
<div class="boss">
<div class="box"></div>
</div>
</body>
</html>

标签:居中,盒子,500px,小盒子,height,width,background,margin,200px
来源: https://www.cnblogs.com/wangyiqun2001/p/14933465.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有