惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - 君临天下之徐少

nvm安装 Homebrew 安装 Less的优点 什么是nrm js 处理大数相减 React函数式组件值之useRef()和useImperativeHandle() 谷歌刷题插件安装 SSL certificate problem: unable to get local issuer certificate 错误解决 常用命令 Mac安装Nvm Node开发环境 解决Mac安装Homebrew失败 Canvas学习:globalCompositeOperation详解 React.CreateContext js下载文件防止白屏 GitHub上README.md编写教程(基本语法) 微信字体大小调整导致的H5页面错乱问题处理 G6-Editor 编辑器入门使用教程 git reset 加不加 --hard的区别 taro, h5拨打电话和发送短信
html让容器居中,css让容器水平垂直居中的7种方式
君临天下之徐少 · 2022-03-09 · via 博客园 - 君临天下之徐少

这篇文章主要为大家详细介绍了css让容器水平垂直居中的7种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

这种css布局平时用的比较多,也是面试题常出的一个题,网上一搜一大丢,不过还是想自己总结一下。

这种方法比较多,本文只总结其中的几种,以便加深印象。

效果图都为这个:

方法一:position加margin

XML/HTML Code复制内容到剪贴板, CSS Code复制内容到剪贴板/**css**/ 

.wrap { width: 200px; height: 200px; background: yellow; position: relative;}
.wrap .center { width: 100px; height: 100px; background: green; margin: auto; position: absolute; left: 0; rightright: 0; top: 0; bottombottom: 0;}

兼容性:主流浏览器均支持,IE6不支持

方法二:diaplay:table-cell

XML/HTML Code复制内容到剪贴板, CSS Code复制内容到剪贴板/*css*/

.wrap{ width: 200px; height: 200px; background: yellow; display: table-cell; vertical-align: middle; text-align: center;}
.center{ display: inline-block; vertical-align: middle; width: 100px; height: 100px; background: green;}

兼容性:由于display:table-cell的原因,IE67不兼容

方法三:position加 transform

XML/HTML Code复制内容到剪贴板, CSS Code复制内容到剪贴板/* css */

.wrap { position: relative; background: yellow; width: 200px; height: 200px;} 
.center { position: absolute; background: green; top:50%; left:50%; -webkit-transform:translate(-50%,-50%); transform:translate(-50%,-50%); width: 100px; height: 100px;}

兼容性:ie9以下不支持 transform,手机端表现的比较好。

方法四:flex;align-items: center;justify-content: center

XML/HTML Code复制内容到剪贴板, CSS Code复制内容到剪贴板/* css */ 

.wrap { background: yellow; width: 200px; height: 200px; display: flex; align-items: center; justify-content: center;} 
.center { background: green; width: 100px; height: 100px;}

移动端首选

方法五:display:flex;margin:auto

XML/HTML Code复制内容到剪贴板, CSS Code复制内容到剪贴板/* css */ 

.wrap { background: yellow; width: 200px; height: 200px; display: flex;} 
.center { background: green; width: 100px; height: 100px; margin: auto;}

移动端首选

方法六:纯position

XML/HTML Code复制内容到剪贴板, CSS Code复制内容到剪贴板/* css */ 

.wrap { background: yellow; width: 200px; height: 200px; position: relative;}
 /**方法一**/
.center { background: green; position: absolute; width: 100px; height: 100px; left: 50px; top: 50px;}
 /**方法二**/ 
.center { background: green; position: absolute; width: 100px; height: 100px; left: 50%; top: 50%; margin-left:-50px; margin-top:-50px;}

兼容性:适用于所有浏览器

方法六中的方法一计算公式如下:

子元素(conter)的left值计算公式:left=(父元素的宽 - 子元素的宽 ) / 2=(200-100) / 2=50px;

子元素(conter)的top值计算公式:top=(父元素的高 - 子元素的高 ) / 2=(200-100) / 2=50px;

方法二计算公式:

left值固定为50%;

子元素的margin-left= -(子元素的宽/2)=-100/2= -50px;

top值也一样,固定为50%

子元素的margin-top= -(子元素的高/2)=-100/2= -50px;

方法七:兼容低版本浏览器,不固定宽高

XML/HTML Code复制内容到剪贴板

不固定宽高,自适应

CSS Code复制内容到剪贴板/*css*/

.table { height: 200px;/*高度值不能少*/ width: 200px;/*宽度值不能少*/ display: table; position: relative; float:left; background: yellow;} .tableCell { display: table-cell; vertical-align: middle; text-align: center; position: absolute; padding: 10px; top: 50%; left: 50%;} .content { position:relative; top: -50%; left: -50%; background: green;}

暂时总结上面的七种,这种方法太多,其实只要习惯了其中的一两种也就够用了。

总结

如果是移动端,那么用方法四和方法五是比较方便的。而且支持不固定宽高的情况,快、准、狠

也就是用 flex; align-items: center; justify-content: center;

XML/HTML Code复制内容到剪贴板, CSS Code复制内容到剪贴板/* css */ 

.wrap { background: yellow; width: 200px; height: 200px; display: flex; align-items: center; justify-content: center;}
.center { background: green; width: 100px; height: 100px;}

或者display:flex;margin:auto;

XML/HTML Code复制内容到剪贴板, CSS Code复制内容到剪贴板/* css */ 

.wrap { background: yellow; width: 200px; height: 200px; display: flex;} 
.center { background: green; width: 100px; height: 100px; margin: auto;}

如果是PC端,要考虑兼容性的话。方法六是不错滴,也就是纯position。

XML/HTML Code复制内容到剪贴板, CSS Code复制内容到剪贴板/* css */ 

.wrap { background: yellow; width: 200px; height: 200px; position: relative;}
 /**方法一**/ 
.center { background: green; position: absolute; width: 100px; height: 100px; left: 50px; top: 50px;}
 /**方法二**/ 
.center { background: green; position: absolute; width: 100px; height: 100px; left: 50%; top: 50%; margin-left:-50px; margin-top:-50px;}

如果PC端的中间的元素高度不固定,那么就用方法七即可,代码就不复制了。

这种css元素垂直的如果真的要总结起来,应该有十几二十几种。不过也没必要全部掌握吧,只要大概了解一些,用起来没有副作用就行。