zoukankan      html  css  js  c++  java
  • 多种方式垂直水平居中

    1. 不需要知道父容器和子容器的具体尺寸

     (1)方法一:margin

     <div class="wrapper">
            <div class="content"></div>
        </div>
    .wrapper {
        display: table-cell;
        width: 500px;
        height: 500px;
        text-align: center; /*水平居中*/
        border: 1px solid #eee;
        vertical-align: middle; /*图片垂直居中*/
        position: relative;
        
    } 
    .content {
        width: 200px;
        height: 200px;
        background-color: red;
        margin: auto;
    }

       (2)方法二:绝对定位 + transform, 应用到了css3知识,需要注意兼容性问题

    <div class="wrapper">
            <div class="content"></div>
        </div>
    .wrapper {
        display: block;
        width: 500px;
        height: 500px;
        border: 1px solid #eee;
        position: relative;
        
    } 
    .content {
        width: 200px;
        height: 200px;
        background-color: red;
        position: absolute;
        left: 50%;
        right: 50%;
       transform: translate(-50%, -50%);
    }

      (3)flex弹性盒子,应用到了css3知识,需要注意兼容性问题

    <div class="wrapper">
            <div class="content"></div>
        </div>
    .wrapper {
        width: 500px;
        height: 500px;
        border: 1px solid #eee;
        display:flex;
        justify-content:center;// 主轴(一般为横轴)的排列方式
        align-items: center;  // 纵轴(一般为竖轴)的排列方式
    } 
    .content {
        width: 200px;
        height: 200px;
        background-color: red;
    }

    2.已知宽高尺寸

    <div class="wrapper">
            <div class="content"></div>
        </div>
    .wrapper {
        width: 500px;
        height: 500px;
        border: 1px solid #eee;
        position: relative;
    } 
    .content {
        width: 200px;
        height: 200px;
        background-color: red;
        position: absolute;
        left: 50%;
        top: 50%;
        /*自身宽高一半的负值*/
        margin-left: -100px;
        margin-top: -100px;
    }

    3.文字水平垂直居中

      (1)单行文字

      水平居中在父级元素添加text-align: center;

      垂直居中在父级元素添加和父级元素高度一样的line-height

      (2)多行文字

      水平居中在父级元素添加text-align: center;

      垂直居中在父级元素添加一定的行高和padding设置为xx 0;

      

  • 相关阅读:
    测试软件—禅道BUG管理工具
    C语言 线性表的操作~(未完)
    数据库考纲~
    圣杯布局和双飞翼布局总局
    总结布局用法
    springboot~入门第三篇~与mybatis整合~(未完)
    微信小程序里 wx:for和wx:for-item区别(补充下wx:key)
    对比下小程序语法和Vue语法异同
    视频转换 rtsp 流 转rtmp流播放(待完善)
    Vue钩子函数~
  • 原文地址:https://www.cnblogs.com/Walker-lyl/p/5854267.html
Copyright © 2011-2022 走看看