美文网首页
垂直居中的几种方法

垂直居中的几种方法

作者: 前端开发工程师老唐 | 来源:发表于2018-12-24 14:50 被阅读0次

1css定位

    <style>
        .wrap {
            position: relative;
            background: #333;
            height: 300px;
            width: 300px;
        }
        .box {
            position: absolute;
            top: 50%;
            left: 50%;
            background: #999;
            width: 100px;
            height: 100px;
            margin: -50px 0 0 -50px;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="box">
        </div>
    </div>
</body>
image.png

适用于父容器和子容器的宽高是确定的。

2.transition

    <style>
        .wrap {
            position: relative;
            background: #333;
            height: 300px;
            width: 300px;
        }
        .box {
            position: absolute;
            top: 50%;
            left: 50%;
            background: #999;
            width: 100px;
            height: 100px;
            transform: translate(-50%, -50%)
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="box">
        </div>
    </div>
</body>
image.png

利用c3的动画属性

3.强大的flex

    <style>
        .wrap {
            position: relative;
            background: #333;
            height: 300px;
            width: 300px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .box {
            background: #999;
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="box">
        </div>
    </div>
</body>
image.png

定义父容器为flex容器,添加设置主轴和次主轴居中 justify-content: center; align-items: center;代码简洁优雅

4.设置table的属性,模拟表格定位

    <style>
        .table {
            display: table;
            height: 300px;
            background: #aaa;
        }

        .cell {
            display: table-cell;
            width: 300px;
            background: #999;
            vertical-align: middle;
            text-align: center;
        }
        .img {
            width: 100px;
            height: 100px;
            background: #333;
            display: inline-block;
        }
    </style>
</head>
<body>
    <div class="table">
        <div class="cell">
            <div class="img">
            </div>
        </div>
    </div>
</body>

页面看成一个表格的思想,也很不错


用table完成一个两边固定,中间自适应(图片居中)的经典布局:

  <style>
        .table {
            display: table;
            width: 100%;
            background: #aaa;
        }

        .cell {
            display: table-cell;
            /* background: #999; */
            vertical-align: middle;
            text-align: center;
        }
        .table .left,.table .right {
            width: 300px;
            height: 300px;
            background: burlywood;
        }
        .img {
            width: 100px;
            height: 100px;
            background: #333;
            display: inline-block;
        }
    </style>
</head>
<body>
    <div class="table">
        <div class="cell left"></div>
        <div class="cell">
            <div class="img">
            </div>
        </div>
        <div class="cell right"></div>
    </div>
</body>
image.png

相关文章

  • 页面中垂直居中的几种实现方法

    页面中垂直居中的几种方法: 1. 通过使用absolute定位来实现垂直居中 HTML: CSS: 2. 使用t...

  • 几种垂直居中的方法

    如果父容器不固定高度,padding/line-height(慎用)即可实现元素的垂直居中 如果父容器固定高度 d...

  • 垂直居中

    垂直居中是我们在使用css做页面时常见的需求,以下列举几种垂直居中的实现方法: 1. 使用父元素内边距设置实现居中...

  • 垂直居中方法汇总

    做页面时经常会碰到垂直居中问题,下面几种方法可以根据不同场合选着相对应的合适的方法来使用。 实现单行文字垂直居中,...

  • 不定宽高的div盒子和不定宽高的盒子水平垂直居中

    让一个不定宽高的div,垂直水平居中的几种实现方式 不定宽高的div垂直居中的方式: 1、使用CSS方法: 父盒子...

  • 常用的居中方法

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

  • 水平居中和垂直居中

    本章介绍几种常见的水平居中和垂直居中的实现方式

  • CSS 的几种典型居中

    CSS 中,有几种经典的居中,水平居中,垂直居中。其中水平居中比较常见,也相对比较简单;垂直居中相对少见,但是也很...

  • css3 元素居中的几个方法

    原文参考 纯CSS实现垂直居中的几种方法 html 当元素为 block时 position position +...

  • 垂直居中的几种方法

    1css定位 适用于父容器和子容器的宽高是确定的。 2.transition 利用c3的动画属性 3.强大的fle...

网友评论

      本文标题:垂直居中的几种方法

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