• CSS水平居中/垂直居中的N个方法

    2013-09-26 185阅读 0评论

    我看最近微博流行CSS居中技术,老外码农争相写相关的文章,一篇赛一篇的长啊,我把几篇归纳总结了一下,算是笔记。

    孔乙己曾说:"茴香豆的回字有四种写法",万一哪天有个面试官问你:"居中一共有几种写法"呢,哈哈,先备着吧~~各种方法各有利弊,大家自己权衡吧,至少在需要居中时多个思路。

    <center>

    不建议用了。

    text-align:center

    在父容器里水平居中 inline 文字,或 inline 元素

    vertical-align:middle

    垂直居中 inline 文字,inline 元素,配合 DE>display:tableDE> ,DE>display:table-cellDE>,有奇效。

    line-height

    与 height 联手,垂直居中文字

    margin:auto

    复制代码
    <style>
      #ex2_container { width:200px; background-color:yellow; }
      #ex2_content { margin:0px auto; background-color:gray; color:white; display:table; }
    </style>
    <div id="ex2_container"><div id="ex2_content">Hello World</div></div>
    复制代码

     

    hacks, hacks(小技巧)

    有许多 hacks ,负 margin,影子元素 ::before 等。如果你的内容不是固定大小的话,它们大部分是很脆弱的。

    translate(-50%,-50%)

    用 position 加 translate translate(-50%,-50%) 比较奇特,百分比计算不是以父元素为基准,而是以自己为基准。

    示例:

    复制代码
     1 <style>
     2 #ex3_container{
     3 width:200px;
     4 height:200px;
     5 background-color:yellow;
     6 position:relative;
     7 }
     8 #ex3_content{
     9 left:50%; top:50%; 
    10 transform:translate(-50%,-50%);
    11 -webkit-transform:translate(-50%,-50%);
    12 background-color:gray; color:white; position:absolute;
    13 }
    14 </style>
    15 <div id="ex3_container"><div id="ex3_content">Hello World</div></div>
    复制代码

     

    这个技巧相当嚣张,同样适用于没固定大小的内容,DE>min-widthDE>,DE>max-heightDE>,DE>overflow:scrollDE>等。

    绝对定位居中

    父容器元素:DE>position: relativeDE>

    复制代码
    .Absolute-Center {
      width: 50%;
      height: 50%;
      overflow: auto;
      margin: auto;
      position: absolute;
      top: 0; left: 0; bottom: 0; right: 0;
    }
    复制代码

     

    注意:高度必须定义,建议加 DE>overflow: autoDE>,防止内容溢出。

    视口居中

    内容元素:DE>position: fixedDE>,DE>z-index: 999DE>,记住父容器元素 DE>position: relativeDE>

    复制代码
    1 .Absolute-Center.is-Fixed {
    2   width: 50%;
    3   height: 50%;
    4   overflow: auto;
    5   margin: auto;
    6   position: fixed;
    7   top: 0; left: 0; bottom: 0; right: 0;
    8   z-index: 999;
    9 }
    复制代码

     

    响应式

    百分比宽高,最大、最小宽度均可以,加 padding 也可以

    复制代码
     1 .Absolute-Center.is-Responsive {
     2   width: 60%;
     3   height: 60%;
     4   min-width: 400px;
     5   max-width: 500px;
     6   padding: 40px;
     7   overflow: auto;
     8   margin: auto;
     9   position: absolute;
    10   top: 0; left: 0; bottom: 0; right: 0;
    11 }
    复制代码

    偏移

    只要 DE>margin: auto;DE> 在,内容块将垂直居中,top, left, bottom, right 可以设置偏移。

    复制代码
    1 .Absolute-Center.is-Right {
    2   width: 50%;
    3   height: 50%;
    4   margin: auto;
    5   overflow: auto;
    6   position: absolute;
    7   top: 0; left: auto; bottom: 0; right: 20px;
    8   text-align: right;
    9 }
    复制代码

    溢出

    居中内容比父容器高时,防止溢出,加 DE>overflow: autoDE> (没有任何 padding 时,也可以加 DE>max-height: 100%;DE>)。

    复制代码
    1 .Absolute-Center.is-Overflow {
    2   width: 50%;
    3   height: 300px;
    4   max-height: 100%;
    5   margin: auto;
    6   overflow: auto;
    7   position: absolute;
    8   top: 0; left: 0; bottom: 0; right: 0;
    9 }
    复制代码

     

    调整尺寸

    resize 属性可以让尺寸可调。 设置 min- /max- 限制尺寸,确定加了 DE>overflow: autoDE> 。

    复制代码
     1 .Absolute-Center.is-Resizable {
     2   min-width: 20%;
     3   max-width: 80%;
     4   min-height: 20%;
     5   max-height: 80%;
     6   resize: both;
     7   overflow: auto;
     8   margin: auto;
     9   position: absolute;
    10   top: 0; left: 0; bottom: 0; right: 0;
    11 }
    复制代码

    图像

    图像同样适用,设置 DE>height: auto;DE>

    复制代码
    1 .Absolute-Center.is-Image {
    2   width: 50%;
    3   height: auto;
    4   margin: auto;
    5   position: absolute;
    6   top: 0; left: 0; bottom: 0; right: 0;
    7 }
    复制代码

    可变高度

    高度必须定义,但可以是百分比或 max-height。不想定义高度的话,用 DE>display: tableDE> (需要考虑 Table-Cell 兼容性)。

    复制代码
    1 .Absolute-Center.is-Variable {
    2   display: table;
    3   width: 50%;
    4   overflow: auto;
    5   margin: auto;
    6   position: absolute;
    7   top: 0; left: 0; bottom: 0; right: 0;
    8 }
    复制代码

    负 margin

    确切知道宽高,负 margin 是宽和高的一半。

    复制代码
    1 .is-Negative {
    2 width: 300px;
    3 height: 200px;
    4 padding: 20px;
    5 position: absolute;
    6 top: 50%; left: 50%;
    7 margin-left: -170px; /* (width + padding)/2 */
    8 margin-top: -120px; /* (height + padding)/2 */
    9 }
    复制代码

    Table-Cell

    参考文章:Flexible height vertical centering with CSS, beyond IE7

    HTML结构:

    复制代码
    1 <div class="Pos-Container is-Table">
    2   <div class="Table-Cell">
    3     <div class="Center-Block">
    4     &lt!-- CONTENT -->
    5     </div>
    6   </div>
    7 </div>
    复制代码

     

    CSS样式:

    复制代码
    1 .Pos-Container.is-Table { display: table; }
    2 .is-Table .Table-Cell {
    3   display: table-cell;
    4   vertical-align: middle;
    5 }
    6 .is-Table .Center-Block {
    7   width: 50%;
    8   margin: 0 auto;
    9 }
    复制代码

     

    FlexBox

    参考文章:Designing CSS Layouts With Flexbox Is As Easy As Pie

    复制代码
     1 .Pos-Container.is-Flexbox {
     2   display: -webkit-box;
     3   display: -moz-box;
     4   display: -ms-flexbox;
     5   display: -webkit-flex;
     6   display: flex;
     7   -webkit-box-align: center;
     8      -moz-box-align: center;
     9      -ms-flex-align: center;
    10   -webkit-align-items: center;
    11           align-items: center;
    12   -webkit-box-pack: center;
    13      -moz-box-pack: center;
    14      -ms-flex-pack: center;
    15   -webkit-justify-content: center;
    16           justify-content: center;
    17 }
    复制代码

     

    posted @   webnote  阅读(185)  评论(0编辑  收藏  举报
    我要评论

    编辑推荐:
    · .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
    · 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
    · 一次Java后端服务间歇性响应慢的问题排查记录
    · dotnet 源代码生成器分析器入门
    · ASP.NET Core 模型验证消息的本地化新姿势
    阅读排行:
    · 开发的设计和重构,为开发效率服务
    · 从零开始开发一个 MCP Server!
    · .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
    · Ai满嘴顺口溜,想考研?浪费我几个小时
    · ThreeJs-16智慧城市项目(重磅以及未来发展ai)
    点击右上角即可分享
    微信分享提示