zoukankan      html  css  js  c++  java
  • 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%)
    }
    
    
  • 相关阅读:
    前端工程化
    前端模块化CommonJS&ES6
    为什么浮点型运算结果会有误差?
    RequestAnimationFrame知多少?
    CSS三栏布局
    秋招面试
    实现Storage
    Angular
    TypeScript
    微服务架构设计模式
  • 原文地址:https://www.cnblogs.com/dobeco/p/11385677.html
Copyright © 2011-2022 走看看