Sam Coutteau
Sam Coutteau

Reputation: 417

Text nudges right on css animation

Whilst following this tutorial for an animated login screen using html and css. I noticed that whenever you focused on an input field the text would nudge a tiny bit like this:

illustration of the problem

How does one fix this? From testing I found it's related to the text centering.

For reference here is the original code

CSS:

body{
  margin: 0;
  padding: 0;
  font-family: sans-serif;
  background: #34495e;
}
.box{
  width: 300px;
  padding: 40px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background: #191919;
  text-align: center;
}
.box h1{
  color: white;
  text-transform: uppercase;
  font-weight: 500;
}
.box input[type = "text"],.box input[type = "password"]{
  border:0;
  background: none;
  display: block;
  margin: 20px auto;
  text-align: center;
  border: 2px solid #3498db;
  padding: 14px 10px;
  width: 200px;
  outline: none;
  color: white;
  border-radius: 24px;
  transition: 0.25s;
}
.box input[type = "text"]:focus,.box input[type = "password"]:focus{
  width: 280px;
  border-color: #2ecc71;
}
.box input[type = "submit"]{
  border:0;
  background: none;
  display: block;
  margin: 20px auto;
  text-align: center;
  border: 2px solid #2ecc71;
  padding: 14px 40px;
  outline: none;
  color: white;
  border-radius: 24px;
  transition: 0.25s;
  cursor: pointer;
}
.box input[type = "submit"]:hover{
  background: #2ecc71;
}

Upvotes: 1

Views: 470

Answers (1)

UModeL
UModeL

Reputation: 1227

If you mean a shiver during the transition, then it really is related to the recalculation of centering. And if we are talking about a shift to the right, then you just need to subtract the double border width from the final transition size.

body {
  margin: 0;
  padding: 0;
  font-family: sans-serif;
  background: #34495e;
}

.box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 300px;
  padding: 40px;
  text-align: center;
  background: #191919;
}

.box h1 {
  color: white;
  text-transform: uppercase;
  font-weight: 500;
}

.box input[type="text"],
.box input[type="password"] {
  margin: 20px auto;
  display: block;
  width: 200px;
  padding: 14px 10px;
  text-align: center;
  border: 2px solid #3498db;
  border-radius: 24px;
  outline: none;
  color: white;
  background: none;
  transition: 0.25s;
}

.box input[type="text"]:focus,
.box input[type="password"]:focus {
  width: 276px;
  border-color: #2ecc71;
}

.box input[type="submit"] {
  display: block;
  margin: 20px auto;
  padding: 14px 40px;
  text-align: center;
  border: 2px solid #2ecc71;
  border-radius: 24px;
  outline: none;
  color: white;
  background: none;
  cursor: pointer;
  transition: 0.25s;
}

.box input[type="submit"]:hover {
  background: #2ecc71;
}
<form class="box" action="index.html" method="post">
  <h1>Login</h1>
  <input type="text" name="" placeholder="Username">
  <input type="password" name="" placeholder="password">
  <input type="submit" name="" value="Login">
</form>

Upvotes: 1

Related Questions