美文网首页
Flex垂直居中(或其他)

Flex垂直居中(或其他)

作者: StarLikeRain | 来源:发表于2018-09-12 09:56 被阅读19次
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            html,
            body,
            div,
            span,
            applet,
            object,
            iframe,
            h1,
            h2,
            h3,
            h4,
            h5,
            h6,
            p,
            blockquote,
            pre,
            a,
            abbr,
            acronym,
            address,
            big,
            cite,
            code,
            del,
            dfn,
            em,
            img,
            ins,
            kbd,
            q,
            s,
            samp,
            small,
            strike,
            strong,
            sub,
            sup,
            tt,
            var,
            b,
            u,
            i,
            center,
            dl,
            dt,
            dd,
            ol,
            ul,
            li,
            fieldset,
            form,
            label,
            legend,
            table,
            caption,
            tbody,
            tfoot,
            thead,
            tr,
            th,
            td,
            article,
            aside,
            canvas,
            details,
            embed,
            figure,
            figcaption,
            footer,
            header,
            hgroup,
            main,
            menu,
            nav,
            output,
            ruby,
            section,
            summary,
            time,
            mark,
            audio,
            video {
                margin: 0;
                padding: 0;
                border: 0;
                font-size: 100%;
                font: inherit;
                vertical-align: baseline;
            }
    
            /* HTML5 display-role reset for older browsers */
            article,
            aside,
            details,
            figcaption,
            figure,
            footer,
            header,
            hgroup,
            main,
            menu,
            nav,
            section {
                display: block;
            }
    
            /* HTML5 hidden-attribute fix for newer browsers */
            *[hidden] {
                display: none;
            }
    
            body {
                line-height: 1;
            }
    
            ol,
            ul {
                list-style: none;
            }
    
            blockquote,
            q {
                quotes: none;
            }
    
            blockquote:before,
            blockquote:after,
            q:before,
            q:after {
                content: '';
                content: none;
            }
    
            table {
                border-collapse: collapse;
                border-spacing: 0;
            }
    
            body {
                margin: 0 0 2rem 2rem;
                width: 90%;
                height: 20rem;
                border: 2px solid seagreen;
                display: inline-flex;
                flex-direction: row;
                flex-wrap: wrap;
                justify-content: center;
                align-items: center;
            }
    
            div {
                width: 200px;
                height: 200px;
                background-color: coral;
                border: 1px solid black
            }
        </style>
    </head>
    
    <body>
        <div class="main">
            1
        </div>
        <div class="vice">2</div>
    
        <div class="cao">3</div>
        <!-- <div class="cao">4</div>
        <div class="cao">5</div>
        <div class="cao">6</div>
        <div class="cao">7</div>
        <div class="cao">8</div>
        <div class="cao">9</div>
        <div class="cao">10</div>
        <div class="cao">11</div>
        <div class="cao">12</div> -->
    </body>
    
    </html>
    
    image.png
    // 用伪类
    .middle {
      text-align:center;
    }
    .middle:before {
      content:'';
      display:inline-block;
      height:100%;
      vertical-align: middle;
    }
    // 比如是<image  class="middle img"/>给加了middle 的 class,需要再弄一个
    .img {
      vertical-align: middle;
    }
    
    // 用表格来渲染 (不太好)
    .box {
      width:100px;
      height:100px;
      border:1px solid black;
      display: table-cell;
      vertical-align: middle;
      text-align: center;
    }
    
    // 用css3 的transform
    .dialog {
      width: 100px;
      height: 100px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
    

    相关文章

      网友评论

          本文标题:Flex垂直居中(或其他)

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