CSS盒子水平垂直居中的实现方式有什么
Admin 2022-08-25 群英技术资讯 606 次浏览
最全的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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
CSS中transform的旋转、缩放、倾斜、移动的形变处理怎样做?下文有实例供大家参考,对大家了解操作过程或相关知识有一定的帮助,而且实用性强,希望这篇文章能帮助大家,下面我们一起来了解看看吧。
这篇文章主要介绍了AmazeUI 单选框和多选框的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
在css中,font的意思是“字体”,是一个简写属性,用于设置所有字体属性;font属性可以按顺序设置字体的样式,语法为“元素{font:style variant weight size/height family;}”。
用CSS实现骰子效果的方法和代码是什么?有不少朋友对此感兴趣,下面小编给大家整理和分享了相关知识和资料,易于大家学习和理解,有需要的朋友可以借鉴参考,下面我们一起来了解一下吧。
本篇文章给大家介绍一下如何使用CSS实现有意思的鼠标跟随 3D 旋转效果,让交互更加生动,希望对大家有所帮助!
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008