flex实现登录框水平垂直居中

编程入门 行业动态 更新时间:2024-10-14 14:16:54

在我们的布局中,水平居中很容易,但垂直居中稍微有点复杂。下面就给大家分享下通过Flex布局轻松实现登录框在水平、垂直方向上的居中效果。
完整代码:

* {
    margin: 0;
    padding: 0;
}

body {
    height: 100vh;
    display: flex;
    /*登录框水平垂直居中*/
    justify-content: center;
    align-items: center;
    background-color: #f2f2f2;
}

main {
    width: 700px;
    background-color: white;
    box-shadow: 2px 2px 10px #808080;
}

h3 {
    font-size: 2.5rem;
    letter-spacing: 6px;
    text-align: center;
    font-weight: 500;
    margin-top: 48px;
}

form {
    margin: 60px 80px 100px;
}

form > p {
    margin-bottom: 20px;
    display: flex;
}

form > p > input {
    flex: 1;
    height: 52px;
    border: 1px solid #c3c6cf;
    border-radius: 3px;
    padding-left: 6px;
    transition: linear 0.4s;
}



form > p > input:hover,
form > p > input:active,
form > p > input:focus{
    border-color: #464d5f;
    outline: none;
    box-shadow: 2px 8px 8px rgba(70, 77, 95, 0.15);
}

form > p:last-child {
    margin: 56px 0 0;
}

form > p:last-child > input{
    border: none;
}
form > p:last-child > input:hover,
form > p:last-child > input:focus{
    border: none;
    box-shadow: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>flex实现用户登录水平垂直居中</title>
    <link rel="stylesheet" href="css/style4.css">
</head>
<body>
<main>
    <h3>用户登录</h3>
    <form action="" method="post">
        <p>用户:</p>
        <p>
            <input type="text" id="username" name="username">
        </p>
        <p>密码:</p>
        <p>
            <input type="password" id="password" name="password">
        </p>
        <p>
            <input type="submit" id="submit" value="登录">
        </p>
    </form>
</main>
</body>
</html>

代码效果:

总结:felx 设置元素水平垂直居中布局非常方便,三句代码搞定:

 /*登录框水平垂直居中*/
    justify-content: center;
    align-items: center;
    background-color: #f2f2f2;
THE END !

更多推荐

flex实现登录框水平垂直居中

本文发布于:2023-06-13 12:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:水平   flex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!