zoukankan      html  css  js  c++  java
  • CSS居中布局

    • 水平居中

      • 行内元素: text-align: center
      • 块级元素: margin: 0 auto
      • relative + transform
      • flex + justify-content: center(考虑兼容性)
    • 垂直居中

      • line-height: height
      • relative + transform
      • flex + align-items: center(考虑兼容性)
    • 水平垂直居中

      • relative + transform
      • relative + top50% left50% + margin自身宽度一半
      • flex + justify-content + align-items(考虑兼容性)

     水平居中   

        常见行内元素: h1 - h6,p,div ,ol ,ul,table ,form 等

        常见块级元素:a,b(粗体),br ,font ,image,input,label ,strong ,textarea 等

     行内元素:

    //实现文本居中
    p
    { text-align : center; }
    //实现div居中,子元素inline-block
    .parent{
      text-align:center
    }
    .son{
     display:inline-block
    }
        块级元素:

    //上下边距为0 左右边距自动
    #center{
         margin : 0 auto;
    }

    flex实现水平居中(考虑浏览器兼容性):在父元素中设置flex

    #center{
        display : flex;
        justify-content: center;  /*水平居中*/        
    }

     垂直居中     

    flex实现垂直居中(考虑浏览器兼容性):在父元素中设置flex

    #center{
        display : flex;
        align-items : center; /*垂直居中*/        
    }

    line-height实现垂直居中:line-height:height;

    水平垂直居中      

     relative + transform实现居中:

    #center{
     position : relative;
    top : 50%;
    left : 50%;
    transform : translate(-50%,-50%); /*x,y轴移动父元素的一半*/
    //margin : -50px 0 0 -150px; /*可替换
    transform 长度为容器宽高的一半*/
    }

    flex实现水平垂直居中(考虑浏览器兼容性):在父元素中设置flex

    #center{
        display : flex;
        justify-content: center;  //水平居中
        align-items : center;    //垂直居中  
    }
  • 相关阅读:
    @Autowired和@Resource的区别是什么?
    关于事务,事务的特性,spring事务的传播特性
    Java 用Freemarker完美导出word文档(带图片)
    关于 MySQL查询当天、本周,本月,上一个月的数据
    js如何使用radio
    Freemarker提供了3种加载模板目录的方法
    190707Python-MySQL
    190707Python-RabbitMQ
    190707select和selector模块
    4、kubernetes资源清单快速入门190625
  • 原文地址:https://www.cnblogs.com/barryzhang/p/10394689.html
Copyright © 2011-2022 走看看