Vue Vue项目中使用Html+Css使div在页面中居中显

2022-08-27  本文已影响0人  Rinaloving
备注:由于修改全局样式(之前也写的不好),导致登陆div框位置偏移,然后参考网上做法
1. div使用绝对布局,设置margin:auto;并设置top、left、right、bottom的值相等即可,不一定要都是0。
.main{
    text-align: center;
    background-color: #fff;
    border-radius: 20px;
    width: 300px;
    height: 350px;
    margin: auto;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}


2. 让left和top都是50%,这在水平方向上让div的最左与屏幕的最左相距50%,垂直方向上一样,所以再用transform向左(上)平移它自己宽度(高度)的50%,也就达到居中效果了。
 .main{
    text-align: center;
    background-color: #fff;
    border-radius: 20px;
    width: 300px;
    height: 350px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}


效果
登录效果.png
上一篇 下一篇

猜你喜欢

热点阅读