EDIT Demo is here : http://3.cnxical.appspot.com
The text-shadow property changes on hover, and with animation-fill-mode set forwards the state persists.
The animation for the :active state does not work, and nothing happens when the title is clicked.
The expected behaviour is the title should disappear because the text-shadow property was set to (and both of these were tried) none or 0 0 1px transparent. Setting text-shadow for :active was also tried without an animation and it did not work.
How can the correct behaviour be achieved?
The code is :
#title {
position:absolute;
cursor:pointer;
text-align:center;
top:15%;
left:50%;
-webkit-transform:translate(-50%,-50%);
color:transparent;
text-shadow:0 0 10px lime;
font-size:5vmin;
font-weight:bold;
font-family:"Courier New",Courier,monospace;
-webkit-animation: push_title_focus 0.3s;
}
#title:active {
-webkit-user-select:none;
-webkit-animation: vanish_title 0.3s;
}
#title:hover {
-webkit-animation: pull_title_focus 0.3s;
-webkit-animation-fill-mode: forwards;
}
#-webkit-keyframes pull_title_focus {
from { text-shadow: 0 0 10px lime; }
to { text-shadow: 0 0 1px lime; }
}
#-webkit-keyframes push_title_focus {
from { text-shadow: 0 0 1px lime; }
to { text-shadow: 0 0 10px lime; }
}
#-webkit-keyframes vanish_title {
from { text-shadow: 0 0 1px lime; }
to { text-shadow: none !important; }
}
When you press the mouse button down to activate the link, the mouse is still pointing to it, so it is still being hovered.
#title:hover and #title:active are equally specific, and the hover rule is defined last.
Any rules with properties that are specified in both rule-sets, will be overridden by the :hover rule (including -webkit-animation).
Reorder your rulesets so the :hover rule appears before the :active rule.
Related
I tried my method here,
.Subscribe-button:hover {
background-color : rgba(150, 0, 0, 0.917);
transition : background-color 1s;
}
.Subscribe-button:active {
background-color : black;
}
and it works but you have to hold your mouse click for the event to happen and I want to make the transition activate only for the hover.
P.S. I'm new to HTML and CSS and this is just a sample of the code I used.
Since it's the same transition selector and same element it will animate. One way to do that is to add a pseudo-element to the button and change the color of that one for :active instead. Here is how:
.Subscribe-button:hover {
background-color : rgba(150, 0, 0, 0.917);
transition : background-color 1s;
}
.Subscribe-button{
position:relative
}
.Subscribe-button:before{
content: '';
display:block;
position:absolute;
left:0;
top:0;
bottom:0;
right:0;
z-index:0
}
.Subscribe-button:active:before {
background-color : black;
}
<div class="Subscribe-button">Press Me</div>
Use an extra background layer:
.Subscribe-button {
border: none;
padding: 20px;
background-color: #f2f2f2;
transition: background-color 1s;
}
.Subscribe-button:hover {
background-color: rgba(150, 0, 0, 0.917);
}
.Subscribe-button:active {
background-image: linear-gradient(black 0 0);
color: #fff;
}
<button class="Subscribe-button">Press Me</button>
Is there any way to do the opposite of :hover using only CSS? As in: if :hover is on Mouse Enter, is there a CSS equivalent to on Mouse Leave?
Example:
I have a HTML menu using list items. When I hover one of the items, there is a CSS color animation from #999 to black. How can I create the opposite effect when the mouse leaves the item area, with an animation from black to #999?
jsFiddle
(Have in mind that I do not wish to answer only this example, but the entire "opposite of :hover" issue.)
If I understand correctly you could do the same thing by moving your transitions to the link rather than the hover state:
ul li a {
color:#999;
transition: color 0.5s linear; /* vendorless fallback */
-o-transition: color 0.5s linear; /* opera */
-ms-transition: color 0.5s linear; /* IE 10 */
-moz-transition: color 0.5s linear; /* Firefox */
-webkit-transition: color 0.5s linear; /*safari and chrome */
}
ul li a:hover {
color:black;
cursor: pointer;
}
http://jsfiddle.net/spacebeers/sELKu/3/
The definition of hover is:
The :hover selector is used to select elements when you mouse over
them.
By that definition the opposite of hover is any point at which the mouse is not over it. Someone far smarter than me has done this article, setting different transitions on both states - http://css-tricks.com/different-transitions-for-hover-on-hover-off/
#thing {
padding: 10px;
border-radius: 5px;
/* HOVER OFF */
-webkit-transition: padding 2s;
}
#thing:hover {
padding: 20px;
border-radius: 15px;
/* HOVER ON */
-webkit-transition: border-radius 2s;
}
The opposite is using :not
e.g.
selection:not(:hover) { rules }
Just use CSS transitions instead of animations.
A {
color: #999;
transition: color 1s ease-in-out;
}
A:hover {
color: #000;
}
Live demo
Put your duration time in the non-hover selection:
li a {
background-color: #111;
transition:1s;
}
li a:hover {
padding:19px;
}
Just add a transition to the element you are messing with. Be aware that there could be some effects when the page loads. Like if you made a border radius change, you will see it when the dom loads.
.element {
width: 100px;
transition: all ease-in-out 0.5s;
}
.element:hover {
width: 200px;
transition: all ease-in-out 0.5s;
}
No there is no explicit property for mouse leave in CSS.
You could use :hover on all the other elements except the item in question to achieve this effect. But Im not sure how practical that would be.
I think you have to look at a JS / jQuery solution.
Another way of using transition is just specifying the milliseconds like so: transition: 500ms;
Try the following snippet
div{
background: DeepSkyBlue;
width:150px;
height:100px;
transition: 500ms;
}
div:hover{
opacity: 0.5;
cursor:pointer;
}
<div>HOVER ME</div>
You can use CSS3 transition
Some good links:
http://css-tricks.com/different-transitions-for-hover-on-hover-off/
http://www.alistapart.com/articles/understanding-css3-transitions/
Just add a transition and the name of the animation on the class inicial, in your case, ul li a, just add a "transition" property and that is all you need
ul li {
display: inline;
margin-left: 20px;
}
ul li a {
color: #999;
transition: 1s;
-webkit-animation: item-hover-off 1s;
-moz-animation: item-hover-off 1s;
animation: item-hover-off 1s;
}
ul li a:hover {
color: black;
cursor: pointer;
-webkit-animation: item-hover 1s;
-moz-animation: item-hover 1s;
animation: item-hover 1s;
}
#keyframes item-hover {
from {
color: #999;
}
to {
color: black;
}
}
#-moz-keyframes item-hover {
from {
color: #999;
}
to {
color: black;
}
}
#-webkit-keyframes item-hover {
from {
color: #999;
}
to {
color: black;
}
}
#keyframes item-hover-off {
from {
color: black;
}
to {
color: #999;
}
}
#-moz-keyframes item-hover-off {
from {
color: black;
}
to {
color: #999;
}
}
#-webkit-keyframes item-hover-off {
from {
color: black;
}
to {
color: #999;
}
}
<ul>
<li><a>Home</a></li>
<li><a>About</a></li>
<li><a>Contacts</a></li>
</ul>
Although answers here are sufficient, I really think W3Schools example on this issue is very straightforward (it cleared up the confusion (for me) right away).
Use the :hover selector to change the style of a button when you move
the mouse over it.
Tip: Use the transition-duration property to determine the speed of
the "hover" effect:
Example
.button {
-webkit-transition-duration: 0.4s; /* Safari & Chrome */
transition-duration: 0.4s;
}
.button:hover {
background-color: #4CAF50; /* Green */
color: white;
}
In summary, for transitions where you want the "enter" and "exit" animations to be the same, you need to employ transitions on the main selector .button rather than the hover selector .button:hover. For transitions where you want the "enter" and "exit" animations to be different, you will need specify different main selector and hover selector transitions.
You have misunderstood :hover; it says the mouse is over an item, rather than the mouse has just entered the item.
You could add animation to the selector without :hover to achieve the effect you want.
Transitions is a better option: http://jsfiddle.net/Cvx96/
The opposite of :hover appears to be :link.
(edit: not technically an opposite because there are 4 selectors :link, :visited, :hover and :active. Five if you include :focus.)
For example when defining a rule .button:hover{ text-decoration:none } to remove the underline on a button, the underline shows up when you roll off the button in some browsers. I've fixed this with .button:hover, .button:link{ text-decoration:none }
This of course only works for elements that are actually links (have href attribute)
This will add background color to the .icon when hovered and background fades when mouse pointer left the element..
.icon {
transition: background-color 0.5s ease-in-out; /* this is important */
}
.icon:hover {
background-color: rgba(169, 169, 169, 0.9);
}
I'm trying to use the same animation with no selector and with selector :active. The only difference is that I change the animation-direction. The result is that the browser plays the animation once (on page load). When I click the animation is not being played.
I could create two keyframes, with the same content and different names, then it works. But I don't like copy-paste code. So I'm tying to find a way to make this work. Any suggestions?
<style>
button.click {
animation: click 0.5s 0s 1 reverse ease none;
}
button.click:active {
animation: click 0.5s 0s 1 normal ease forwards;
}
#keyframes click {
0% {
margin-left: 0px;
margin-top: 0px;
box-shadow: 5px 10px rgba(0,0,0,0.2);
}
100% {
margin-left: 2.5px;
margin-top: 5px;
box-shadow: 2.5px 5px rgba(0,0,0,0.2);
}
}
</style>
<button class="click">test</button>
You cannot restart the keyframe animation once its executed on page load, the only way is to give 2 keyframes as u said before.
I want to make
background-color: green;
border: 5px solid green;
for a button only for 0.5 seconds effect after a click, to animate a click. How can I do it?
You'll need to use some JavaScript to detect the click and change change the styles. You could possibly make do with a CSS animation, but you'd still need JavaScript to trigger it, so I'm not sure it's worth the effort to make it work in CSS anyways:
function setStyles(el, styles) {
Object.entries(styles).map(
([property, value]) => el.style[property] = value
)
}
const btn = document.querySelector("button")
btn.addEventListener("click", () => {
// capture original styles
const origStyles = {
background: btn.style.background,
border: btn.style.border,
}
// set temp styles
setStyles(btn, {
background: "green",
border: "5px solid green",
})
// reset original styles after 0.5 seconds
window.setTimeout(() => setStyles(btn, origStyles), 500)
})
<button>Click Me</button>
.foo-button {
/* Disable OS styling */
appearance: none;
border: none;
/* Rounded corners */
border-radius: 4px;
/* Text padding */
padding: 6px 12px;
/* Normal color */
background: #2442a0;
/* Text style */
color: white;
text-shadow: 0 0 1px rgba(0, 0, 0, .3);
/* Animation (“transition” timing) */
transition: .5s background;
}
.foo-button:active {
animation-duration: .5s;
animation-name: blink;
animation-fill-mode: forwards;
animation-iteration-count: 1;
background: green;
}
#keyframes blink {
0% {
background: green;
}
100% {
background: #2442a0;
}
}
<button class="foo-button">Button Text</button>
I am trying to make two buttons animate from small to large on hover using CSS. I have the following which does make the button change but without no animation. (button.png and buttonHover.png are the same pixel width and height - but the images are of a small button with a transparent surround and a large button).
It may well be that this is the wrong way to do it - this is the first time I have trued this.
a.button {
background: url(button.png) no-repeat 0 0;
width: 150px;
height: 62px;
display: block;
line-height: 62px;
text-align: center;
font-size:9px;
color: white;
}
a.button:hover{
background: url(buttonPressed.png) no-repeat 0 0;
font-size:14px;
-webkit-animation: myButton 1s; /* Chrome, Safari, Opera */
animation: myButton 1s;
}
/* Chrome, Safari, Opera */
#-webkit-keyframes myButton {
background: url(buttonHover.png) no-repeat 0 0;
font-size:14px;
}
/* Standard syntax */
#keyframes myButton {
background: url(buttonPressed.png) no-repeat 0 0;
font-size:14px;
}
No need for keyframes; use transition.
As Zach mentioned in the comments, background images can't be animated between. You should recreate the backgrounds in CSS.
From the MDN:
The CSS transition property is a shorthand property for transition-property, transition-duration, transition-timing-function, and transition-delay. It allows to define the transition between two states of an element. Different states may be defined using pseudo-classes like :hover or :active or dynamically set using JavaScript.
Example
In this example, the "all" indicates that every difference between the normal state and :hover that can be animated should transition over 0.5 seconds. Here is a complete list of animated properties.
Use the appropriate browser prefixes before the non-prefixed transition as needed. Depending on your needs, browser prefixes could be unnecessary. Have a look over here on caniuse.com for an overview of browser support.
a.button {
background: #000;
width: 150px;
height: 62px;
display: block;
line-height: 62px;
text-align: center;
font-size: 9px;
color: #FFF;
transition: all 0.5s;
}
a.button:hover {
background: #F00;
font-size: 14px;
}
<a class="button">Button</a>
Im leaving you a working example on how to implement it. Hope it helps.
button {
width : 100px;
height: 20px;
-webkit-transition: 1s ease-in-out;
-moz-transition: 1s ease-in-out;
-o-transition: 1s ease-in-out;
transition: 1s ease-in-out;
}
button:hover {
width : 150px;
height: 30px;
}
<button>
JSfiddle