块、行内块水平垂直居中

1.定位实现水平垂直居中

复制代码
<div class="outer">
        <div class="test inner1">定位实现水平垂直居中</div>
    </div>

<style>
 .outer {
            width: 300px;
            height: 300px;
            border: 1px solid gray;
            margin: 100px auto 0;
            position: relative;
       }
       .test {
            width: 100px;
            height: 100px;
            background-color: orange;
           
       }
       .inner1 {
             /* 方法一 定位*/
             position: absolute;
            /* 相对于包含块,最近的定位的祖先或父元素 */
            top: 50%; 
            left: 50%;
            margin-left: -50px;
            margin-top: -50px;
       }
</style>

2.定位+位移

复制代码
<div class="outer">
        <div class="test inner2">定位+位移</div>
    </div>
<style>
.outer {
            width: 300px;
            height: 300px;
            border: 1px solid gray;
            margin: 100px auto 0;
            position: relative;
       }
       .test {
            width: 100px;
            height: 100px;
            background-color: orange;
           
       }
       .inner2 {
            position: absolute;
            top: 50%;
            left: 50%;
            /*方法二  translate中的百分比是相对自身,50% 相当于100*50% = 50*/
            transform: translate(-50%, -50%);
       }
</style>

3.相对于整个视口水平垂直居中

复制代码
 <div class="view">相对于整个视口水平垂直居中</div>
    <style>
       
       body {
            position: relative;
        }
        /* 方法三 */
       .view {
            width: 100px;
            height: 100px;
            background-image: linear-gradient(red, yellow, green);
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
       }
    </style>

4.flex+margin

复制代码
<head>
    <style>
        .outer {
            width: 400px;
            height: 400px;
            background-color: gray;
            display: flex;
        }
        .inner {
            width: 100px;
            height: 100px;
            margin: auto;
            background-image: linear-gradient(green, yellow);
        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner"></div>
    </div>
</body>
  1. flex 水平垂直居中

    <head> <style> .outer { width: 400px; height: 400px; background-color: gray; display: flex; /* 水平居中 */ justify-content: center; /* 垂直居中 */ align-items: center; } .inner { width: 100px; height: 100px; background-image: linear-gradient(green, yellow); } </style> </head> <body>
    </body>

6.遮挡层

复制代码
<div class="item"></div>

/* 遮挡层 */
       .item {
             background-image: repeating-linear-gradient(red 200px, yellow 500px, green 700px);
            opacity: 0.5;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0; 
       }
相关推荐
逍遥德3 分钟前
前端工程化-包管理NPM-package.json 和 package-lock.json 详解
前端·npm·json
一只小风华~4 分钟前
Web前端 (CSS篇)
前端·css·html·html5
HelloRevit8 分钟前
npm install 版本过高引发错误,请添加 --legacy-peer-deps
前端·npm·node.js
工九度10 分钟前
2025前端社招最新面试题汇总- 场景题篇
前端·javascript
AronTing10 分钟前
状态模式:有限状态机在电商订单系统中的设计与实现
前端·设计模式·面试
这可不简单11 分钟前
git push 受阻,原是未拉取代码惹的祸
前端·git·面试
啊吧啊吧曾小白14 分钟前
封装 downloadFile 函数,从服务器下载文件
前端·javascript·面试
左言16 分钟前
PYLSP 桥接 MONACO
前端
Danny_FD31 分钟前
前端BFC详解:从基础到深入的全面解读
前端
临枫38840 分钟前
网页图像优化:现代格式与响应式技巧
前端