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

简介: 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宽度和高度的一半。

62396f5c52b44347ae60fdfd64b2d2f2.png

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

<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;
}

08da38ef4e084bf581ebf7d79b7b280e.png

多种水平垂直居中方法

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

!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月前
html中div内容居中的方法使用弹性盒子居中
html中div内容居中的方法使用弹性盒子居中
72 0
|
10月前
div盒子垂直居中的3种方法
div盒子垂直居中的3种方法
75 2
|
5月前
|
前端开发 UED
如何使用css给一个盒子加上好看的hover阴影效果
如何使用css给一个盒子加上好看的hover阴影效果
156 1
|
10月前
|
前端开发
css flex布局两个元素水平居中垂直居中
css flex布局两个元素水平居中垂直居中
175 1
|
前端开发
DIV+CSS文字与图片上下垂直居中-div css居中
DIV+CSS文字与图片上下垂直居中-div css居中
93 0
|
前端开发
【CSS】解决图片和盒子底部产生的缝隙问题
【CSS】解决图片和盒子底部产生的缝隙问题
|
前端开发 小程序
CSS 盒子垂直居中的方法
CSS 盒子垂直居中的方法
194 0
【css3】盒子居中的8中方法
【css3】盒子居中的8中方法
85 0
CSS3盒子居中模型,如何让盒子居中。
CSS3盒子居中模型,如何让盒子居中。
121 0
CSS3盒子居中模型,如何让盒子居中。
|
前端开发 容器
CSS样式(三) - div盒子
CSS样式(三) - div盒子
195 0
CSS样式(三) - div盒子