div盒子水平垂直居中以及表格的居中的方法

 

目录

多种水平垂直居中方法 


这里的方法和第二种一致,方便用来对系统后台进行布局

#admin {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -150px;
  margin-left: -200px;
  width: 400px;
  height: 300px;
  background: #fff;
}

1.必需知道该div的宽度和高度,

2.然后设置位置为绝对位置,

3.距离页面窗口左边框和上边框的距离设置为50%,这个50%就是指页面窗口的宽度和高度的50%,

4.最后将该div分别左移和上移,左移和上移的大小就是该DIV宽度和高度的一半。

登录页面使用表格进行布局

 <table>
          <tr>
            <td width='100'>用户名:</td>
            <td width="300">
              <el-input
                v-model="input"
                placeholder="请输入内容"
                style="width: 250px"
              ></el-input>
            </td>
          </tr>
          <tr>
            <td width='100'>密&nbsp;&nbsp;&nbsp;码:</td>
            <td width='300'>
              <el-input
                v-model="input"
                placeholder="请输入内容"
                style="width: 250px;"
              ></el-input>
            </td>
          </tr>
        </table>
table{
  width: 350px;
  margin: auto;
}
td{
  letter-spacing:1px;  
  text-align: right;
}

多种水平垂直居中方法 

当然居中的方式还有很多,下面是水平和垂直居中的一些写法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 第一种居中方式 */
       /*  .pre{
            width: 500px;
            height: 500px;
            background-color: aqua;
            position: relative;
        }
        .child{
            width: 100px;
            height: 100px;
            background-color: black;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
        } */
        /* 第二种方式 */
        /* .pre{
            width: 500px;
            height: 500px;
            background-color: aqua;
            position: relative;
        }
        .child{
            width: 100px;
            height: 100px;
            background-color: black;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left: -50px;
            margin-top: -50px;
        }  */
        /* 第三种方式flex居中 */
        .pre{
            width: 500px;
            height: 500px;
            background-color: aqua;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .child{
            width: 100px;
            height: 100px;
            background-color: black;
        }
    </style>
</head>
<body>
    <div class="pre">
        <div class="child"></div>
    </div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Southern Wind

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值