在使用Element时无法居中卡片组件(元素)

1,使用技术(产品)
HTML5,Vue,Element
2,问题描述
image.png如图,主卡片无法居中
3,预期结果(期望解决的问题)
使得<div id="app"><el-card class="box-card" style="width:480px;margin:auto;">垂直居中
4,参考资料
Element相关文档
5,已尝试过的解决方案(失败)
(1)

#app{
display:flex;
justify-content: center;
align-items: center;
}

(2)

#app{
position:relative;
.box-card{
    position: absolute;
    top: 0;
    left: 0;
    right:0;
    bottom: 0;
    margin: auto;
    }
}

6,代码(前端HTML)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Login - Unified authentication for Lime Network users</title>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <script src="https://cdn.staticfile.org/element-ui/2.13.2/index.js"></script>
        <script src="https://cdn.staticfile.org/element-ui/2.13.2/locale/en.min.js"></script>
        <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.13.2/theme-chalk/index.css">
    </head>
    
    <body>
        <div id="app">
            <el-card class="box-card" style="width:480px;margin:auto;">
                <p style="text-align:center">Login - Unified authentication for Lime Network users</p>
                <el-form ref="form" :model="form" status-icon :rules="rules" label-position="left" label-width="auto">
                    <el-form-item label="Username" prop="username">
                        <el-input v-model="form.username" prefix-icon="el-icon-user"></el-input>
                    </el-form-item>
                    <el-form-item label="Password" prop="password">
                        <el-input v-model="form.password" prefix-icon="el-icon-lock" show-password></el-input>
                    </el-form-item>
                    <el-form-item label="Remember me">
                        <el-switch v-model="form.remember"></el-switch>
                    </el-form-item>
                    <el-form-item label="Other ways">
                        <el-button type="text" @click="oauth_github()">GitHub</el-button>
                        <el-button type="text" @click="oauth_gitee()">Gitee</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('form')">Login</el-button>
                        <el-button @click="resetForm('form')">Reset</el-button>
                    </el-form-item>
                </el-form>
            </el-card>
        </div>
        <script>
        Script部分省略
        </script>
    </body>

</html>
阅读 236
评论
    2 个回答
    • 65

    试试将 #app 元素撑开

    #app{
        position: relative;
        width: 100%;
        height: 100vh;
        display:flex;
        justify-content: center;
        align-items: center;
    }

    另外可能会出现的展开元素后页面出现滚动条,尝试设置 body 元素的 marginpadding 为 0px

      太长了不知道你在讲啥,垂直居中可以使用flex布局或者绝对定位

      #app{
          display:flex;
          justify-content: center;
          align-items: center;
      }
      #app{
          position:relative;
          .box-card{
              position: absolute;
              top: 0;
              left: 0;
              right:0;
              bottom: 0;
              margin: auto;
          }
      }

      你之所以不能垂直居中是因为你的#app的高度是给子元素撑开的,所以当然得不到你想要的垂直居中 给他个height:100vh
      百度一下还有别的

        撰写回答

        登录后参与交流、获取后续更新提醒