zoukankan      html  css  js  c++  java
  • 纯css使div垂直居中,div垂直,div居中的方法

    首先编写一个简单的html代码,设置一个父div类名为boxFather,再设置一个子div类名为box1。html代码如下:

    <div class="boxFather">
       <div class="box1"></div>
    </div>

    下面使用div盒子里面要有内容撑开,html代码如下:

    <div class="boxFather">
            <div class="box1">
                测试内容
            </div>
        </div>

    1.div居中的方法

    (1)父div不定宽高,子div定宽高

      第一种方式:

    <style>
            .box1{
                background-color: red;
                width: 300px;
                height: 200px;
                margin: 0 auto; 
            }
    </style>

      第二种方式:

    <style>
            .boxFather{
                position: relative;
            }
            .box1{
                width: 300px;
                height: 200px;
                background-color: red;
                position: absolute;
                left: 50%;
                -webkit-transform: translate(-50%);
                -moz-transform: translate(-50%);
                -ms-transform: translate(-50%);
                -o-transform:translate(-50%) ;
                transform:translate(-50%);
            }
        </style>

    第三种方式:

     <style>
            .boxFather{
                display: flex;
                justify-content: center;
            }
            .box1{
                background-color: red;
                width: 300px;
                height: 200px;
            }
       </style>

    第四种方法:

        <style>
            .boxFather{
                text-align: center;
            }
            .box1{
                display: inline-block;
                background-color: red;
                width: 300px;
                height: 200px;
            }
        </style>

    第五种方法:

    <style>
            .boxFather{
                position: relative;
            }
            .box1{
                position: absolute;
                left:0;
                top:0;
                right:0;
                bottom:0;
                margin:auto;
                background-color: red;
                width: 300px;
                height: 200px;
            }
        </style>

     第六种:

    <style>
            .boxFather{
                position: relative;
            }
            .box1{
                position: absolute;
                left:50%;
                top:50%;
                width: 300px;
                height: 200px;
                margin-left: -150px;
                margin-top: -100px;
                background-color: red;
                
            }
        </style>

     第七种:

    <style>
            .boxFather{
                display: flex;
                justify-content:center;
            }
            .box1{
                width: 300px;
                height: 200px;
                background-color: red;
            }
            
        </style>

    (2)父div不定宽高,子div不定宽高 。(注意div盒子里面要有内容撑开)

      第一种方法:

        <style>
             .boxFather{
                position: relative;
            }
            .box1{
                background-color: red;
                position: absolute;
                left: 50%;
                -webkit-transform: translate(-50%);
                -moz-transform: translate(-50%);
                -ms-transform: translate(-50%);
                -o-transform:translate(-50%) ;
                transform:translate(-50%);
            }
        </style>

    第二种方法:

    <style>
            .boxFather{
                display: flex;
                justify-content:center;
            }
            .box1{
                background-color: red;
            }
        </style>

    2.div垂直居中

    (1)父div不定宽高,子div不定宽高

      第一种方法:

    <style>
            .boxFather{
                display: flex;
                justify-content:center;
                align-items: center;
                height: 200px;
                border:1px solid #ccc;
            }
            .box1{
                background-color: red;
            }
        </style>

    (2)父div定高,子div定宽高

      第一种方法:

    <style>
            .boxFather{
                position: relative;
                height: 500px;
                border:1px solid #ccc;
            }
            .box1{
                width: 300px;
                height: 200px;
                background-color: red;
                position: absolute;
                top: 50%;
                left: 50%;
                -webkit-transform: translate(-50%,-50%);
                -moz-transform: translate(-50%,-50%);
                -ms-transform: translate(-50%,-50%);
                -o-transform:translate(-50%,-50%) ;
                transform:translate(-50%,-50%);
            }
        </style>

    第二种方法:

     <style>
            .boxFather{
                
            }
            .box1{
                position: fixed;
                left:0;
                top:0;
                right:0;
                bottom:0;
                margin:auto;
                background-color: red;
                width: 300px;
                height: 200px;
            }
        </style>

    (3)父div定宽高,子div不定宽高(需要内容撑开)

      第一种方法:

     <style>
            .boxFather{
                width: 300px;
                height: 200px;
                background-color: red;
                display: table-cell;
                vertical-align: middle;
                text-align: center;        
            }
            .box1{
                display: inline-block;
            }
            
        </style>

    第二种方法:

    <style>
            .boxFather{
                display: flex;
                justify-content:center;
                align-items: center;
                height: 200px;
                border:1px solid #ccc;
            }
            .box1{
                background-color: red;
            }
        </style>

    3.div垂直

    文字垂直,html代码如下:

    <div class="boxFather">
      测试内容
     </div>

    第一种方法:

      .boxFather{
                height: 100px;
                line-height: 100px;
                background-color: red;
                border: 1px solid #ccc;
            }

    第二种方法:

     .boxFather{
                height: 100px;
                background-color: red;
                display:table-cell; 
                vertical-align:middle;
            }

    div垂直的内容可复用‘div垂直居中’的内容是内容垂直

    记录一下,以后还会陆续添加。

  • 相关阅读:
    oracle取字符串长度的函数length()和hengthb()
    kafka优化–JVM参数配置优化
    Linux 查看CPU信息,机器型号,内存等信息
    elasticsearch入库错误:gc overhead导致数据节点脱离集群
    揭密新一代运维管理平台建设七种利器
    EM12C 安装及卸载 注意点整理
    Tools:downloading and Building EDK II工具篇:安装/使用EDKII源代码获取/编译工具[2.3]
    【C语言探索之旅】 第二部分第十课:练习题和习作
    CSDN code使用教程之git使用方法具体解释
    [javase学习笔记]-7.5 构造函数须要注意的几个细节
  • 原文地址:https://www.cnblogs.com/zyt-it/p/10288038.html
Copyright © 2011-2022 走看看