【前端】盒子水平垂直居中的六大妙招

第一种方式 定位方法

使用定位实现,让子盒子相对于父盒子进行定位

  • 父盒子设置相对定位,父盒子设置绝对定位
  • 先把盒子挪到中间,然后向上向下移动自己的一半
  • 缺点嘛就是必须知道盒子的宽高,才能计算移动的距离
<style>
    
        .aa{
           
            width: 600px;
            height: 600px;
           position: relative;
           background-color: blueviolet;
        }
        .bb{
            width: 200px;
            height: 200px;
            background-color: pink;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left: -100px;
            margin-top: -100px;

        }
    </style>
</head>
<body>
    <div class="aa">
        <div class="bb">

        </div>
    </div>
</body>

第二种方式 四个方向都设为0 拖动

  • 缺点嘛:就是虽然不用知道具体的宽高,但是必须要有固定的宽高
<style>
    
        .aa{
           
            width: 600px;
            height: 600px;
           position: relative;
           background-color: blueviolet;
        }
        .bb{
            width: 200px;
            height: 200px;
            background-color: pink;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin:  auto;

        }
    </style>
</head>
<body>
    <div class="aa">
        <div class="bb">

        </div>
    </div>
</body>

第三种方式 css3的动画

+/* 兼容问题 /
/
可以 子盒子不设置宽高 */

<!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>
    
        .aa{
           
            width: 600px;
            height: 600px;
           position: relative;
           background-color: blueviolet;
        }
        .bb{
            /* width: 200px; */
            /* height: 200px; */
            background-color: pink;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);

        }
        /* 兼容问题 */
        /* 可以子盒子不设置宽高 */
    </style>
</head>
<body>
    <div class="aa">
        <div class="bb">
大萨达
        </div>
    </div>
</body>
</html>

上面三种方法都用到了定位
———————————————————————————————

第四种方式 cflex布局

  • 缺点就是兼容性问题,适合移动端大面积使用,很爽
<!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>
    
        .aa{
           
            width: 600px;
            height: 600px;
           display: flex;
           justify-content: center;
           align-items: center;
           background-color: blueviolet;
        }
        .bb{
            width: 200px;
            height: 200px;
            background-color: pink;
            

        }
    </style>
</head>
<body>
    <div class="aa">
        <div class="bb">

        </div>
    </div>
</body>
</html>

第五种方式 javacript实现

  • 获得当前屏幕的宽和高,获取盒子的宽和高,然后定位的时候用屏幕宽高减去盒子宽高再除以2.本质上还是基于定位来的,需设置body的position为relative,box的position为absolute。
<!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>
    
        body{
           position: relative;
          overflow: hidden
          ;
         height: 100%;
        }
        .bb{box-sizing: border-box;

            width: 100px;
            height: 50px;
            line-height: 48px;
            text-align: center;
            font-size: 16px;
            background-color: pink;
        }
    </style>
</head>
<body>
   
        <div class="box" id="box">
sdsd 
        </div>
    
</body>
<script>
    let html=document.documentElement,
    winw=html.clientWidth,
    
    
    winh=html.clientHeight,
    aa=box.offsetWidth,
    boxh=box.offsetHeight;
    box.style.position="absolute"
    box.style.left=(winw-aa)/2+"px"
    box.style.top=(winh-boxh)/2+"px"
    console.log(winw);
    console.log(box.offsetWidth);
    console.log(winh);
    console.log(boxh);
    
    
    
</script>
</html>

第六种方式 table-cell布局实现

    1. 不要与float:left; position:absolute; 一起使用
  1. 可以实现大小不固定元素的垂直居中
  2. margin设置无效,响应padding设置
  3. 对高度和宽度高度敏感
  4. 不要对display:table-cell使用百分比设置宽度和高度
  • display:table-cell;会使元素表现的类似一个表格中的单元格td,利用这个特性可以实现文字的垂直居中效果。同时它也会破坏一些CSS属性,使用table-cell时最好不要与float以及position: absolute一起使用,设置了table-cell的元素对高度和宽度高度敏感,对margin值无反应,可以响应padding的设置,表现几乎类似一个td元素。
<style>
    
        .aa{
           
            width: 600px;
            height: 600px;
            background-color: yellow;
            display: table-cell;
            vertical-align: middle;
            /* text-align: center; */
        }
        .bb{
            margin: auto;
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <div class="aa">
        <div class="bb">

        </div>
    </div>
</body>

总结:其实盒子水平居中还有其他的解决方案,当前最常用的是第三种和第四种方式,但是他们需要考虑兼容,对于一些老版本的浏览器例如IE8以及之前的是不兼容的。可根据项目中的实际需要去选择采用的方式。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值