1

I have a problem. I am making a register form in HTML and CSS. I putted a border on focus of the input (once you click, border around input appears), but then another inputs start moving around. How can I fix that movements? Here's my code:

input {
  width: 450px;
  height: 3.5vh;
  margin-bottom: 1rem;
  outline: none;
  color: white;
  border: 1px solid rgba(170, 170, 170, 0.3);
  background: rgba(0, 0, 0, 0.5);
  font-family: 'Poppins', sans-serif;
  font-weight: bold;
  border-radius: 3px;
}

input:focus {
  border: 2px solid blueviolet;
  outline: none!important;
}
<div class="inputs">
  <input type="text" placeholder="User Name" name="name">
</div>
<div class="inputs">
  <input type="email" placeholder="Email" name="email">
</div>
<div class="inputs">
  <input type="password" placeholder="Password" name="password">
</div>
<div class="inputs">
  <input type="password" placeholder="Confirm Password" name="confirmit">
</div>
<span class="img-text">Choose avatar: </span>
<input type="file" id="img" accept="image/*" style="border: none!important; background: none!important;">
<button class="register" type="submit" name="register">Register</button>

I would really appreciate help.

1
  • This is because you're increasing the border size on focus so it will look as if things are moving. You could set your border to 1px on input:focus and it will fix the issue. You could also set an outline of transparent 2px on the input, then on focus set the outline to 2px solid. Commented Oct 15, 2020 at 10:38

5 Answers 5

2

On default input style, you have set border-width to 1px.

But when focused, you have set the border-width to 2px. Due to this increment, the input width and height are increased when the focus.

So it is needed to set the border-width same on default & focused as follows.

In the below snippet, I have set the default input style border-width to 2px so same as focused input.

input {
  width: 450px;
  height: 3.5vh;
  margin-bottom: 1rem;
  outline: none;
  color: white;
  border: 2px solid rgba(170, 170, 170, 0.3);
  background: rgba(0, 0, 0, 0.5);
  font-family: 'Poppins', sans-serif;
  font-weight: bold;
  border-radius: 3px;
}

input:focus {
  border: 2px solid blueviolet;
  outline: none!important;
}
<div class="inputs">
  <input type="text" placeholder="User Name" name="name">
</div>
<div class="inputs">
  <input type="email" placeholder="Email" name="email">
</div>
<div class="inputs">
  <input type="password" placeholder="Password" name="password">
</div>
<div class="inputs">
  <input type="password" placeholder="Confirm Password" name="confirmit">
</div>
<span class="img-text">Choose avatar: </span>
<input type="file" id="img" accept="image/*" style="border: none!important; background: none!important;">
<button class="register" type="submit" name="register">Register</button>

Sign up to request clarification or add additional context in comments.

Comments

2

This is happening because you are adding a border on focus but initially, there is no border present in the input tag that is the reason you are facing fluctuations in the UI.

To fix that add border property in the input as well with the transparency.

input {
    width: 450px;
    height: 3.5vh;
    margin-bottom: 1rem;
    outline: none;
    color: white;
    background: rgba(0, 0, 0, 0.5);
    font-family: 'Poppins', sans-serif;
    font-weight: bold;
    border-radius: 3px;
    border: solid 2px transparent;
}

1 Comment

Correct there should be an only a single border.
1

If you want to have increasing border widths, then you could use an inset box shadow instead.

Also it's good practice to add * { box-sizing: border-box } to avoid issues like this.

* {
  box-sizing: border-box;
}

input {
  width: 450px;
  height: 3.5vh;
  margin-bottom: 1rem;
  outline: none;
  color: white;
  border: none;
  box-shadow: inset 0 0 0 1px rgba(170, 170, 170, 0.3);
  background: rgba(0, 0, 0, 0.5);
  font-family: 'Poppins', sans-serif;
  font-weight: bold;
  border-radius: 3px;
  min-height: 40px;
  padding-left: 10px;
}

input:focus {
  box-shadow: inset 0 0 0 2px blueviolet;
  outline: none!important;
}
<div class="inputs">
  <input type="text" placeholder="User Name" name="name">
</div>
<div class="inputs">
  <input type="email" placeholder="Email" name="email">
</div>
<div class="inputs">
  <input type="password" placeholder="Password" name="password">
</div>
<div class="inputs">
  <input type="password" placeholder="Confirm Password" name="confirmit">
</div>
<span class="img-text">Choose avatar: </span>
<input type="file" id="img" accept="image/*" style="border: none!important; background: none!important;">
<button class="register" type="submit" name="register">Register</button>

Comments

0
input {
  width: 450px;
  height: 3.5vh;
  margin-bottom: 1rem;
  outline: none;
  color: white;
  border: 1px solid rgba(170, 170, 170, 0.3);
  background: rgba(0, 0, 0, 0.5);
  font-family: 'Poppins', sans-serif;
  font-weight: bold;
  border-radius: 3px;
  border: 2px solid transparent; /* input has border 2px when it has focus that's why here border 2px */
}

input:focus {
  border: 2px solid blueviolet;
  outline: none!important;
}

Comments

0

i went through your code, default border size is 1px and when it's focused, you made the border size 2px that's why other input are moving around.

/*either change input initial border size to 2px like/*
 input {
      width: 450px;
      height: 3.5vh;
      margin-bottom: 1rem;
      outline: none;
      color: white;
      border: 2px solid rgba(170, 170, 170, 0.3);
      background: rgba(0, 0, 0, 0.5);
      font-family: 'Poppins', sans-serif;
      font-weight: bold;
      border-radius: 3px;
    }
    /* or change focused input border size to 1px like/*
    input:focus {
      border: 1px solid blueviolet;
      outline: none!important;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.