zoukankan      html  css  js  c++  java
  • 如何实现固定宽高的DOM元素的水平垂直居中

    效果:

    实现代码1:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style type="text/css">
            body{margin: 0; padding: 0;}
            #div1{
                margin-top: 100px;
                margin-left: 150px;
                width: 300px;
                height: 300px;
                background-color: red;
                position: relative;
            }
            #div2{
                width: 120px;
                height: 120px;
                background-color: black;
                position: absolute;
                /*(300-120)/2/300*/
                top: 30%;
                left: 30%;
            }
        </style>
    </head>
    <body>
        <div id="div1" class="classname">
            <div id="div2" class="classname">
            </div>
        </div>
    </body>
    </html>

    实现代码2:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style type="text/css">
            body{margin: 0; padding: 0;}
            #div1{
                margin-top: 100px;
                margin-left: 150px;
                width: 300px;
                height: 300px;
                background-color: red;
            }
            #div2{
                width: 120px;
                height: 120px;
                background-color: black;
                /*(300-120)/2/300=30%*/
                position: relative;
                top: 30%;
                left: 30%;
            }
        </style>
    </head>
    <body>
        <div id="div1" class="classname">
            <div id="div2" class="classname">
            </div>
        </div>
    </body>
    </html>

    实现代码3:

  • 相关阅读:
    多按键设计的标准思路
    与,非,或门总结
    i2c中应答信号信号总结
    i2c中start和restart的区别
    poj 1631 Bridging signals
    poj 2533 Longest Ordered Subsequence
    poj 1887 Testing the CATCHER
    poj 1088 滑雪
    poj 1014 Dividing
    babel转码时generator的regeneratorRuntime
  • 原文地址:https://www.cnblogs.com/arvintang/p/5433390.html
Copyright © 2011-2022 走看看