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


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

    • 绝对定位+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
  • 相关阅读:
    Nessus已激活,New Scan按钮不可点击
    OpenCV图像特征提取学习四,SIFT特征检测算法
    第一篇章:JVM与Java体系结构
    Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
    机器学习7—聚类算法之K-means算法
    Vue 3 + TypeScript + Vite + Element-Plus + Router + Axios + Pinia项目搭建(内含完整架构)
    react-query
    软考复习(二):操作系统
    CogView中的单层TransformerLayer
    线性代数:向量、张量、矩阵和标量
  • 原文地址:https://blog.csdn.net/qq_40864647/article/details/125535475