Search code examples
htmlcssmargin

Why are my input field shifted to the right?


I have a small webpage with a container and a form inside it. When I play around with the margins and paddings, the input fields seem to get shifted to the right, even overflowing the window, despite their width being at 100%

How would I go about fixing that, and am I doing it wrong in the first place?

Code:

.mini-window {
    font-family: "Lato", sans-serif;
    width: 33%;
    margin:auto;
    -webkit-box-shadow: 10px 10px 40px -20px rgba(0,0,0,0.75);
    -moz-box-shadow: 10px 10px 40px -20px rgba(0,0,0,0.75);
    box-shadow: 10px 10px 40px -20px rgba(0,0,0,0.75);
    padding: 20px;
    margin-top: 200px;
    text-align: center;
}

input {
    width: 100%;
    font-size: 17px;
    margin: 10px;
    padding: 5px;
}
<div class="center-container">
    <div class="mini-window">
        <h1>Registration</h1>
        <form>
            <input id="email" type="text" placeholder="Email">
            <input id="password" type="password" placeholder="Password">
            <input id="repeatPassword" type="password" placeholder="Repeat password">
            <input id="organization" type="text" placeholder="Organization">
            <input id="first" type="text" placeholder="First name">
            <input id="last" type="text" placeholder="Last name">
            <button type="button" onclick="registerClick()">Register</button>
        </form>
        <p id="errorMessage" class="error"></p>
    </div>
</div>


Solution

  • The width of your inputs is 100% of the width of the containing div, with a margin of 10px you will of course end up with that result.

    Changing the width to 90% for instance is a simple way of solving it.

    .mini-window {
        font-family: "Lato", sans-serif;
        width: 33%;
        margin:auto;
        -webkit-box-shadow: 10px 10px 40px -20px rgba(0,0,0,0.75);
        -moz-box-shadow: 10px 10px 40px -20px rgba(0,0,0,0.75);
        box-shadow: 10px 10px 40px -20px rgba(0,0,0,0.75);
        padding: 20px;
        margin-top: 200px;
        text-align: center;
    }
    
    input {
        width: 90%;
        font-size: 17px;
        margin: 10px;
        padding: 5px;
    }
    <div class="center-container">
        <div class="mini-window">
            <h1>Registration</h1>
            <form>
                <input id="email" type="text" placeholder="Email">
                <input id="password" type="password" placeholder="Password">
                <input id="repeatPassword" type="password" placeholder="Repeat password">
                <input id="organization" type="text" placeholder="Organization">
                <input id="first" type="text" placeholder="First name">
                <input id="last" type="text" placeholder="Last name">
                <button type="button" onclick="registerClick()">Register</button>
            </form>
            <p id="errorMessage" class="error"></p>
        </div>
    </div>