Douglas Gaskell Douglas Gaskell - 2 months ago 7
CSS Question

Border-like transition causes content to shift

Title pretty much sums it up, here is a demonstration and the CSS thus far.



.edit.input {
display: inline-block;
}

.edit.input input {
border: none;
border-bottom: 1px solid #D4D4D5;
}

.edit.input input:focus {
outline: none;
border: transparent;
}

.bar {
display: block;
}

.bar:after {
content: '';
display: block;
transform: scaleX(0);
bottom: -2px;
height: 2px;
background: #48afb9;
transition: 300ms ease all;
}

.edit.input input:focus ~ .bar:after {
transform: scaleX(1);
}

<div class="edit input">
<input type="text">
<span class="bar"></span>
</div>

<br>
<br> stuff
<br> other stuff





https://jsfiddle.net/a554h0oo/

What I am trying to achieve:

enter image description here

This is what I get:

enter image description here

Answer

When you set border : transparent, you are reseting the border width to 1 in the top .

Set border-color instead

.edit.input {
    display: inline-block;
}

.edit.input input {
    border: none;
    border-bottom: 1px solid #D4D4D5;
}

.edit.input input:focus {
  outline: none;
    border-color: transparent; /* changed */
}

.bar {
    display: block;
}

.bar:after {
    content: '';
    display: block;
    transform: scaleX(0);
    bottom: -2px;
    height: 2px;
    background: #48afb9;
    transition: 300ms ease all;
}

.edit.input input:focus ~ .bar:after {
    transform: scaleX(1);
}
<div class="edit input">
    <input type="text">
    <span class="bar"></span>
</div>

<br>
<br> stuff
<br> other stuff