Apply transition for single pseudoclass - css

I want a transition on my button which should only be applied on hover but not on other pseudoclasses.
button {
width: 100px;
height: 50px;
color: white;
background-color: green;
transition: background-color 1s;
}
button:hover {
background-color: blue;
}
button:active {
background-color: red;
}
<button>Nice Button!</button>
So in this case I want it the transition from green to blue on hover but not from blue to red when active. How can I achieve it?

Set transition: none; on :active pseudo class + if you don't want a transition on mouseup, you can add
button:focus {
background-color: blue;
transition: none;
}
button {
width: 100px;
height: 50px;
color: white;
background-color: green;
transition: background-color 1s;
}
button:hover {
background-color: blue;
}
button:focus:hover {
background-color: blue;
transition: none;
}
button:active:hover {
background-color: red;
transition: none;
}
<button>Nice Button!</button>

I have to admit that your question is challenging !
My proposed solution:
button {
width: 100px;
height: 50px;
color: white;
background-color: green;
transition: background-color 2s;
}
button:hover {
background-color: blue;
}
button:active {
/* background-color: red; */
-webkit-animation: activate 0s 0s forwards;
animation: activate 0s 0s forwards;
}
#-webkit-keyframes activate {
0% {background-color: green;}
100% {background-color: red;}
}
#keyframes activate {
0% {background-color: green;}
100% {background-color: red;}
}
fiddle
The problem is that the base state is shared beteen the back transition from the hover and the activer pseudo classes, so I don'tthink this can be solved using only transitions.
The trick is not to change the color in the active state, it is still green inside, but it looks red :-)

Related

Animated underline not showing once link is selected

I'm using Bootstrap and SCSS to customize my CSS file. I have a nav-link like this:
.sidebar .nav-pills .nav-link:hover,
.sidebar .nav-pills .show > .nav-link {
#extend %underline-link;
background: linear-gradient(120deg, $sidebar-navitem-background-color 65%, $sidebar-navitem-background-color 65%);
}
.sidebar .nav-pills .nav-link.active,
.sidebar .nav-pills .show > .nav-link {
#extend %underline-link;
background: linear-gradient(120deg, darken($sidebar-navitem-background-color, 2) 65%, darken($sidebar-navitem-background-color, 2) 65%);
}
%underline-link {
color: $underline-link-color;
position: relative;
text-decoration: none;
transition: all 0.4s ease;
&:before {
content: "";
position: absolute;
width: 100%;
height: 3px;
bottom: 0px;
left: 0;
background: $underline-link-color;
visibility: hidden;
transform: scaleX(0);
transform-origin: center left;
transition: all 0.3s ease 0s;
}
&:hover {
transition: all 0.4s ease;
&:before {
visibility: visible;
transform: scaleX(1);
}
}
}
The line is animated as expected when hovering over the item. However, when the item is active, the line disappears.
How do I keep the line showing as long as the item is active?
As you want to make your active state the same as your hover state you need to add the :active pseudo class alongside your :hover pseudo class in your SCSS. That will ensure that when the user clicks the link (i.e. the link is active) the style remains the same. More information about active state on MDN.
I have created a codepen with different styles for hover and active to show the interaction happening. If :hover and :active have the same styles you would therefore see no difference between the two states.
&:hover,
&:active {
transition: all 0.4s ease;
&:before {
visibility: visible;
transform: scaleX(1);
}
}

No animation with CSS keyframe (Mozilla Firefox)

I'm trying to learn CSS. I tried to do a simple animation: changing the background color of a span by using keyframes, but nothing change/animate
My code looks like this :
HTML :
<span class="brand1">Test</span>
CSS :
`body{
margin: 0;
padding: 0;}
.brand1{
display: block;
font-size: 2em;
width: 10vw;
-moz-animation: test, 2s, infinite;
animation: test, 2s, infinite;
}
#header{
width: 100%;
background-color: teal;
}
#keyframes test{
from {background-color: tomato; }
to { background-color: violet; }
}
#-moz-keyframes test{
from {background-color: tomato; }
to { background-color: violet; }
}`
I use Mozilla, so I think that there shouldn't be any compatibility issues. So where is the problem in my code?
1) Because you have put commas in animation property, we need to separate property methods by using space and not by commas.
2) If you want to change the color of text you use color property which is used to change the color of fonts and not the background-color property it will change the background color of your webpage.
Here is the code I have made changes to it. You can have a look.
body{
margin: 0;
padding: 0;}
.brand1{
display: block;
font-size: 2em;
width: 10vw;
animation: test 1s infinite;
}
#keyframes test{
from {color: tomato; }
to { color: violet; }
}
<span class="brand1">Test</span>
The animation of your .brand1 is not correctly written, you need to separate the duration and the animation.
Here a sample with the way you need to do it
p {
animation-duration: 25s;
animation-name: slidein;
}
#keyframes slidein {
from {
margin-left: 100%;
width: 300%;
}
75% {
font-size: 300%;
margin-left: 25%;
width: 150%;
}
to {
margin-left: 0%;
width: 100%;
}
}
Here your code modified for this way
.brand1 {
display: block;
font-size: 2em;
width: 10vw;
animation-duration: 1s;
animation: test;
}
#keyframes test {
from {color: tomato; }
to { color: violet; }
}

Making the Ball Change Color

This is a relatively simple question but I can't seem to see what I'm doing wrong here. I just want the ball to change from red, to blue, to yellow at the 0%, %50, and %100 mark. Currently I'm not seeing any changes.
Thanks for your help,
Anna
#ball {
background-color: red;
height: 50px;
width:50px;
border-radius: 50%;
animation-name: ball;
animation-duration: 4s;
#keyframes ball{
0% {
background-color: red;
}
50% {
background-color: blue;
}
100% {
background-color: yellow;
}
}
}
<html>
<body>
<div id="ball">
</div>
</body>
</html>
Move #keyframes out of your ball styles (unless you use a CSS preprocessor like SASS...)
#ball {
background-color: red;
height: 50px;
width: 50px;
border-radius: 50%;
animation-name: ball;
animation-duration: 4s;
}
#keyframes ball {
0% {
background-color: red;
}
50% {
background-color: blue;
}
100% {
background-color: yellow;
}
}
<div id="ball"></div>
Whilst using SCSS instead, you can: jsFiddle example

Transition to red instantly, after 2 seconds transition to blue

This is the only thing I could think of.
#div {
height: 20px;
width: 20px;
background-color: black;
transition: 1s;
}
#div:hover {
background-color: red;
}
#div:hover {
transition-delay: 2s;
background-color: blue;
}
<div id="div">
</div>
It'll instead ignore the first #div:hover
EDIT:
Alright this seemed to work.
#div:hover {
animation: fade 3s forwards;
}
#keyframes fade {
0%, 66% {background-color: red}
100% {background-color: blue}
}
but how do I make it fade out in reverse?
You can achieve with animation.
#div {
height: 20px;
width: 20px;
background-color: black;
}
#div:hover {
background-color: blue;
animation: delayed 4s forwards;
}
#keyframes delayed {
0% {
background-color: red;
}
50% {
background-color: red;
}
51% {
background-color: blue;
}
100% {
background-color: blue;
}
}
<div id="div">
</div>
The second :hover is overwriting the first and the transition-delay will always be 2 seconds.
I guess you'll need to create an animation for that:
#div {
height: 20px;
width: 20px;
background-color: black;
}
#div:hover {
animation: redtoblue 3s;
animation-fill-mode: forwards;
}
#keyframes redtoblue{
0%, 65.9%{
background-color: red;
}
66%, 100%{
background-color: blue;
}
}
<div id="div">
</div>

How to get #keyframes opacity to change opacity every time on FireFox?

My goal is to create a dropdown that fades in every time its parent element is moused over.
And, I want to use the CSS #keyframe property to control the opacity.
See the below example. It works in IE and Chrome as expected (fade-in happens on every mouse over). But, in FireFox, the fade-in happens only on the first mouse over. How can I get the fade-in to happen every time in FireFox?
CodePen showing example:
http://codepen.io/anon/pen/IEBgb
(notice the green "Baz" text fades in)
HTML:
<div class="foo">Foo
<div class="bar">
<div class="baz">
Baz
</div>
</div>
</div>
CSS:
#-webkit-keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
#-moz-keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
#keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.foo {
cursor: pointer;
background: #333;
color: #ededed;
height: 50px;
line-height: 50px;
position: relative;
text-align: center;
width: 200px;
font-size: 30px;
}
.bar {
width: 200px;
position: absolute;
top: 52px;
background: gray;
display: none;
padding: 20px 0;
}
.foo:hover .bar {
display: block;
}
.baz {
font-size: 50px;
color: green;
visibility: visible;
opacity: 1;
-webkit-animation: fadeIn 2s;
-moz-animation: fadeIn 2s;
-o-animation: fadeIn 2s;
animation: fadeIn 2s;
}
It can be done, but you will have to adjust a few things:
Working Example
#-webkit-keyframes fadeIn {
0% {
color: rgba(0, 128, 0, 0); /* transparent text color */
opacity: 0;
}
50% {
color: rgba(0, 128, 0, 0); /* transparent text color */
opacity:1;
}
100% {
color: rgba(0, 128, 0, 1); /* fade in text color */
opacity: 1;
}
}
#keyframes fadeIn {
0% {
color: rgba(0, 128, 0, 0);
opacity: 0;
}
50% {
color: rgba(0, 128, 0, 0);
opacity:1;
}
100% {
color: rgba(0, 128, 0, 1);
opacity: 1;
}
}
.foo {
cursor: pointer;
background: #333;
color: #ededed;
height: 50px;
line-height: 50px;
position: relative;
text-align: center;
width: 200px;
font-size: 30px;
}
.bar {
display:none;
width: 200px;
position: absolute;
top: 50px;
background: gray;
padding: 20px 0;
}
.baz {
font-size: 50px;
}
.foo:hover .bar {
display: block;
-webkit-animation: fadeIn 2s both; /* attach the animation to bar rather than baz */
animation: fadeIn 2s both;
}
Or if you're looking to fade in and fade out you could try something like this:
Working Example 2
Note that the second method uses pointer-events:none/auto so it may have compatibility issues in older browsers. Also seeing the fadeOut animation when the page first loads may be a problem.

Resources