Sliding border from right to left using CSS
Asked Answered
H

2

12

I have a header that I want to have a border on that slides from right to left on hover, using pure CSS if that's possible.

At the moment, I can get a border that slides from left to right using the following CSS:

#header a:after {
content: '';
display: block;
border-bottom: 3px solid $(header.hover.color);
width: 0;
-webkit-transition: 1s ease;
        transition: 1s ease;
}

#header a:hover:after { width: 100%; }

Has anyone a good solution for accomplishing this task?

Hermineherminia answered 2/7, 2015 at 9:30 Comment(0)
W
26

You can position your :after element to the right

#header {
  position: relative;
}
#header a:after {
  content: '';
  display: block;
  border-bottom: 3px solid red;
  width: 0;
  position: absolute;
  right: 0;
  -webkit-transition: 1s ease;
  transition: 1s ease;
}

#header a:hover:after { 
  width: 100%; 
}
Wapentake answered 2/7, 2015 at 9:37 Comment(3)
This worked like a charm! Thank you! I feel silly for not thinking of using <code>right:</code>Hermineherminia
Since you are only animating the width property I would write the transition property like this: transition: width 1s ease;Savoirfaire
Future readers: note that animating width isn't considered best practice. Try using a combination of "overflow: hidden" on the parent and "transform: translateX(-100%)" on the child. This allows you to slide it in without altering the box model which results in a higher FPS animation.Araxes
T
1

Give all:

-webkit-transition: 1s all ease;
        transition: 1s all ease;
Thibaut answered 2/7, 2015 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.