css实现居中的方法

水平居中

1. 行内设置text-align

给父元素设置text-align为center,一般用于实现文字水平居中

2. 给当前元素设置margin:0 auto

原理:块级独占一行,表现为在水平方向上占满整个父容器,当水平方向padding,border,width成定值时,margin左右为auto,默认margin左右平分剩余空间

html 复制代码
<head>
    <style>
        .content{
            width: 600px;
            height: 300px;
            border: 1px solid #000;
        }
        .box{
            width: 200px;
            height: 200px;
            background-color: darkcyan;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="content">
        <div class="box"></div>
    </div>
</body>

但是当该元素设置了position为absolute时,需要加上left为0,right为0才能使margin:0 auto生效,让元素左右边界都紧贴其包含块的边缘。

html 复制代码
.box{
    width: 200px;
    height: 200px;
    background-color: darkcyan;
    margin: 0 auto;
    left: 0;
    right: 0;
    position: absolute;
}

3. justify-content

父元素设置display为flex布局,justify-content为center,设置盒子在主轴的对齐方式为center

html 复制代码
.content{
   width: 600px;
   height: 300px;
   border: 1px solid #000;
   display: flex;
   justify-content: center;
}

4. transform + position

父元素设置relative,子元素设置absolute,left为50%,距离左边偏移父元素50%,transform为translateX(-50%),向左平移自身宽度的50%

5. margin-left + position

与transform类似,只是margin-left为负的自身宽度的一半

当未知宽高时用transform,已知宽高可以用margin-left

垂直居中

1. 单行文本可以设置line-height

设置line-height与height相等

一般用于文字垂直居中

2. align-items

父元素设置flex,align-items为center,定义元素在侧轴对齐方式为center

3. transform + position

与水平居中一样,设置top:50%距离上边偏移父元素50%,transform为translateY(-50%)

4. margin-top + position

与水平居中一样,设置top:50%,margin-top:负的自身高度的一半

5. margin:0 auto

子元素设置absolute定位,top为0,bottom为0,margin设置auto

html 复制代码
.box{
    width: 200px;
    height: 100px;
    background-color: darkcyan;
    position: absolute;
    margin: auto;
    top: 0;
    bottom: 0;
}

6. table-cell + vertical-align

给父元素设置display为table-cell,vertical-align为middle

整体居中

行内:text-align:center; line-height:height

块级:

  1. flex
html 复制代码
.content{
    width: 400px;
    height: 300px;
    border: 1px solid #000;
    display: flex;
    justify-content: center; /*水平居中*/
    align-items: center;  /*垂直居中*/
}
  1. transform
html 复制代码
.content{
    width: 400px;
    height: 300px;
    border: 1px solid #000;
    position: relative;
}
.box{
    width: 200px;
    height: 100px;
    background-color: darkcyan;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%)
}
  1. margin
html 复制代码
.content{
    width: 400px;
    height: 300px;
    border: 1px solid #000;
    position: relative;
}
.box{
    width: 200px;
    height: 100px;
    background-color: darkcyan;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -50px;
    margin-left: -100px;
}
相关推荐
星空寻流年1 小时前
css3伸缩盒模型第二章(侧轴相关)
javascript·css·css3
GalenWu3 小时前
对象转换为 JSON 字符串(或反向解析)
前端·javascript·微信小程序·json
GUIQU.3 小时前
【Vue】微前端架构与Vue(qiankun、Micro-App)
前端·vue.js·架构
数据潜水员3 小时前
插槽、生命周期
前端·javascript·vue.js
2501_907136823 小时前
CSS 学习与工程化实践指南
css
2401_837088503 小时前
CSS vertical-align
前端·html
优雅永不过时·3 小时前
实现一个漂亮的Three.js 扫光地面 圆形贴图扫光
前端·javascript·智慧城市·three.js·贴图·shader
CodeCraft Studio4 小时前
报表控件stimulsoft教程:使用 JoinType 关系参数创建仪表盘
前端·ui
春天姐姐5 小时前
vue知识点总结 依赖注入 动态组件 异步加载
前端·javascript·vue.js
互联网搬砖老肖6 小时前
Web 架构之数据读写分离
前端·架构·web