• css垂直居中方案


    垂直居中的几种方法

    html结构

    <div class="container">
          <div class="item">垂直居中</div>
    </div>
    
    

    css

    <style>
        
    .container {
       500px;
      height: 500px;
      border: 1px solid #000;
      position: relative;
    }
    
    
    
    </style>
    
    

    1. 使用flex布局

    在父容器中设置

    
    .container{
      displayy: flex;
      justify-content: center; 
      align-items: center
    
    }
    

    2. 绝对定位

    分为已知宽高和未知宽高两种情况

    • 已知宽高都是100px,设置自身为绝对定位(absolute),top和left为50%,margin-left、margin-top为自身的一半,也就是50px
    .item {
       100px;
      height: 100px;
      border: 1px solid red;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -50px;
      margin-top: -50px;
    }
    
    
    
    
    • 未知宽高
    /*使用traansform*/
    
     .item {
      border: 1px solid red;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%)
    }
    
    
  • 相关阅读:
    自己收集的一些伪元素/伪类
    ie9的placeholder不显示的解决办法(包含多个密码框)
    9.14上午
    9.13
    9.11笔记
    html基础英语单词
    选择器的分辨
    学习笔记
    RecyleView
    自定义view获取宽高
  • 原文地址:https://www.cnblogs.com/dobeco/p/11385677.html
Copyright © 2020-2023  润新知