zoukankan      html  css  js  c++  java
  • css块级居中的四种方法

    HTML

    <div class="fater">
        <div class="sum"></div>
    </div>

    方式一: 父级相对 子级绝对

      要求子级宽度设置

    <style>
        .fater{
            width: 500px;
            height: 500px;
            border: 1px solid black;
            position: relative;
        }
        .sum{
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -50px;
            margin-top: -50px;
        }
    </style>

    方式二:transform:translate(-50%);

      宽高不确定,transform是css3属性注意兼容性问题

    <style>
        .fater{
            width: 500px;
            height: 500px;
            border: 1px solid black;
            position: relative;
        }
        .sum{
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%);
        }
    </style>

    方式三

      子级宽高不确定

    <style>
        .fater{
            width: 500px;
            height: 500px;
            border: 1px solid black;
            position: relative;
        }
        .sum{
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>

    方式四:flex布局

    <style>
        .fater{
            width: 500px;
            height: 500px;
            border: 1px solid black;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .sum{
            width: 100px;
            height: 100px;
            background: red;
        }
    </style>
  • 相关阅读:
    【NOIP2013】花匠
    【DP合集】tree-knapsack
    【DP合集】m-knapsack
    【DP合集】背包 bound
    【DP合集】合并 union
    【DP合集】棋盘 chess
    BZOJ1026 [SCOI2009]windy数
    最长上升子序列 LIS nlogn
    再谈线性基
    数论问题算法模板
  • 原文地址:https://www.cnblogs.com/liangfc/p/12752817.html
Copyright © 2011-2022 走看看