zoukankan      html  css  js  c++  java
  • 实现DIV居中的几种方法

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>demo</title>
        </head>
        <body>
            <style type="text/css">
                .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
                .div2{ width:40px ; height: 40px; background-color: green;}
            </style>
    
            <div class="div1">
                <div class="div2">
    
                </div>
            </div>
    
        </body>
    </html>

    如上的两个div,实现div2在div1里面是居中显示

    一、方法一

      利用margin,div1的宽减去div2的宽就是div2margin-left的数值:(100-40)/2=30

      div1的高减去div2的高就是div2margin-top的数值:(100-40)/2=30

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>demo</title>
        </head>
        <body>
            <style type="text/css">
                .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
                .div2{ width:40px ; height: 40px; background-color: green;}
    
                .div22{
                    margin-left: 30px;margin-top: 30px;
                }
            </style>
    
            <div class="div1">
                <div class="div2 div22">
    
                </div>
            </div>
    
        </body>
    </html>

    二、方法二

      利用css的 position属性,把div2相对于div1的top、left都设置为50%,然后再用margin-top设置为div2的高度的负一半拉回来,用marg-left设置为宽度的负一半拉回来,css如下设置

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>demo</title>
        </head>
        <body>
            <style type="text/css">
                .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
                .div2{ width:40px ; height: 40px; background-color: green;}
    
                .div11{
                    position: relative;
                }
                .div22{
                    position: absolute;top:50%;left: 50%;margin-top: -20px;margin-left: -20px;
                }
            </style>
    
            <div class="div1 div11">
                <div class="div2 div22">
    
                </div>
            </div>
    
        </body>
    </html>

    三、方法三

      还是用css的position属性,如下的html

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>demo</title>
        </head>
        <body>
            <style type="text/css">
                .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
                .div2{ width:40px ; height: 40px; background-color: green;}
    
                .div11{
                    position: relative;
                }
                .div22{
                    position: absolute;margin:auto; top: 0;left: 0;right: 0;bottom: 0;
                }
            </style>
    
            <div class="div1 div11">
                <div class="div2 div22">
    
                </div>
            </div>
    
        </body>
    </html>

    四、方法四

      利用css3的新增属性table-cell

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>demo</title>
        </head>
        <body>
            <style type="text/css">
                .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
                .div2{ width:40px ; height: 40px; background-color: green;}
    
                .div11{
                    display: table-cell;vertical-align: middle;
                }
                .div22{
                    margin: auto;
                }
            </style>
    
            <div class="div1 div11">
                <div class="div2 div22">
                </div>
            </div>
    
        </body>
    </html>

    这个方法还有一个好处就是,div2的高度可以不固定,如下

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>demo</title>
        </head>
        <body>
            <style type="text/css">
                .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
                .div2{ width:40px ; background-color: green;}
    
                .div11{
                    display: table-cell;vertical-align: middle;
                }
                .div22{
                    margin: auto;
                }
            </style>
    
            <div class="div1 div11">
                <div class="div2 div22">
                   div居中方法
                </div>
            </div>
    
        </body>
    </html>

  • 相关阅读:
    Delphi的对话框窗体
    Delphi中窗体的事件
    TForm类有关属性简介
    Delphi的工具栏
    一个简单的MDI示范程序(Delphi)
    Delphi的组件选项卡(Component Palette)
    最简单的多重窗体的应用(Delphi)
    发现一个SVG做的地图网站:ChinaQuest
    计算最近点和最近线段
    寻找MapBar的地图切割方法
  • 原文地址:https://www.cnblogs.com/jiekk/p/5802147.html
Copyright © 2011-2022 走看看