zoukankan      html  css  js  c++  java
  • 盒子水平居中的五大方案

    首先先展示结构代码

    <style>
    .father {
    500px;
    height: 500px;
    background-color: red;
    }
    .child {
    100px;
    height: 100px;
    background-color: #000;
    }
    </style>
    <div class="father" id="father">
    <div class="child" id="child"></div>
    </div>

    这里展示两种方式:
    1、定位实现
    transform有兼容问题
    .father{
    position:relative;
    }
    .child{
    position:abolute;
    top:50%;
    left:50%;
    transform:translate(-50%,50%);
    }

    2、js方式

    let widW = father.childWidth,
    widH = father.clientHeight,
    boxW = child.offsetWidth,
    boxW = child.offsetHeight,
    child.style.position = 'absolute';
    child.style.left = (winW - boxW)/2 +'px';
    child.style.top= (winH - boxH)/2 +'px';

    其余的三种方式分别是margin,display:flex,以及display:table-cell。而在实际开发中,我们会发现display:flex;的功能特别强大,也是运用的最为广泛的

    再宏伟的目标,拆分下来,也只是一串串的代码而已;一串串的代码,细分来看,也只是一个一个的字母而已!也许,我做不到一晚上完成大神一小时随便敲打的项目,但为这一目标,每天添砖加瓦,我想我应该是可以完成的!
  • 相关阅读:
    双六
    除法取模
    欧拉函数及费马小定理
    基础模运算
    Leeetcode--581. Shortest Unsorted Continuous Subarray
    Codeforces Round #541--1131F. Asya And Kittens(基础并查集)
    leetcode--200. Number of Islands
    leetcode--21. Merge Two Sorted Lists
    leetcode--155. Min Stack
    Codeforces Round #539--1113B
  • 原文地址:https://www.cnblogs.com/Annely/p/14518735.html
Copyright © 2011-2022 走看看