CSS盒子水平垂直居中的实现方式有什么
Admin 2022-08-25 群英技术资讯 632 次浏览
最全的CSS盒子(div)水平垂直居中布局,对CSS 布局掌握程度决定你在 Web 开发中的开发页面速度。
<div class="box"></div>
<style>
body {
background: green;
}
.box {
position: fixed;
top: 50%;
left: 50%;
margin: -150px 0 0 -150px;
width: 300px;
height: 300px;
background: orange;
}
</style>
设置 Position 为 fixed 定位,top 和 left 各设置 50%,margin 设置负的容器宽高的一半。
<div class="box"></div>
<style>
body {
background: green;
}
.box {
position: fixed;
top: 50%;
left: 50%;
width: 300px;
height: 300px;
transform: translate(-50%, -50%);
background: orange;
}
</style>
设置 Position 为 fixed 定位,top 和 left 各设置 50%,transform 的 translate 设置上、左 -50%。
<div class="box"></div>
<style>
body {
background: green;
}
.box {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
width: 300px;
height: 300px;
background: orange;
}
</style>
设置 Position 为 fixed 定位,上、右、下、左设置为 0,margin 设置为 auto。
<div class="parent">
<div class="child"></div>
</div>
<style>
.parent {
position: relative;
margin: 100px auto 0;
width: 500px;
height: 500px;
background: green;
}
.child {
position: absolute;
top: 50%;
left: 50%;
margin: -100px 0 0 -100px;
width: 200px;
height: 200px;
background: orange;
}
</style>
父容器设置为相对定位,子容器设置为绝对定位,top 和 left 各设置 50%,margin 设置负的子容器宽高的一半。
<div class="parent">
<div class="child"></div>
</div>
<style>
.parent {
position: relative;
margin: 100px auto 0;
width: 500px;
height: 500px;
background: green;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 200px;
height: 200px;
background: orange;
}
</style>
父容器设置为相对定位,子容器设置为绝对定位,top 和 left 各设置 50%,transform 的 translate 设置上、左 -50%。
<div class="parent">
<div class="child"></div>
</div>
<style>
.parent {
position: relative;
margin: 100px auto 0;
width: 500px;
height: 500px;
background: green;
}
.child {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
width: 200px;
height: 200px;
background: orange;
}
</style>
父容器设置为相对定位,子容器设置为绝对定位,上、右、下、左设置为 0,margin 设置为 auto。
<div class="parent">
<div class="child"></div>
</div>
<style>
.parent {
position: relative;
margin: 100px auto 0;
width: 500px;
height: 500px;
display: flex;
justify-content: center;
align-items: center;
background: green;
}
.child {
width: 200px;
height: 200px;
background: orange;
}
</style>
父容器 display 设置为 flex,水平垂直设置为居中。
<div class="parent">
<div class="child"></div>
</div>
<style>
.parent {
margin: 100px auto 0;
width: 500px;
height: 500px;
overflow: hidden;
background: green;
}
.child {
margin: 150px auto;
width: 200px;
height: 200px;
background: orange;
}
</style>
计算父盒子与子盒子的空间距离。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
HTML中相对位置与绝对位置是怎样,怎么使用?一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解一下,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
这篇文章主要介绍了在html页面中取得session中的值的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
HTML输入标记是什么,语法和使用是怎样的?HTML表单标记之输入标记属性的使用知道怎么操作吗?带着这个问题,小编特地整理了以下相关资料给大家,如果有需要可以随时来参考阅读下。
这篇文章主要介绍了修复一个因为scrollbar占据空间导致的bug问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
在css中,可以利用overflow属性设置超出的文本隐藏起来,该属性用于规定当内容超出元素框的时候发生的事情,当属性的值为“hidden”时,超出的内容会被隐藏,语法为“文本元素{overflow:hidden;}”。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008