zoukankan      html  css  js  c++  java
  • css居中

    一、定位居中

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style type="text/css">
            .out {
                 300px;
                height: 300px;
                background: red;
                position: relative;
            }
    
            .in {
                 100px;
                height: 100px;
                background: green;
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
            }
        </style>
    </head>
    <body>
    <div class="out">
        <div class="in"></div>
    </div>
    </body>
    </html>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style type="text/css">
            .out {
                 300px;
                height: 300px;
                background: red;
                position: relative;
            }
    
            .in {
                 100px;
                height: 100px;
                background: green;
                position: absolute;
                margin: auto;
                top: 0;
                right: 0;
                bottom: 0;
                left: 0;
            }
        </style>
    </head>
    <body>
    <div class="out">
        <div class="in"></div>
    </div>
    </body>
    </html>

    二、弹性布局

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style type="text/css">
            .out {
                 300px;
                height: 300px;
                background: red;
                display: flex;
                flex-direction: row; /*定义主轴方向,row为默认值*/
                justify-content: center; /*主轴对齐方式*/
                align-items: center; /*交叉轴对齐方式*/
            }
    
            .in {
                 100px;
                height: 100px;
                background: green;
            }
        </style>
    </head>
    <body>
    <div class="out">
        <div class="in"></div>
    </div>
    </body>
    </html>

    三、vertical-align

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style type="text/css">
            .out {
                 300px;
                height: 300px;
                background: red;
                text-align: center;
            }
    
            .in {
                 100px;
                height: 100px;
                background: green;
                display: inline-block;
                vertical-align: middle;
            }
    
            .verticalaligndiv {
                height: 100%;
                display: inline-block;
                vertical-align: middle;
            }
        </style>
    </head>
    <body>
    <div class="out">
        <!--两个div之间不能有换行或空格,否则居中会有误差-->
        <div class="verticalaligndiv"></div><div class="in"></div>
    </div>
    </body>
    </html>
  • 相关阅读:
    redis redis-cli 操作指令
    Apache 配置默认编码
    Apache 查找httpd.conf文件
    Apache 错误日志
    dataTable 自定义排序
    bootstrap select2 参数详解
    获取元素滚动条高度
    TP5 操作DB is null is not null 条件
    TP5 自带分页类的传参
    jquery 获取 file 表单 上传的文件名
  • 原文地址:https://www.cnblogs.com/linding/p/13566049.html
Copyright © 2011-2022 走看看