CSS 水平,垂直,水平垂直居中方案——持续更新

准备

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .parent{
            background: #eee;
            width: 300px;
            height: 300px;
        }
        .block{
            background: skyblue;
            width: 100px;
            height: 100px;
        }
        .inline{
            background: pink;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="parent">
            <div class="block">
                块级元素
            </div>
            <span class="inline">
                行内元素
            </span>
        </div>
    </div>
</body>
</html>

在这里插入图片描述

水平居中

行内元素

text-align: center;

.parent{
            background: #eee;
            width: 300px;
            height: 300px;
            text-align: center;
        }

在这里插入图片描述

块级元素

margin: 0 auto;

.block{
            background: skyblue;
            width: 100px;
            height: 100px;
            margin: 0 auto;
        }

在这里插入图片描述

flex布局

display: flex;
justify-content: center;

.parent {
            background: #eee;
            width: 300px;
            height: 300px;
            display: flex;
            justify-content: center;
        }

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

绝对定位 定宽

position: absolute;
width: 宽度;
left: 50%;
margin-left: -0.5*宽度

		.parent {
            background: #eee;
            width: 300px;
            height: 300px;
            position: relative;
        }

 		.block {
            background: skyblue;
            width: 100px;
            height: 100px;
            position: absolute;
            left: 50%;
            margin-left: -50px;
        }

        .inline {
            background: pink;
            position: absolute;
            width: 100px;
            left: 50%;
            margin-left: -50px;
        }

在这里插入图片描述
position:absolute和float会隐式地改变display类型,不论之前什么类型的元素(display:none除外), 只要设置了position:absolute、 float中任意一个,都会让元素以display:inline-block的方式显示:可以设置长宽,默认宽度并不占满父元素。

绝对定位 不定宽

position: absolute;
left: 50%;
transform: translate(-50%, 0);

		.parent {
            background: #eee;
            width: 300px;
            height: 300px;
            position: relative;
        }

		.block {
            background: skyblue;
            width: 100px;
            height: 100px;
            position: absolute;
            left: 50%;
            transform: translate(-50%, 0);
        }

        .inline {
            background: pink;
            position: absolute;
            width: 100px;
            left: 50%;
            transform: translate(-50%, 0);
        }

在这里插入图片描述

绝对定位 left/right: 0

position: absolute;
width: 宽度;
left: 0;
right: 0;
margin: 0 auto;

		.parent {
            background: #eee;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .block {
            background: skyblue;
            width: 100px;
            height: 100px;
            position: absolute;
            left: 0;
            right: 0;
            margin: 0 auto;
        }

        .inline {
            background: pink;
            position: absolute;
            width: 100px;
            left: 0;
            right: 0;
            margin: 0 auto;
        }

在这里插入图片描述

垂直居中

行内元素

父元素 height: 高度;
行内元素 line-height: 高度;

		.parent {
            background: #eee;
            width: 300px;
            height: 300px;
        }
        .inline {
            background: pink;
            line-height: 300px;
        }

在这里插入图片描述

table

父元素 display: table-cell;
子元素 vertical-align: middle;

 .parent {
            background: #eee;
            width: 300px;
            height: 300px;
            display: table-cell;
            vertical-align: middle;
        }

在这里插入图片描述

flex

display: flex;
align-items: center;

 		.parent {
            background: #eee;
            width: 300px;
            height: 300px;
            display: flex;
            align-items: center;
        }

        .block {
            background: skyblue;
            width: 100px;
            height: 100px;
        }

        .inline {
            background: pink;
        }

在这里插入图片描述

绝对定位 定高

position: absolute;
top: 50%;
height: 高度;
margin-top: -0.5高度;

       .parent {
            background: #eee;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .block {
            background: skyblue;
            width: 100px;
            height: 100px;
            position: absolute;
            top: 50%;
            margin-top: -50px;
        }

        .inline {
            background: pink;
            height: 100px;
            position: absolute;
            top: 50%;
            margin-top: -50px;
        }

在这里插入图片描述

绝对定位 不定高

position: absolute;
top: 50%;
transform: translate( 0, -50%);

		.parent {
            background: #eee;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .block {
            background: skyblue;
            width: 100px;
            height: 100px;
            position: absolute;
            top: 50%;
            transform: translate( 0, -50%);
        }

        .inline {
            background: pink;
            height: 100px;
            position: absolute;
            top: 50%;
            transform: translate( 0, -50%);
        }

在这里插入图片描述

绝对定位 top/bottom: 0;

position: absolute;
height: 高度;
top: 0;
bottom: 0;
margin: auto 0;

	.parent {
            background: #eee;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .block {
            background: skyblue;
            width: 100px;
            height: 100px;
            position: absolute;
            top: 0;
            bottom: 0;
            margin: auto 0;
        }

        .inline {
            background: pink;
            position: absolute;
            height: 100px;
            top: 0;
            bottom: 0;
            margin: auto 0;
        }

在这里插入图片描述

元素水平垂直居中

绝对定位 不定宽高

绝对定位 transform
position:absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%)

		.parent {
            background: #eee;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .block {
            background: skyblue;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50% ,-50%);
        }

在这里插入图片描述

绝对定位 定宽高

绝对定位 margin
position:absolute;
top:50%;
left:50%;
margin-top:-(height/2)px;
margin-left:-(width/2)px;

 .parent {
            background: #eee;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .block {
            background: skyblue;
            width: 100px;
            height: 100px;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -50px;
            margin-left: -50px;
        }

绝对定位 margin:auto
position:absolute/fixed;
top:0;left:0;right:0;bottom:0;
margin:atuo;

		.parent {
            background: #eee;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .block {
            background: skyblue;
            width: 100px;
            height: 100px;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
        }

在这里插入图片描述
绝对定位 calc 水平、垂直居中也可以使用此方法
position:absolute;
top:calc(50% - (height/2)px);
left:calc(50% - (width/2)px);

	 	.parent {
   	        background: #eee;
            width: 300px;
            height: 300px;
            position: relative;
        }

        .block {
            background: skyblue;
            position: absolute;
            width: 100px;
            height: 100px;
            top:calc(50% - 50px);
            left:calc(50% - 50px);
        }
  • calc() 函数
    calc() 函数用于动态计算长度值
    需要注意的是,运算符前后都需要保留一个空格,例如:width: calc(100% - 10px);
    任何长度值都可以使用calc()函数进行计算;
    calc()函数支持"+", "-", "*", "/"运算;
    calc()函数使用标准的数学运算优先级规则

flex

display: flex;
justify-content: center;
align-items: center;

.parent {
            background: #eee;
            width: 300px;
            height: 300px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

在这里插入图片描述

table

display: table-cell;
vertical-align: middle;
text-align: center;
如果是块级元素 需要 display: inline-block;

     background: #eee;
            width: 300px;
            height: 300px;
            display: table-cell;
            vertical-align: middle;
            text-align: center;
        }

        .block {
            background: skyblue;
            width: 100px;
            height: 100px;
            display: inline-block;
        }

        .inline {
            background: pink;
        }

在这里插入图片描述

文本垂直居中

height:高度
line-height:高度

.parent {
            background: #eee;
            width: 300px;
            height: 300px;
            text-align: center;
            line-height: 300px;
        }

在这里插入图片描述

图片垂直居中

在图片前或者后定义一个span元素
width: 0; height: 100%; display: inline-block; vertical-align: middle;
给图片img添加vertical-algin:middle中线对齐

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .wrap{
            width: 600px;
            height: 600px;
            margin: 0 auto;
            border: 5px dotted skyblue;
            text-align: center;

        }
        span{
            width: 0;
            height: 100%;
            display: inline-block;
            vertical-align: middle;
        }
        img{
            vertical-align: middle;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <span></span>
        <img src="./img/image.jpg" alt="">
    </div>
</body>
</html>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值