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>
相关文章
|
3月前
div盒子垂直居中的3种方法
div盒子垂直居中的3种方法
30 2
|
5天前
|
前端开发
css flex布局两个元素水平居中垂直居中
css flex布局两个元素水平居中垂直居中
15 1
让里面的DIV上下左右居中在外面的DIV里
让里面的DIV上下左右居中在外面的DIV里
45 0
CSS3盒子居中模型,如何让盒子居中。
CSS3盒子居中模型,如何让盒子居中。
79 0
CSS3盒子居中模型,如何让盒子居中。
|
前端开发 容器
CSS样式(三) - div盒子
CSS样式(三) - div盒子
146 0
CSS样式(三) - div盒子
|
前端开发
CSS布局之居中
CSS布局之居中本文主要是介绍水平居中,垂直居中,还有水平垂直居中的方法水平居中 1.行内元素水平居中 使用text-align:center;就可以实现行内元素的水平居中,但是记得要在父元素中设置,会对子元素生效。
1347 0
|
前端开发
span内文字居中css布局方法_让span内容居中
display:block,让span形成块(独占一行),就像DIV一样,在设置内容居中css text-align:center。
2105 0
|
前端开发
div 居中方法汇总
居中问题无论在工作还是在面试中, 都是很常见的 下面我来给大家总结几种常用的方法, 大家可根据实际情况选择 好了, 废话不多说, 先看下效果 法1 定位+偏移 (margin) html css .
1019 0
|
容器 前端开发
内联元素垂直居中方法汇总
1. 让 line-height 与 height 相等 .container { width: 1rem; height: 1rem; line-height: 1rem; font-size: 0.
1627 0