CSS盒子水平垂直居中的实现方式有什么
Admin 2022-08-25 群英技术资讯 633 次浏览
最全的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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
css3属性控制旋转的代码是:1、“元素{transform:rotate(旋转角度);}”代码控制元素2d旋转样式;2、“元素{transform:rotate3d(x,y,z,旋转角度);}”代码控制元素3d旋转样式。
这篇文章主要介绍了HTML+CSS+JS模仿win10亮度调节效果的示例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要介绍了AmazeUI图片轮播效果的示例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
本篇文章带大家一起深入了解一下CSS3中的新特性@layer,希望对大家有所帮助!
这篇文章主要介绍了css解决浮动导致父元素高度坍塌的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008