美文网首页
水平垂直居中

水平垂直居中

作者: abb67685c9b6 | 来源:发表于2018-08-02 15:17 被阅读0次

用到的HTML代码如下:

<div class="parent">

<div class="child"></div>

</div>

一、vertical-align实现水平垂直居中(child无须固定宽高)

vertical-align只对inline-block型起作用,即display为   inline-block   和  table-cell的

1、 display:inline-block

.parent {

            width: 300px;

            line-height: 300px;

            border: 1px solid #b766dd;

            text-align: center;

        }

        .child {

            display: inline-block;

            vertical-align: middle;

            width: 4px;

            height: 4px;

            background: rgb(4, 57, 170);

        }

2、display:table-cell

.parent {

            width: 300px;

            height: 300px;

            border: 1px solid #b766dd;

            display: table-cell;

            vertical-align: middle;         

        }

        .child {

            width: 4px;

            height: 4px;

            background: rgb(4, 57, 170);

            margin: 0 auto;

        }

3、:after伪类

.parent {

            width: 300px;

            height: 300px;

            border: 1px solid #b766dd;

            text-align: center;

        }

        .child {

            width: 4px;

            height: 4px;

            background: rgb(4, 57, 170);

            display: inline-block;

        }

        .parent:after{

            content:"";

            width: 0;

            height: 100%;

            display: inline-block;

            vertical-align: middle;

        }

二、position定位

1、child固定宽高

.parent {

            width: 300px;

            height: 300px;

            border: 1px solid #b766dd;

            position: relative;

        }

        .child {

            width: 4px;

            height: 4px;

            background: rgb(4, 57, 170);

            position: absolute;

            top: 50%;

            left: 50%;

            margin-top: -2px;

            margin-left: -2px;

        }

2、child不固定宽高

.parent {

            width: 300px;

            height: 300px;

            border: 1px solid #b766dd;

            position: relative;

        }

        .child {

            width: 4px;

            height: 4px;

            background: rgb(4, 57, 170);

            position: absolute;

            top: 50%;

            left: 50%;

            transform: translate(-50%,-50%);

        }

三、flex

.parent {

            width: 300px;

            height: 300px;

            border: 1px solid #b766dd;

            display: flex;

            justify-content: center;/*水平*/

            align-items: center;/*垂直*/

        }

        .child {

            width: 4px;

            height: 4px;

            background: rgb(4, 57, 170);

        }

相关文章

  • CSS居中布局方案

    水平居中 垂直居中 水平垂直居中

  • 常用的居中方法

    本文将介绍的居中方法有 ,水平居中,垂直居中和水平垂直居中。 一水平居中 二水平垂直居中

  • 元素居中的方式

    1 水平居中 2 垂直居中 3 水平垂直居中

  • css居中方式总结(亲测有效)

    水平居中(行内元素水平居中、块级元素水平居中) 垂直居中 水平垂直居中 行内元素水平居中 text-align: ...

  • CSS水平垂直居中总结

    CSS水平居中、垂直居中、水平垂直居中方法总结 文字的水平居中: 单行文字的垂直居中: 让有宽度的div水平居中:...

  • 居中布局

    水平居中 垂直居中 垂直水平居中 已知元素的宽高的居中布局 定位居中布局 盒模型居中布局 图片的垂直水平居中(利用...

  • CSS图片居中(水平居中和垂直居中)

    css图片水平居中 css图片垂直居中 css图片水平垂直居中

  • css多行垂直水平居中--table布局大法

    ======= SEO专用 table-cell 定高水平垂直居中 不定高水平垂直居中 单行定高水平垂直居中 单行...

  • CSS居中大全(带截图)

    文字水平居中 图片水平垂直居中 图片与文字水平垂直居中 代码同上 DIV相对于页面垂直居中并且响应式 视口绝对垂直...

  • 居中对齐

    行内元素居中[#hang]垂直居中[#hc]水平居中[#hs] 块级元素居中[#kuai]垂直居中[#kc]水平居...

网友评论

      本文标题:水平垂直居中

      本文链接:https://www.haomeiwen.com/subject/zuhwaxtx.html