• CSS水平垂直居中


    先来一个盒盒(要操作它嘛)

       <div style="height:50px;border:1px solid red;">
            <label style="line-height:50px">单行文本label垂直居中:设置line-height为父级行高</label>
       </div>

    水平垂直居中

    方式一:Flex布局 [推荐]
    优点:内容块的高度任意,优雅的溢出。
    缺点:IE8/IE9不支持,需要浏览器前缀,渲染可能会有问题。

        <div class="hv_flex">
          <div style=" 20px;"></div>
          <div class="hv_flexChild">flex布局垂直水平居中</div>
          <div style=" 20px;"></div>
        </div>
    
    .hv_flex {
      display: flex;
       600px;
      height: 100px;
      border: 1px solid red;
      justify-content: center;
      align-content: center;
      align-items: stretch; /*  stretch让盒子内的每个元素的高度都等于行高 */
    }
    .hv_flexChild {
      flex: 1;
      border: 2px solid green;
      left: 10px;
      right: 10px;
    }

    方式二:after伪元素实现水平垂直居中 [推荐]
    优点:兼容所有游览器

    <div class="parent">
            <div class="son">after伪元素实现水平垂直居中</div>
    </div>
    
    .parent {
      border: 2px solid blue;
      text-align: center;
      height: 100px;
       600px;
      /* overflow: hidden; */
    }
    
    .parent::after {
      content: "";
      display: inline-block;
      vertical-align: middle;
      height: 100%;
      /*  100%; */
    }
    
    .son {
      display: inline-block;
      vertical-align: middle;
      border: 1px solid red;
      /*  100%; */
    }

    方式三:left:0;right:0;top:0;bottom:0;margin:auto;
    优点:简单
    缺点:没有足够空间时, 子元素会被截断, 但不会有滚动条.

     <div class="hv_parent">
          <div class="hv_child1">
            方式一:left:0;right:0;top:0;bottom:0;margin:auto;
          </div>
        </div>
    .hv_parent {
      position: relative;
       600px;
      height: 180px;
      border: 1px solid red;
    }
    
    .hv_child1 {
      border: 2px solid green;
      position: absolute;
      /* height: 40px; */
      /*  400px; */
      left: 10px;
      right: 10px;
      top: 0;
      bottom: 0;
      margin: auto;
    }

    方式四:left: 50%;top: 50%;transform: translate(-50%, -50%); [推荐]
    优点:代码量少
    缺点:IE8不支持, 属性需要追加浏览器厂商前缀, 可能干扰其他 transform 效果, 某些情形下会出现文本或元素边界渲染模糊的现象.

        <div class="hv_parent">
          <div class="hv_child2">
            方式二:left: 50%;top: 50%;transform: translate(-50%, -50%);
          </div>
        </div>
    
    .hv_parent {
      position: relative;
       600px;
      height: 180px;
      border: 1px solid red;
    }
    .hv_child2 {
      border: 2px solid blue;
      /* margin-left:50%; */
      /* margin-top: 50%; */
       400px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }

    方式五:height:固定;固定;left:50%;top:50%;margin-top:-0.5height;margin-left:- 0.5width;
    优点: 适用于所有浏览器.
    缺点: 父元素空间不够时, 子元素可能不可见(当浏览器窗口缩小时,滚动条不出现时).如果子元素设置了overflow:auto, 则高度不够时, 会出现滚动条.

        <div class="hv_parent">
          <div class="hv_child3">
            方式三:height:固定;固定;left:50%;top:50%;margin-top:-0.5*height;margin-left:- 0.5*width;
          </div>
        </div>

    参考文献

    [1] (https://louiszhai.github.io/2016/03/12/css-center/)

  • 相关阅读:
    不同数据库中两列字段相减(某列有空值)
    ASP.Net MVC利用NPOI导入导出Excel
    ASP.Net MVC中数据库数据导出Excel,供HTTP下载(转)
    Asp.net操作Excel(终极方法NPOI)(转)
    开发中可能会用到的几个 jQuery 小提示和技巧(转)
    最火的.NET开源项目(转)
    sql行转列和列转行(转)
    run fsck manually
    RTP-实时协议
    linux环境几个特殊的shell变量
  • 原文地址:https://www.cnblogs.com/Army-Knife/p/12029824.html
Copyright © 2020-2023  润新知