当前位置:网站首页>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
边栏推荐
- Markdown grammar learning
- QT redraw events and cuts
- uni-app 原生APP-云打包集成极光推送(JG-JPUSH)详细教程
- 一个平面设计师的异想世界|ONES 人物
- 外包干了五年,废了...
- S2-062 远程命令执行漏洞复现(cve-2021-31805)
- 传统企业如何应对数字化转型?这些书给你答案
- XinChaCha Trust SSL Organization Validated
- Introduction to metalama 4 Use fabric to manipulate items or namespaces
- 【vulnhub靶场】-dc2
猜你喜欢
Why is there a wrapper class? By the way, how to convert basic data types, wrapper classes and string classes?
Introduction to metalama 4 Use fabric to manipulate items or namespaces
Tips for installing MySQL service in windows11: Install / Remove of the Service denied
Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward
c# 设置logo图标和快捷方式的图标
大家帮我看一下这是啥情况,MySQL5.5的。谢了
Plato Farm-以柏拉图为目标的农场元宇宙游戏
网站首页文件被攻击篡改的形式有哪些
Idea setting copyright information
QT double buffer drawing
随机推荐
XinChaCha Trust SSL Organization Validated
论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
How does sqlserver insert or update the number of weeks of the day instead of text
The maximum number of remote desktop servers has been exceeded
Qt进程间通信
远程桌面之终端服务器超出了最大允许连接数解决
One way ANOVA of SPSS
网络信息安全之零信任
【每日一题】棋盘问题
RT-thread中关键词解释及部分API
Everything can be expected in the future | one 2022 campus recruitment officially opened
编程辅助工具推荐:图片工具snipaste
数组---
QT double buffer drawing
QT interprocess communication
uni-app 原生APP-云打包集成极光推送(JG-JPUSH)详细教程
php生成json处理中文
如何防止网站被黑客入侵篡改
Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
Fabric 1.0 source code analysis (33) implementation of peer channel command and subcommand