Make border-bottom animation from left to right [duplicate] - css

I am trying to replicate this transition from uber.design site:
The thing is that i am stuck at reversing the transition:
.un {
display: inline-block;
}
.un:after {
content: '';
width: 0px;
height: 2px;
display: block;
background: black;
transition: 300ms;
}
.un:hover:after {
width: 100%;
<span class="un">Underlined Text</span>

You can use gradient and adjust background-position with a delay to obtain such effect:
.un {
display: inline-block;
padding-bottom:2px;
background-image: linear-gradient(#000 0 0);
background-position: 0 100%; /*OR bottom left*/
background-size: 0% 2px;
background-repeat: no-repeat;
transition:
background-size 0.3s,
background-position 0s 0.3s; /*change after the size immediately*/
}
.un:hover {
background-position: 100% 100%; /*OR bottom right*/
background-size: 100% 2px;
}
<span class="un">Underlined Text</span>
In case you want a continuous animation on hover you can try this:
.un {
display: inline-block;
padding-bottom:2px;
background-image: linear-gradient(#000 0 0);
background-position: right -100% bottom 0;
background-size: 200% 2px;
background-repeat: no-repeat;
}
.un:hover {
background-position: left -100% bottom 0;
transition: background-position 0.5s;
}
<span class="un">Underlined Text</span>
You can check this answer for more details about how the calculation of the different value is done: Using percentage values with background-position on a linear-gradient
Another kind of animation
.un {
display: inline-block;
padding-bottom:2px;
background-image: linear-gradient(to right, #000 33%,#0000 33% 66%,#000 66%);
background-position: right bottom;
background-size: 300% 2px;
background-repeat: no-repeat;
}
.un:hover {
background-position: left bottom;
transition: background-position 0.5s;
}
<span class="un">Underlined Text</span>
let's don't forget the basic one:
.un {
display: inline-block;
padding-bottom:2px;
background-image: linear-gradient(#000 0 0);
background-position: right bottom; /* OR left bottom*/
background-size: 100% 2px;
background-repeat: no-repeat;
transition: background-size 0.5s;
}
.un:hover {
background-size: 0% 2px;
}
<span class="un">Underlined Text</span>
You can find more techniques here: https://dev.to/afif/100-underline-overlay-animation-the-ultimate-css-collection-4p40
Another related article: Cool Hover Effects That Use Background Properties

You'll need your pseudo element to be absolute positioned and use the :not selector to reproduce this effect.
.un {
display: inline-block;
position: relative;
}
.un:after {
content: '';
width: 0px;
height: 2px;
position: absolute;
top: 100%;
left: 0;
background: black;
transition: 300ms;
}
.un:hover:after {
width: 100%;
}
.un:not(:hover):after {
right: 0;
left: auto;
}
<span class="un">Underlined Text</span>

The easiest solution of all, without :not selector or gradients, is to switch between right and left positions such as in the code.
span.un {
position: relative;
}
span.un::after {
position: absolute;
content: "";
background: black;
bottom: 0;
right: 0;
height: 2px;
width: 0%;
transition: 300ms ease-in-out;
}
span.un:hover::after {
width: 100%;
left: 0;
}
<span class="un">Underline me</span>

Related

CSS animated underline goes from left to right, and I need it from right to left [duplicate]

I am trying to replicate this transition from uber.design site:
The thing is that i am stuck at reversing the transition:
.un {
display: inline-block;
}
.un:after {
content: '';
width: 0px;
height: 2px;
display: block;
background: black;
transition: 300ms;
}
.un:hover:after {
width: 100%;
<span class="un">Underlined Text</span>
You can use gradient and adjust background-position with a delay to obtain such effect:
.un {
display: inline-block;
padding-bottom:2px;
background-image: linear-gradient(#000 0 0);
background-position: 0 100%; /*OR bottom left*/
background-size: 0% 2px;
background-repeat: no-repeat;
transition:
background-size 0.3s,
background-position 0s 0.3s; /*change after the size immediately*/
}
.un:hover {
background-position: 100% 100%; /*OR bottom right*/
background-size: 100% 2px;
}
<span class="un">Underlined Text</span>
In case you want a continuous animation on hover you can try this:
.un {
display: inline-block;
padding-bottom:2px;
background-image: linear-gradient(#000 0 0);
background-position: right -100% bottom 0;
background-size: 200% 2px;
background-repeat: no-repeat;
}
.un:hover {
background-position: left -100% bottom 0;
transition: background-position 0.5s;
}
<span class="un">Underlined Text</span>
You can check this answer for more details about how the calculation of the different value is done: Using percentage values with background-position on a linear-gradient
Another kind of animation
.un {
display: inline-block;
padding-bottom:2px;
background-image: linear-gradient(to right, #000 33%,#0000 33% 66%,#000 66%);
background-position: right bottom;
background-size: 300% 2px;
background-repeat: no-repeat;
}
.un:hover {
background-position: left bottom;
transition: background-position 0.5s;
}
<span class="un">Underlined Text</span>
let's don't forget the basic one:
.un {
display: inline-block;
padding-bottom:2px;
background-image: linear-gradient(#000 0 0);
background-position: right bottom; /* OR left bottom*/
background-size: 100% 2px;
background-repeat: no-repeat;
transition: background-size 0.5s;
}
.un:hover {
background-size: 0% 2px;
}
<span class="un">Underlined Text</span>
You can find more techniques here: https://dev.to/afif/100-underline-overlay-animation-the-ultimate-css-collection-4p40
Another related article: Cool Hover Effects That Use Background Properties
You'll need your pseudo element to be absolute positioned and use the :not selector to reproduce this effect.
.un {
display: inline-block;
position: relative;
}
.un:after {
content: '';
width: 0px;
height: 2px;
position: absolute;
top: 100%;
left: 0;
background: black;
transition: 300ms;
}
.un:hover:after {
width: 100%;
}
.un:not(:hover):after {
right: 0;
left: auto;
}
<span class="un">Underlined Text</span>
The easiest solution of all, without :not selector or gradients, is to switch between right and left positions such as in the code.
span.un {
position: relative;
}
span.un::after {
position: absolute;
content: "";
background: black;
bottom: 0;
right: 0;
height: 2px;
width: 0%;
transition: 300ms ease-in-out;
}
span.un:hover::after {
width: 100%;
left: 0;
}
<span class="un">Underline me</span>

CSS - yet another transition not working question

Trying to understand transition css property, but can't seem to figure out why this code is not working. Border needs to go from solid to dotted
.home {
color: #ff652f;
font-weight: 700;
border-bottom: 18px solid #ff652f;
-webkit-transition: border-bottom 3s ease-in-out;
transition: border-bottom 3s ease-in-out;
}
.home {
border-bottom: 18px dashed #ff652f;
}
Made a jsfiddle here - https://jsfiddle.net/h7925b8g/
Would like the transition to happen slowly. Any ideas what I am doing wrong? Any help is greatly appreciated!
As mentioned in comments, border-style is not animatable, so you can't simply use the transition property to change it.
Instead, you can fake it. How exactly you pull this off depends on what you want the transition to look like. One approach is to use a repeating linear-gradient for the dashed effect and then transition that to overlay the border (either a literal border or just some other element that acts like a border).
For example, sliding up from the bottom:
.home {
color: #ff652f;
font-weight: 700;
width: 200px;
padding-bottom: 10px;
position: relative;
}
.home::before,
.home::after {
content: '';
display: block;
width: 100%;
position: absolute;
bottom: 0;
left: 0;
}
.home::before {
height: 10px;
background-color: orange;
z-index: 0;
}
.home::after {
height: 0px;
transition: height 350ms ease;
background-image: linear-gradient(to right, white 0px 10px, orange 10px 20px, white 20px);
background-size: 20px 100%;
background-repeat: repeat;
z-index: 1;
}
.home:hover::after {
height: 10px;
}
<div class="home">Hover me!</div>
Or sliding in from the left:
.home {
color: #ff652f;
font-weight: 700;
width: 200px;
padding-bottom: 10px;
position: relative;
}
.border-animation {
display: block;
position: absolute;
bottom: 0;
left: 0;
z-index: 0;
width: 100%;
height: 10px;
overflow: hidden;
background-color: orange;
}
.border-animation::after {
content: '';
display: block;
width: 100%;
height: 100%;
position: absolute;
bottom: 0;
left: 0;
z-index: 1;
background-image: linear-gradient(to right, white 0px 10px, orange 10px 20px, white 20px);
background-size: 20px 100%;
background-repeat: repeat;
transform: translateX(-100%);
transition: transform 350ms ease;
}
.home:hover .border-animation::after {
transform: translateX(0);
}
<div class="home">Hover me!<span class="border-animation"></span></div>

animate speedometer needle using only CSS

I am trying to animate a speedometer - going from left (green) to right (red). Once the animation has run am I trying also trying to make the needle loop at the end/red area of the barometer. How can I achieve this using only CSS?
#speedometer {
display: inline-block;
position: relative;
}
#speedometer .barometer {
background-image: url("https://svgshare.com/i/GAZ.svg");
background-repeat: no-repeat;
width: 200px;
height: 200px;
display: inline-block;
}
#speedometer .needle {
background-image: url("https://svgshare.com/i/GBP.svg");
background-repeat: no-repeat;
z-index: 999999;
width: 200px;
height: 250px;
display: inline-block;
left: 0px;
position: absolute;
top: 0px;
}
<div id="speedometer">
<span class="barometer"></span>
<span class="needle"></span>
</div>
You can first adjust the dimension of the needle element and the transform-origin then simply use a rotation:
#speedometer {
display: inline-block;
position: relative;
}
#speedometer .barometer {
background-image: url("https://svgshare.com/i/GAZ.svg");
background-repeat: no-repeat;
width: 200px;
height: 110px;
display: inline-block;
}
#speedometer .needle {
background-image: url("https://svgshare.com/i/GBP.svg");
background-repeat: no-repeat;
z-index: 999999;
width: 200px;
height: 110px;
display: inline-block;
left: 0px;
position: absolute;
bottom: 5px;
animation:
change 3s linear,
loop 1s linear 3s infinite alternate;
transform-origin:50% calc(100% - 8px) ;
}
#keyframes change {
0% {
transform:rotate(-90deg);
}
50% {
transform:rotate(0deg);
}
100% {
transform:rotate(90deg);
}
}
#keyframes loop {
0% {
transform:rotate(90deg);
}
100% {
transform:rotate(70deg);
}
}
<div id="speedometer">
<span class="barometer"></span>
<span class="needle"></span>
</div>

Changing background-position-x on :hover

I am trying to get a little hover animation going by moving the background image on the x-axis. However, I can't seem to find how to capture the element:hover. Does anyone has an idea what is wrong? Is the selector below valid?
Thank you for your help
div.card:hover .cardbg {
}
.card {
border-radius:15px;
border:none;
height: 280px;
position: relative;
background: linear-gradient(to bottom right,#ffec99 0,#ffb549 100%);
z-index: -1;
font-family: 'Arial';
}
.cardbg {
width: 100%;
height: 100%;
background-image: url('images/atom-original.svg');
opacity: 0.2;
background-size: cover;
background-repeat: no-repeat;
background-position-x: 100px;
transition: .2s ease all;
position: absolute;
top: 0;
left: 0;
}
div.card:hover .cardbg {
background-position-x: 10px;
background-color: red;
}
<div class="card">
<div class="cardbg">
</div>
</div>
Try removing the z-index: -1 from the .card class. That worked for me.

Duplicate image and set hover CSS

I want to make the following animation:
One div with 2 same arrows and on hover first arrow should move on left/right. I tried to do it, but it unsuccessfully. I'm setting background with 2 images, but how I can set animation for 1 of the images like the gif?
.arrow-right2 {
content: "";
background: transparent url(https://i.imgur.com/u7cYXIo.png) 0 -185px no-repeat, transparent url(https://i.imgur.com/u7cYXIo.png) 0 -185px no-repeat;
height: 35px;
position: absolute;
top: -5%;
left: 0;
width: 35px;
}
Try to use 2 different divs with the same arrows, with position absolute and use this to overlap the two arrows. If you can, use a single image, not a sprite. Then apply the effect on hover on one of the images.
body {
background: red;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
display: flex;
justify-content: center;
align-items: center;
width: 50px;
height: 50px;
position: relative;
}
.arrow1 {
background: url('https://i.imgur.com/u7cYXIo.png') no-repeat -17px -199px;
width: 12px;
height: 24px;
display: block;
left: 0;
position: absolute;
}
.arrow2 {
background: url('https://i.imgur.com/u7cYXIo.png') no-repeat -17px -199px;
width: 12px;
height: 24px;
display: block;
position: absolute;
transition: all 0.4s;
left: 0;
}
.arrow2:hover {
left: -10px;
transition: all 0.4s;
}
<div class="container">
<div class="arrow1">
</div>
<div class="arrow2">
</div>
</div>
You can adjust background-position like below. You start with a different position for each one then you make them the same:
.arrow {
background:
url(https://i.imgur.com/u7cYXIo.png) -10px -185px,
url(https://i.imgur.com/u7cYXIo.png) 10px -185px,
red;
background-repeat:no-repeat;
height: 50px;
width: 50px;
transition:all 0.5s;
}
.arrow:hover {
background-position:10px -185px;
}
<div class="arrow"></div>
Or the opposite
.arrow {
background:
url(https://i.imgur.com/u7cYXIo.png),
url(https://i.imgur.com/u7cYXIo.png),
red;
background-position:10px -185px;
background-repeat:no-repeat;
height: 50px;
width: 50px;
transition:all 0.5s;
}
.arrow:hover {
background-position:
-10px -185px,
10px -185px;
}
<div class="arrow"></div>
And if you want to adjust coloration you can consider mix-blend-mode
.arrow {
background:
url(https://i.imgur.com/u7cYXIo.png),
url(https://i.imgur.com/u7cYXIo.png),
#000;
background-position:10px -185px;
background-repeat:no-repeat;
height: 50px;
width: 50px;
transition:all 0.5s;
position:relative;
}
.arrow:before {
content:"";
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
background: red;
mix-blend-mode: multiply;
opacity:0;
transition:all 0.5s;
}
.arrow:hover {
background-position:
-10px -185px,
10px -185px;
}
.arrow:hover:before {
opacity:1;
}
<div class="arrow"></div>

Resources