CSS 元素水平,垂直居中

HTML

    <div class="father">
        <div class="son"></div>
    </div>

1、利用 absolutetranslate

CSS

        .father{
            position: relative;
            margin: 0 auto;
            width: 200px;
            height: 200px;
            background: red;
        }

        .son{
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 100px;
            height: 100px;
            background: blue;
        }

2、利用 absolute 和 负的margin

CSS

        .father{
            position: relative;
            margin: 0 auto;
            width: 200px;
            height: 200px;
            background: red;
        }

        .son{
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -50px;
            margin-left: -50px;
            width: 100px;
            height: 100px;
            background: blue;
        }

3、利用 absolutetop right bottom leftmargin: auto

CSS

        .father{
            position: relative;
            margin: 0 auto;
            width: 200px;
            height: 200px;
            background: red;
        }

        .son{
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            margin: auto;
            width: 100px;
            height: 100px;
            background: blue;
        }

4、利用 display: tabledisplay: table-cell

(1)display: tablepadding 会失效
(2)display: table-rowmargin、padding 同时失效
(3)display: table-cellmargin 会失效
(4) 表格中的单元格中的 div 设置宽度无效,是因为 div 被设置为 display: table-cell ,后将其修改为 display: block 则设置的宽度生效

CSS

        .father{
            display: table-cell;
            text-align: center;
            vertical-align: middle;
            width: 200px;
            height: 200px;
            background: red;
        }

        .son{
            display: inline-block;
            width: 100px;
            height: 100px;
            background: blue;
        }

效果

table-cell 居中

5、通过 font-size 实现居中

IE7 以下有效

font-size 值为 height / 1.14

HTML

    <div class="father">
        <span class="son"></span>
    </div>

CSS

        .father{
            width: 200px;
            height: 200px;
            font-size: 175.4px;  /* height / 1.14 */
            text-align: center;
            background: red;
        }

        .son{
            display: inline-block;
            zoom: 1;
            vertical-align: middle; /*inline 和 inline-block 才能设置该属性*/
            font-size: 12px; /* 初始化字体大小 */
            width: 100px;
            height: 100px;
            background: blue;
        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,814评论 1 92
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,749评论 1 45
  • 一 外部式css样式 (也可称为外联式)就是把css代码写一个单独的外部文件中,这个css样式文件以“.css...
    KunMitnic阅读 965评论 0 1
  • 本课来自http://www.imooc.com/learn/9请不要用作商业用途。 HTML5 HTML介绍 H...
    PYLON阅读 3,320评论 0 5
  • 〔诗/李蕴珂〕 年,春的使者 从远古穿越千年 从来未老 三百六十五天再轮回 又是新年 年,总在寒冬过后 应春之邀 ...
    区块链代币名称设计阅读 273评论 0 4