• 如何使一个不定宽和不定高的元素垂直水平居中


    如何使一个不定宽和不定高的元素垂直水平居中

    • 绝对定位+translate
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
    
            body, html{
                width: 100%;
                height: 100%;
            }
    
            .container {
                width: 500px;
                height: 500px;
                background-color: aqua;
                position: relative;
            }
    
            .box {
                background-color: red;
                position: absolute;
                left: 50%;
                top: 50%;
                transform: translate(-50%, -50%);
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="box">jkahgjahkjdghajkdgh</div>
        </div>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 弹性盒
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
    
            body, html{
                width: 100%;
                height: 100%;
            }
    
            .container {
                width: 500px;
                height: 500px;
                background-color: aqua;
                display: flex;
                justify-content: center;
                align-items: center;
            }
    
            .box {
                background-color: red;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="box">jkahgjahkjdghajkdgh</div>
        </div>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • CSS Grid
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
    
            body, html{
                width: 100%;
                height: 100%;
            }
    
            .container {
                width: 500px;
                height: 500px;
                background-color: aqua;
                display: grid;
                justify-content: center;
                align-items: center;
            }
    
            .box {
                background-color: red;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="box">jkahgjahkjdghajkdgh</div>
        </div>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    数据结构与算法--其他算法
    聊聊如何解决官方提供的onpremise项目安装sentry速度过慢问题
    Python pathlib模块
    什么是本质安全?
    Comparable和Comparator的区别
    Java文件操作
    Unity ProBuilder(自己创建斜面、拐角)
    李开复:我家的AI是坠吼的
    linux-ARM下的数据库管理工具的安装使用(dbeaver)
    本地PHP搭建简单Imagewheel私人云图床,在外远程访问
  • 原文地址:https://blog.csdn.net/qq_40864647/article/details/125535475