当前位置:网站首页>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
边栏推荐
- SSL证书退款说明
- Web17——EL与JSTL的使用
- Introduction to metalama 4 Use fabric to manipulate items or namespaces
- 画结果图推荐网址
- Introduction to metalama 4 Use fabric to manipulate items or namespaces
- NBIOT的AT指令
- Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
- BUUCTF WEB [BJDCTF2020]ZJCTF,不过如此
- 洛谷P3236 [HNOI2014]画框 题解
- 对话PostgreSQL作者Bruce:“转行”是为了更好地前行
猜你喜欢

How do traditional enterprises cope with digital transformation? These books give you the answer

电脑系统卡如何解决?

网络信息安全之零信任
![[redis series] redis learning 13. Redis often asks simple interview questions](/img/4c/2440af6daa26a0ca6cb64f32bf138e.png)
[redis series] redis learning 13. Redis often asks simple interview questions

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

Database Navigator 使用默认MySQL连接提示:The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or repres

STM32工程移植:不同型号芯片工程之间的移植:ZE到C8

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

为什么要有包装类,顺便说一说基本数据类型、包装类、String类该如何转换?

Introduction to metalama 4 Use fabric to manipulate items or namespaces
随机推荐
【vulnhub靶场】-dc2
IDEA 数据库插件Database Navigator 插件
Why is the premise of hash% length = = hash & (length-1) that length is the nth power of 2
Idea code quality specification plug-in sonarlint
What is a gateway
洛谷P5540 [BalkanOI2011] timeismoney | 最小乘积生成树 题解
I changed to a programmer at the age of 31. Now I'm 34. Let me talk about my experience and some feelings
电脑系统卡如何解决?
天梯赛赛前练习
Array---
Why is there a wrapper class? By the way, how to convert basic data types, wrapper classes and string classes?
消息队列概述
Fastjson 2 来了,性能继续提升,还能再战十年
如果你是一个Golang面试官,你会问哪些问题?
Web17——EL与JSTL的使用
Number of nodes of complete binary tree
在 VSCode 中调试 Jest 的测试用例,VSCode调试Jest测试用例报错basedir=$(dirname “$(echo “$0“ | sed -e ‘s,\\,/,g‘)“)解决
Symmetric encryption, certificate encryption
基于卷积神经网络的遥感影像分类识别系统
Idea database navigator plug-in