块、行内块水平垂直居中

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; 
       }
相关推荐
小猪努力学前端9 分钟前
基于PixiJS的小游戏广告开发
前端·webgl·游戏开发
哆啦A梦158814 分钟前
62 对接支付宝沙箱
前端·javascript·vue.js·node.js
Tzarevich25 分钟前
用 OOP 思维打造可复用的就地编辑组件:EditInPlace 实战解析
javascript·前端框架
用户81686947472527 分钟前
Lane 优先级模型与时间切片调度
前端·react.js
虎头金猫27 分钟前
MateChat赋能电商行业智能导购:基于DevUI的技术实践
前端·前端框架·aigc·ai编程·ai写作·华为snap·devui
LiuMingXin27 分钟前
CESIUM JS 学习笔记 (持续更新)
前端·cesium
豆苗学前端36 分钟前
面试复盘:谈谈你对 原型、原型链、构造函数、实例、继承的理解
前端·javascript·面试
Crystal3281 小时前
Git 基础:生成版本、撤消操作、版本重置、忽略文件
前端·git·github
lichenyang4531 小时前
React 组件通讯全案例解析:从 Context 到 Ref 的实战应用
前端
国服第二切图仔1 小时前
Electron for 鸿蒙pc项目实战之右键菜单组件
javascript·electron·harmonyos·鸿蒙pc