skip to Main Content

I’m testing some scenarios with flexbox while learning HTML and CSS. I can’t for the life of me get the user-input div centered in the middle of the login-box div. I was able to get it centered vertically with align-items: center; but I can’t get it to move down without using margins (which I would prefer to avoid).

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>
            Login
        </title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <div class="page-container">
            <div class="login-box">
                <div class="user-input">
                    <input class="email" type="text" placeholder="Email">
                    <input class="password" type="text" placeholder="Password">
                    <button class="login-button">Login</button>
                </div>
            </div>
        </div>
    </body>
</html>

CSS:

.page-container {
    display: flex;
    height: 100vh;
    justify-content: center;
    align-items: center;
}

.login-box {
    height: 350px;
    width: 250px;
    box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.41);
    border-radius: 10px;
}

.user-input {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.email,
.password {
    width: 150px;
    border-radius: 5px;
    border: .5px solid black;
}

.login-button {
    height: 25px;
    width: 100px;
    border: none;
    background-color: darkblue;
    color: white;
    border-radius: 5px;
    margin-top: 10px;
    cursor: pointer;
}

The login-box div is the rectangular shadow box in the middle of the screen. The user-input is where I have a column flexbox created.

Here's a screenshot.

2

Answers


  1. You could use a CSS Grid approach. Then, place the contents in the centre.

    .login-box {
        display: grid;
        place-content: center;
        height: 350px;
        width: 250px;
        box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.41);
        border-radius: 10px;
    }
    
    Login or Signup to reply.
  2. change .login box styling:

    .page-container {
            display: flex;
            height: 100vh;
            justify-content: center;
            align-items: center;
        }
    
        .login-box {
            height: 350px;
            width: 250px;
            box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.41);
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    
        .user-input {
            display: flex;
            flex-direction: column;
            align-items: center;
        }
    
        .email,
        .password {
            width: 150px;
            border-radius: 5px;
            border: .5px solid black;
        }
    
        .login-button {
            height: 25px;
            width: 100px;
            border: none;
            background-color: darkblue;
            color: white;
            border-radius: 5px;
            margin-top: 10px;
            cursor: pointer;
        }
    <!DOCTYPE html>
    <html>
    
    <head>
        <title>
            Login
        </title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <div class="page-container">
            <div class="login-box">
                <div class="user-input">
                    <input class="email" type="text" placeholder="Email">
                    <input class="password" type="text" placeholder="Password">
                    <button class="login-button">Login</button>
                </div>
            </div>
        </div>
    </body>
    
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search