首页 > 其他分享 >CSS实现水平垂直居中的方式汇总

CSS实现水平垂直居中的方式汇总

时间:2022-10-13 23:23:53浏览次数:94  
标签:居中 汇总 100px height width 102 position CSS absolute

原文链接:CSS实现水平垂直居中的1010种方式 (https://cloud.tencent.com/developer/article/2035014)

一、目录

1.1 居中元素固定宽高使用

1.2 居中元素无固定宽高

二、居中元素固定宽高使用

2.1 absolute + 负值margin

<!-- HTML -->
<div class="father">
    <div class="son"></div>
</div>
/* CSS */
.father {
    position: relative;
    width: 200px;
    height: 100px;
    border: 1px solid red;
}

.son {
    width: 100px;
    height: 50px;
    background-color: rgb(102, 179, 102);
    /* 核心代码 */
    position: absolute;
    left: 50%;
    top: 50%;
    margin-top: -25px;
    margin-left: -50px;
}

image

2.2 absolute + margin:auto

<!-- HTML -->
<div class="father">
    <div class="son"></div>
</div>
/* CSS */
.father {
    position: relative;
    width: 200px;
    height: 100px;
    border: 1px solid red;
}

.son {
    width: 100px;
    height: 50px;
    background-color: rgb(102, 179, 102);
    /* 核心代码 */
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
}

image

2.3 absolute + calc

<!-- HTML -->
<div class="father">
    <div class="son"></div>
</div>
/* CSS */
.father {
    position: relative;
    width: 200px;
    height: 100px;
    border: 1px solid red;
}

.son {
    width: 100px;
    height: 50px;
    background-color: rgb(102, 179, 102);
    /* 核心代码 */
    position: absolute;
    left: calc(50% - 50px);
    top: calc(50% - 25px);
}

image

三、居中元素无固定宽高

3.1 absolute + transform

<!-- HTML -->
<div class="father">
    <div class="son"></div>
</div>
/* CSS */
.father {
    position: relative;
    width: 200px;
    height: 100px;
    border: 1px solid red;
}

.son {
    width: 100px;
    height: 50px;
    background-color: rgb(102, 179, 102);
    /* 核心代码 */
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

image

3.2 lineHeight

<!-- HTML -->
<div class="father">
    <div class="son">lineHeight</div>
</div>
/* CSS */
.father {
    position: relative;
    width: 200px;
    height: 100px;
    border: 1px solid red;
    /* 核心代码 */
    line-height: 100px;
    text-align: center;
    font-size: 0px;
}

.son {
    background-color: rgb(102, 179, 102);
    /* 核心代码 */
    font-size: 16px;
    display: inline-block;
    vertical-align: middle;
    line-height: initial;
    text-align: left;
}

image

3.3 table

<!-- HTML -->
<table>
    <tbody>
        <tr>
            <td class="father">
                <div class="son">table 水平垂直居中</div>
            </td>
        </tr>
    </tbody>
</table>
/* CSS */
.father {
    position: relative;
    width: 200px;
    height: 100px;
    border: 1px solid red;
    /* 核心代码 */
    text-align: center;
}

.son {
    background-color: rgb(102, 179, 102);
    /* 核心代码 */
    display: inline-block;
}

image

3.4 flex

<!-- HTML -->
<div class="father">
    <div class="son"></div>
</div>
/* CSS */
.father {
    position: relative;
    width: 200px;
    height: 100px;
    border: 1px solid red;
    /* 核心代码 */
    display: flex;
    justify-content: center;
    align-items: center;
}

.son {
    width: 100px;
    height: 50px;
    background-color: rgb(102, 179, 102);
}

image

3.5 grid

<!-- HTML -->
<div class="father">
    <div class="son"></div>
</div>
/* CSS */
.father {
    position: relative;
    width: 200px;
    height: 100px;
    border: 1px solid red;
    /* 核心代码 */
    display: grid;
}

.son {
    width: 100px;
    height: 50px;
    background-color: rgb(102, 179, 102);
    /* 核心代码 */
    align-self: center;
    justify-self: center;
}

image

标签:居中,汇总,100px,height,width,102,position,CSS,absolute
From: https://www.cnblogs.com/bkzj/p/16790080.html

相关文章

  • 网页制作CSS
     css基础知识(一)css样式表的定义(二)css样式表的语法Pre标签 (三)内部样式表(四)外部样式表(五)内联样式表 css样式表的定义 css:(CascadingStyleSheets)层叠样式......
  • 深入理解css 笔记(4)
    处理元素高度的方式跟处理宽度不一样。之前对border-box的修改依然适用于高度。而且很有用,但是通常最好避免给元素指定明确的高度。当明确设置一个元素的高度时,内容可能......
  • 添加如下样式,现在这个弹框既能在视窗居中,又能在内容过多时防止弹框大小超出视窗,还能把
    1.el-dialog{2display:flex;3flex-direction:column;4margin:0!important;5position:absolute;......
  • 我为什么喜欢原子化CSS
    用Unocss已经有几个月了,从使用者的角度来说说我为什么喜欢上了原子化css。类似“原子化css”的概念其实很早之前就有,比如Bootstrap的css工具类已经很接近现在的原子化css......
  • css面试点总结2
    css面试点-@import与link的详解css面试点-position属性css面试点-CSS预处理器(Sass/Less/Postcss)css面试点-css3的filter详解......
  • css面试点总结一
    css面试点-css盒子模型css面试点-flex布局,css3弹性盒子模型css面试点-BFC(块级格式化上下文)与常见布局方案css面试点-css层叠上下文css面试点-div居中方法(共8种)css面试点-清......
  • 视频融合平台EasyCVR多种协议的接入方式汇总及注意事项
    EasyCVR具备较强的视频能力,可支持海量设备接入、汇聚与管理、视频监控、视频录像、云存储、回放与检索、智能告警、平台级联等功能。平台可支持多协议接入,包括:国标GB/T28181......
  • css面试题大全(持续更新)
    介绍一下css的盒子模型?​css盒子模型详解css选择符有哪些?css3新增伪类有那些?​元素选择符与关系选择符属性选择符伪类选择符,CSS3新增伪类伪对象(伪元素)选择符css那些属性......
  • CSS——下拉菜单
    1.下拉菜单通常使用在鼠标过程中,当鼠标悬停是出现一个下拉的菜单。2.使用任何元素打开下拉菜单内容,例如<span>或<button>元素3.使用容器元素<如div>创建下拉内容,并在......
  • css面试点-css层叠上下文
    什么是层叠上下文层叠上下文(​​stackingcontext​​​),是​​HTML​​​中一个三维的概念。在​​CSS2.1​​规范中,每个盒模型的位置是三维的,分别是平面画布上的X轴,Y轴以......