css的3中水平居中方式和4中水平垂直居中方式及应用情形速记

三种水平居中方式及其应用情形

inline元素:text-align

主要设置在行内元素父元素中

 #box1 {
        /* 水平居中 */
        text-align: center;
    }

block元素:margin:0 auto

  #box2 #radiu{
        height: 100px;
        width: 100px;
        border: 1px solid black;
        border-radius: 50%;
        background-color: yellow;

        /* 水平居中 */
        margin: auto;
    }

absolute定位过的元素:left:50%;margin-left:负值(元素宽度的一半)

注意:使用这种定位必须要知道他的高度,并且别忘记父元素设置positive:reletive

  #box3 #diamond{
        height: 200px;
        width: 200px;
        border: 1px solid black;
        background-color: yellowgreen;

        /* 水平居中 */

        position: absolute;
        left: 50%;
        margin-left: -100px;

        /* transform: translate(-100px); */
        
    }

综合三种情况的代码和效果如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    body{
        margin: 0;
        padding: 0;
    }

    #box1{
        height: 50px;
        border: 1px solid black;
    }

    #box2{
        height: 200px;
        border: 1px solid black;
    }

    #box3{
        height: 300px;
        border: 1px solid black;
        position: relative;
    }
    #box1 {
        /* 水平居中 */
        text-align: center;
        /* height: 50px;
        line-height:50px ; */
    }
    #box2 #radiu{
        height: 100px;
        width: 100px;
        border: 1px solid black;
        border-radius: 50%;
        background-color: yellow;

        /* 水平居中 */
        margin: auto;
    }
 
    #box3 #diamond{
        height: 200px;
        width: 200px;
        border: 1px solid black;
        background-color: yellowgreen;

        /* 水平居中 */

        position: absolute;
        left: 50%;
        margin-left: -100px;

        /* transform: translate(-100px); */
        

    }


</style>
<body>
    <div class="box" id="box1">
        <span>提目</span>
    </div>
    <div class="box" id="box2">
        <div id="radiu"></div>
    </div>
    <div class="box" id="box3">
        <div id="diamond"></div>
    </div>
</body>
</html>

在这里插入图片描述

四种垂直居中方式和应用场景

inline元素:设置line-height等于height的值

这种就是必须要知道元素的height值,主要设置在行内元素父元素中

  #box1{
        /* 水平居中 */
        text-align: center;
        height: 50px;
        line-height:50px ;
    }

absolute定位过的元素:top:50%;margin-top:负值(元素宽度的一半)

注意:使用这种定位必须要知道他的高度,并且别忘记父元素设置positive:reletive

  #box3 #diamond{
        height: 200px;
        width: 200px;
        border: 1px solid black;
        background-color: yellowgreen;

    	/* 水平垂直居中 */
        position: absolute;
        left:50%;
        margin-left:-100px
        top: 50%;
        margin-top: -100px;
       
        

    }

absolute定位过的元素:设置transform:translate(-50%,-50%)

这里只用考虑到transform的兼容性问题

    #box3 #diamond{
        height: 200px;
        width: 200px;
        border: 1px solid black;
        background-color: yellowgreen;

        /* 水平居中 */
        /* 垂直居中 */
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
    }

absolute定位过的元素:top,left,bottom,right =0,margin:auto

这个是最优的既能保持兼容性又不用考虑他的高度宽度值

    #box3 #diamond{
        height: 200px;
        width: 200px;
        border: 1px solid black;
        background-color: yellowgreen;

        /* 水平居中 */

        /* 垂直居中 */
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        margin: ;
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值