zoukankan      html  css  js  c++  java
  • css 居中的汇总

    前言

    对css居中的几种方式汇总,并且分析适用情况。

    正文

    margin+position

    .CenterParent {
       position: relative;
       height: 200px;
        200px;
       background-color:yellow;
    }
    .CenterChild{
       position:absolute;
       height: 100px;
        100px;
       top:50%;
       left: 50%;
       margin-top:-50px;
       margin-left:-50px;
       background-color: red;
    }
    
    <div class="CenterParent">
    <div class="CenterChild">
    </div>
    </div>
    

    后续不展示效果。

    优点:兼容全部浏览器

    缺点:需要知道子元素的宽高。

    margin:aotu+postion

    .CenterParent {
       position: relative;
       height: 200px;
        200px;
       background-color:yellow;
    }
    .CenterChild{
       position:absolute;
       height: 100px;
        100px;
       top:0px;
       left: 0px;
       bottom: 0px;
       right: 0px;
       margin: auto;
       background-color: red;
    }
    

    中规中距:需要兼容的推荐。

    flex

    .CenterParent {
       display: flex;
       justify-content: center;
       align-items: center;
       height: 200px;
        200px;
       background-color:yellow;
    }
    .CenterChild{
       height: 100px;
        100px;
       background-color: red;
    }
    

    缺点:需要浏览器支持flex

    margin+transtion

    .CenterParent {
       position: relative;
       height: 200px;
        200px;
       background-color:yellow;
    }
    .CenterChild{
       position: absolute;
       top: 50%;
       left: 50%;
       transform:translate( -50%, -50%);
       height: 100px;
        100px;
       background-color: red;
    }
    

    缺点:需要支持transform

    table-cell

    .CenterParent {
       display: table-cell;
       text-align: center;
       vertical-align: middle;
       height: 200px;
        200px;
       background-color:yellow;
    }
    .CenterChild{
       100px;
      height: 100px;
      display: inline-block;
      background-color: red;
    }
    

    子元素必须是:inline-block或者inline.

  • 相关阅读:
    luogu P3959 宝藏
    hdu4035 Maze
    [hdu2899]Strange fuction
    luogu4407 [JSOI2009]电子字典 字符串hash + hash表
    SPOJ6717 Two Paths 树形dp
    luogu4595 [COCI2011-2012#5] POPLOCAVANJE 后缀自动机
    后缀数组
    luoguP1659 [国际集训队]拉拉队排练 manacher算法
    luoguP4555 [国家集训队]最长双回文串 manacher算法
    CF17E Palisection 差分+manacher算法
  • 原文地址:https://www.cnblogs.com/aoximin/p/12447307.html
Copyright © 2011-2022 走看看