im very confused and have looked everywhere, im on the newest version of chrome and it should work. i just want it to transition left to right when hovered over. the line shows up when hovered but not transitioned. just note im not the best at coding just messing around.
.about {
height: 50px;
width: 200px;
text-align: center;
}
.about:hover::before {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 2px;
background-color: blue;
transform: scaleX(0);
/* Initially hidden */
transition: width .5s;
/* Smooth transition on hover */
display: block;
}
.about:hover::before {
width: 100%;
transform: scaleX(1);
/* Expand on hover */
transition: all .5s;
}
<div class="about">About</div>
3
Answers
The issue with your code is due to conflicting properties and incorrect initial state setup for the
::before
pseudo-element. Here’s the corrected and simplified code that ensures the underline transitions smoothly from left to right when hoveredKey Changes:
Initial State (width: 0): The line starts with zero width, making it invisible initially.
Hover State (width: 100%): On hover, the width grows smoothly to full width.
transition: width 0.5s ease;: This ensures a smooth animation for the width change.
Removed transform: scaleX: Using width alone simplifies the animation and avoids redundancy.
position: relative: Ensures the ::before pseudo-element is positioned relative to the .about element.
The main issue is you made the pseudo element exists only when hovering, so the animation won’t play because there’s nothing to animate when it’s not hovered.
Here’s the improved version:
A few additions needed.
The div itself needs to be positioned so the before picks up its width from that rather than from the next closest positioned ancestor.
The transition needs to start at the left so transition-origin needs to be set, otherwise it defaults to center.
The transition is not on the width – you are not altering that – but on the transform which is doing the scaling.