当前位置:网站首页>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
边栏推荐
- How much does software testing help reduce program bugs?
- Why is the premise of hash% length = = hash & (length-1) that length is the nth power of 2
- bert-base-chinese下载(智取)
- Pagoda panel command line help tutorial (including resetting password)
- 软件测试对于减少程序BUG有多大帮助?
- Markdown grammar learning
- Windows11 安装MySQL服务 提示:Install/Remove of the Service Denied
- NPDP|产品经理如何做到不会被程序员排斥?
- Number of nodes of complete binary tree
- 网站首页文件被攻击篡改的形式有哪些
猜你喜欢
Database Navigator 使用默认MySQL连接提示:The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or repres
Zigbee之CC2530最小系统及寄存器配置(1)
S2-062 远程命令执行漏洞复现(cve-2021-31805)
解锁OpenHarmony技术日!年度盛会,即将揭幕!
Next.js 静态数据生成以及服务端渲染的方式
Next. JS static data generation and server-side rendering
Idea code quality specification plug-in sonarlint
S2-062 remote command execution vulnerability recurrence (cve-2021-31805)
IDEA 代码格式化插件Save Actions
Running error: unable to find or load the main class com xxx. Application
随机推荐
S2-062 远程命令执行漏洞复现(cve-2021-31805)
QT redraw events and cuts
Intelligent multi line elastic cloud adds independent IP address. How to realize multi line function?
Windows2008系统如何切换PHP版本
宝塔面板命令行帮助教程(包含重置密码)
Qt一个进程运行另一个进程
Xinwangda announced that the price of battery products had been increased, and the investment of "weixiaoli" exceeded 1 billion
Pre competition practice of TIANTI competition
C set Logo Icon and shortcut icon
Markdown语法学习
Running error: unable to find or load the main class com xxx. Application
第二十六课 类的静态成员函数
【每日一题】棋盘问题
31岁才转行程序员,目前34了,我来说说我的经历和一些感受吧...
第二十五课 类的静态成员变量
uni-app 原生APP-本地打包集成极光推送(JG-JPUSH)详细教程
Lesson 24 analysis of classical problems
IDEA 代码质量规范插件SonarLint
网络信息安全之零信任
Next.js 静态数据生成以及服务端渲染的方式