首页 web前端 css教程 css让容器水平垂直居中的7种方式

css让容器水平垂直居中的7种方式

May 21, 2017 am 10:31 AM
css 垂直 居中 水平

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

这种css布局平时用的比较多,也是面试题常出的一个题,网上一搜一大丢,不过还是想自己总结一下。
这种方法比较多,本文只总结其中的几种,以便加深印象。
效果图都为这个:

方法一:position加margin


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

  1. <p class="wrap">  

  2.     <p class="center">p>  

  3. p>  

  4.   


CSS Code复制内容到剪贴板

  1. /**css**/ .wrap { width200pxheight200pxbackgroundyellowpositionrelative;   

  2. } .wrap .center { width100pxheight100pxbackgroundgreenmarginautopositionabsoluteleft: 0; rightright: 0; top: 0; bottombottom: 0;   

  3. }  

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

方法二:diaplay:table-cell


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

  1.   

  2. <p class="wrap">  

  3.      <p class="center">p>  

  4. p>  

  5.   


CSS Code复制内容到剪贴板

  1. /*css*/ .wrap{ width200pxheight200pxbackgroundyellowdisplaytable-cellvertical-alignmiddletext-aligncenter;   

  2. } .centerdisplayinline-blockvertical-alignmiddlewidth100pxheight100pxbackgroundgreen;   

  3. }   

  4.   

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

方法三:position加 transform


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

  1.   

  2. <p class="wrap">  

  3.     <p class="center">p>  

  4. p>  

  5.   


CSS Code复制内容到剪贴板

  1. /* css */ .wrap { positionrelativebackgroundyellowwidth200pxheight200px;} .center { positionabsolutebackgroundgreentop:50%; left:50%; -webkit-transform:translate(-50%,-50%); transform:translate(-50%,-50%); width100pxheight100px;   

  2. }   

  3.   

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

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


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

  1.   

  2. <p class="wrap">  

  3.     <p class="center">p>  

  4. p>  

  5.   


CSS Code复制内容到剪贴板

  1. /* css */ .wrap { backgroundyellowwidth200pxheight200pxdisplay: flex; align-items: centerjustify-contentcenter;   

  2. } .center { backgroundgreenwidth100pxheight100px;   

  3. }   

  4.   

移动端首选

方法五:display:flex;margin:auto


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

  1.   

  2. <p class="wrap">  

  3.     <p class="center">p>  

  4. p>  


CSS Code复制内容到剪贴板

  1. /* css */ .wrap { backgroundyellowwidth200pxheight200pxdisplay: flex;    

  2. } .center { backgroundgreenwidth100pxheight100pxmarginauto;   

  3. }   

  4.   

移动端首选

方法六:纯position


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

  1.   

  2. <p class="wrap">  

  3.     <p class="center">p>  

  4. p>  

  5.   


CSS Code复制内容到剪贴板

  1. /* css */ .wrap { backgroundyellowwidth200pxheight200pxpositionrelative;   

  2. /**方法一**/ .center { backgroundgreenpositionabsolutewidth100pxheight100pxleft50pxtop50px;    

  3.      

  4. /**方法二**/ .center { backgroundgreenpositionabsolutewidth100pxheight100pxleft: 50%; top: 50%; margin-left:-50pxmargin-top:-50px;   

  5. }   

  6.   

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

方法六中的方法一计算公式如下:
子元素(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复制内容到剪贴板

  1.   

  2. <p class="table">  

  3.     <p class="tableCell">  

  4.         <p class="content">不固定宽高,自适应p>  

  5.     p>  

  6. p>  

  7.   


CSS Code复制内容到剪贴板

  1. /*css*/ .table { height200px;/*高度值不能少*/ width200px;/*宽度值不能少*/ display: table; positionrelativefloat:leftbackgroundyellow;   

  2. } .tableCell { displaytable-cellvertical-alignmiddletext-aligncenter;           

  3.     *positionabsolutepadding10px;   

  4.     *top: 50%;   

  5.     *left: 50%;   

  6. } .content {   

  7.     *position:relative;   

  8.     *top: -50%;   

  9.     *left: -50%; backgroundgreen;   

  10. }   

  11.   

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

总结

如果是移动端,那么用方法四和方法五是比较方便的。而且支持不固定宽高的情况,快、准、狠
也就是用 flex; align-items: center; justify-content: center;


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

  1.   

  2. <p class="wrap">  

  3.     <p class="center">p>  

  4. p>  

  5.   


CSS Code复制内容到剪贴板

  1. /* css */ .wrap { backgroundyellowwidth200pxheight200pxdisplay: flex; align-items: centerjustify-contentcenter;   

  2. } .center { backgroundgreenwidth100pxheight100px;   

  3. }   

  4.   

或者display:flex;margin:auto;


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

  1.   

  2. <p class="wrap">  

  3.     <p class="center">p>  

  4. p>  

  5.   


CSS Code复制内容到剪贴板

  1. /* css */ .wrap { backgroundyellowwidth200pxheight200pxdisplay: flex;   

  2. } .center { backgroundgreenwidth100pxheight100pxmarginauto;   

  3. }   

  4.   

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


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

  1.   

  2. <p class="wrap">  

  3.     <p class="center">p>  

  4. p>  

  5.   


CSS Code复制内容到剪贴板

  1. /* css */ .wrap { backgroundyellowwidth200pxheight200pxpositionrelative;   

  2. /**方法一**/ .center { backgroundgreenpositionabsolutewidth100pxheight100pxleft50pxtop50px;     

  3.      

  4. /**方法二**/ .center { backgroundgreenpositionabsolutewidth100pxheight100pxleft: 50%; top: 50%; margin-left:-50pxmargin-top:-50px;   

  5. }   

  6.   

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

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

以上是css让容器水平垂直居中的7种方式的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

vue中怎么用bootstrap vue中怎么用bootstrap Apr 07, 2025 pm 11:33 PM

在 Vue.js 中使用 Bootstrap 分为五个步骤:安装 Bootstrap。在 main.js 中导入 Bootstrap。直接在模板中使用 Bootstrap 组件。可选:自定义样式。可选:使用插件。

HTML,CSS和JavaScript的角色:核心职责 HTML,CSS和JavaScript的角色:核心职责 Apr 08, 2025 pm 07:05 PM

HTML定义网页结构,CSS负责样式和布局,JavaScript赋予动态交互。三者在网页开发中各司其职,共同构建丰富多彩的网站。

bootstrap怎么写分割线 bootstrap怎么写分割线 Apr 07, 2025 pm 03:12 PM

创建 Bootstrap 分割线有两种方法:使用 标签,可创建水平分割线。使用 CSS border 属性,可创建自定义样式的分割线。

bootstrap怎么调整大小 bootstrap怎么调整大小 Apr 07, 2025 pm 03:18 PM

要调整 Bootstrap 中元素大小,可以使用尺寸类,具体包括:调整宽度:.col-、.w-、.mw-调整高度:.h-、.min-h-、.max-h-

bootstrap怎么设置框架 bootstrap怎么设置框架 Apr 07, 2025 pm 03:27 PM

要设置 Bootstrap 框架,需要按照以下步骤:1. 通过 CDN 引用 Bootstrap 文件;2. 下载文件并将其托管在自己的服务器上;3. 在 HTML 中包含 Bootstrap 文件;4. 根据需要编译 Sass/Less;5. 导入定制文件(可选)。设置完成后,即可使用 Bootstrap 的网格系统、组件和样式创建响应式网站和应用程序。

bootstrap怎么插入图片 bootstrap怎么插入图片 Apr 07, 2025 pm 03:30 PM

在 Bootstrap 中插入图片有以下几种方法:直接插入图片,使用 HTML 的 img 标签。使用 Bootstrap 图像组件,可以提供响应式图片和更多样式。设置图片大小,使用 img-fluid 类可以使图片自适应。设置边框,使用 img-bordered 类。设置圆角,使用 img-rounded 类。设置阴影,使用 shadow 类。调整图片大小和位置,使用 CSS 样式。使用背景图片,使用 background-image CSS 属性。

了解HTML,CSS和JavaScript:初学者指南 了解HTML,CSS和JavaScript:初学者指南 Apr 12, 2025 am 12:02 AM

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

bootstrap按钮怎么用 bootstrap按钮怎么用 Apr 07, 2025 pm 03:09 PM

如何使用 Bootstrap 按钮?引入 Bootstrap CSS创建按钮元素并添加 Bootstrap 按钮类添加按钮文本

See all articles