当前位置:网站首页>实现一个盒子在父盒子中水平垂直居中的几种“姿势”
实现一个盒子在父盒子中水平垂直居中的几种“姿势”
2022-04-23 12:28:00 【如花菇凉】
道理说千遍,不如自己敲一遍,来吧,老铁们,就当是复习下加深印象。
首先来个sample
<body>
<div class="container" id="container">
<div class="box" id="box">我是子盒子</div>
</div>
</body>
<style>
.container{
position: relative;
height: 500px;
box-sizing: border-box;
border: 1px solid #999;
}
.box{
box-sizing: border-box;
background-color: pink;
width: 200px;
height: 100px;
}
</style>
效果如下

方法一 绝对定位+margin负值
/* 缺陷:需要知道盒子的宽高 */
.box{
box-sizing: border-box;
background-color: pink;
position:absolute;
width: 200px;
height: 100px;
left:50%;
top:50%;
margin-left:-100px;
margin-top:-50px;
}
方法二 绝对定位+margin:auto
/* 优点:不需要盒子的确切宽高 */
/* 缺陷:盒子一定要有宽高,缺一不可,缺少一个都达不到效果 */
.box{
box-sizing: border-box;
background-color: pink;
position:absolute;
width: 200px;
height: 100px;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
方法三 绝对定位+translate()
/* 优点:不需要盒子的确切宽高,也不要求盒子一定要有宽高 */
/* 缺陷:兼容性要求 */
.box{
box-sizing: border-box;
background-color: pink;
position:absolute;
width:200px; //也可以没有宽
height:100px; //也可以没有高
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
方法四 父盒子flex
/* 优点:不需要盒子的确切宽高,也不要求盒子一定要有宽高 */
/* 缺陷:兼容性要求 */
.container{
width:500px;
height:300px;
background-color: green;
display: flex;
justify-content: center;
align-items: center;
}
.box{
box-sizing: border-box;
background-color: pink;
position:absolute;
width:200px;
height:100px;
}
方法五 父盒子table-cell + 行内块盒子(这块本人不研究,基本不用)
/* 优点:不需要盒子的确切宽高,也不要求盒子一定要有宽高 */
/* 缺陷:父盒子要有确切的宽高,不能是百分比;子盒子需要额外添加行内属性 */
.container{
display: table-cell;
vertical-align: middle;
text-align: center;
width: 600px;
}
.box{
display: inline-block;
}
方法六 js动态设置 id可以直接获取页面相同的dom元素(这块本人用的极少,不过是JS式的,没啥难度)
<script>
let pW = container.clientWidth,
pH = container.clientHeight,
boxW = box.offsetWidth,
boxH = box.offsetHeight;
box.style.position = 'absolute';
box.style.left = (pW - boxW)/2 + 'px'
box.style.top = (pH - boxH)/2 + 'px'
</script>
以上的实践摘自于
版权声明
本文为[如花菇凉]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u012174809/article/details/124361542
边栏推荐
- STM32控制步进电机(ULN2003+28byj)
- 硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
- 如何防止网站被黑客入侵篡改
- Idea code quality specification plug-in sonarlint
- AI video cloud vs narrowband HD, who is the darling of the video era
- Source code analysis of synchronousqueue
- 1. Construction of electron development environment
- What is a gateway
- STM32工程移植:不同型号芯片工程之间的移植:ZE到C8
- 甲辰篇 創世紀《「內元宇宙」聯載》
猜你喜欢

Idea setting copyright information

QT draw image

Tips for installing MySQL service in windows11: Install / Remove of the Service denied

Zigbee之CC2530最小系统及寄存器配置(1)

XinChaCha Trust SSL Organization Validated

The database navigator uses the default MySQL connection prompt: the server time zone value 'Ö Ð¹ ú±ê ×¼ ʱ ¼ ä’ is unrecognized or repres

Zero trust in network information security

Next.js 静态数据生成以及服务端渲染的方式

STM32控制步进电机(ULN2003+28byj)

亿级流量架构,服务器如何扩容?写得太好了!
随机推荐
Lesson 23 temporary objects
CGC: contractual graph clustering for community detection and tracking
Fabric 1.0 source code analysis (33) implementation of peer channel command and subcommand
Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
MySQL函数-递归函数
Plato Farm-以柏拉图为目标的农场元宇宙游戏
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
c# 设置logo图标和快捷方式的图标
航芯技术分享 | ACM32 MCU安全特性概述
QT interprocess communication
Metalama简介4.使用Fabric操作项目或命名空间
IDEA 代码质量规范插件SonarLint
【vulnhub靶场】-dc2
远程桌面之终端服务器超出了最大允许连接数解决
Qt重绘事件与剪切
洛谷P3236 [HNOI2014]画框 题解
QT redraw events and cuts
Idea code formatting plug-in save actions
IDEA 中 .properties文件的中文显示乱码问题的解决办法
九十八、freemarker框架报错 s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request