当前位置:网站首页>Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
2022-04-23 12:33:00 【As cool as mushrooms】
Truth is said a thousand times , Why don't you knock it yourself , Come on , Old fellow iron , Just review and deepen your impression .
Let's start with sample
<body>
<div class="container" id="container">
<div class="box" id="box"> I'm a sub 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>
The effect is as follows

Method 1 Absolute positioning +margin negative
/* defects : You need to know the width and height of the box */
.box{
box-sizing: border-box;
background-color: pink;
position:absolute;
width: 200px;
height: 100px;
left:50%;
top:50%;
margin-left:-100px;
margin-top:-50px;
}
Method 2 Absolute positioning +margin:auto
/* advantage : You don't need the exact width and height of the box */
/* defects : The box must be wide and high , Be short of one cannot , It won't work without one */
.box{
box-sizing: border-box;
background-color: pink;
position:absolute;
width: 200px;
height: 100px;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
Method 3 Absolute positioning +translate()
/* advantage : You don't need the exact width and height of the box , There is no requirement that the box must be wide or high */
/* defects : Compatibility requirements */
.box{
box-sizing: border-box;
background-color: pink;
position:absolute;
width:200px; // No, it's not
height:100px; // It can also not be high
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
Method four Parent box flex
/* advantage : You don't need the exact width and height of the box , There is no requirement that the box must be wide or high */
/* defects : Compatibility requirements */
.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;
}
Method five Parent box table-cell + In line box ( I don't study this , Basically not )
/* advantage : You don't need the exact width and height of the box , There is no requirement that the box must be wide or high */
/* defects : The parent box should have the exact width and height , It can't be a percentage ; The sub box needs to add additional inline attributes */
.container{
display: table-cell;
vertical-align: middle;
text-align: center;
width: 600px;
}
.box{
display: inline-block;
}
Methods six js Dynamic setting id You can get the same page directly dom Elements ( This piece I use very little , But is JS Type , No difficulty. )
<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>
The above practice is taken from
版权声明
本文为[As cool as mushrooms]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231227335075.html
边栏推荐
- SQL 练习(一)
- 编程辅助工具推荐:图片工具snipaste
- Windows11 安装MySQL服务 提示:Install/Remove of the Service Denied
- 电脑系统卡如何解决?
- Why is the premise of hash% length = = hash & (length-1) that length is the nth power of 2
- Idea database navigator plug-in
- Basic software testing Day2 - Case Execution
- 甲辰篇 創世紀《「內元宇宙」聯載》
- QT one process runs another
- How to switch PHP version in Windows 2008 system
猜你喜欢

编程辅助工具推荐:图片工具snipaste

Plato Farm-以柏拉图为目标的农场元宇宙游戏

Qt双缓冲绘图

亿级流量架构,服务器如何扩容?写得太好了!
欣旺达宣布电池产品涨价 此前获“蔚小理”投资超10亿

Debug Jest test cases in VSCode, debug Jest test cases in VSCode, middle note basedir=$(dirname "$" (echo "$0" sed -e -e, s, \ \, / "-e").

One way ANOVA of SPSS

S2-062 remote command execution vulnerability recurrence (cve-2021-31805)

在 VSCode 中调试 Jest 的测试用例,VSCode调试Jest测试用例报错basedir=$(dirname “$(echo “$0“ | sed -e ‘s,\\,/,g‘)“)解决

AI 视频云 VS 窄带高清,谁是视频时代的宠儿
随机推荐
对话PostgreSQL作者Bruce:“转行”是为了更好地前行
QT draw image
What is a gateway
[redis series] redis learning 13. Redis often asks simple interview questions
洛谷P3236 [HNOI2014]画框 题解
How much does software testing help reduce program bugs?
Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward
Number of nodes of complete binary tree
Qt一个进程运行另一个进程
QT double buffer drawing
Plato Farm-以柏拉图为目标的农场元宇宙游戏
How to switch PHP version in Windows 2008 system
Zero trust in network information security
面了一圈,整理了这套面试题。。
Here comes the detailed picture and text installation tutorial of H5 game
关于使用Go语言创建WebSocket服务浅谈
Idea code formatting plug-in save actions
SynchronousQueue 源码解析
CGC: contractual graph clustering for community detection and tracking
BUUCTF WEB [BUUCTF 2018]Online Tool