I was working with some basic CSS animation for button
.The problem is the :focus
pseudo class even works when we press tab on keyboard. So I want that :focus
should only works when I click on the button i.e only when active.
Here is the code:
button {
background: #c33;
width: 150px;
height: 30px;
border: 0;
color: #fff;
}
button:after {
content: 'RENT ME';
display: block;
}
button:active,
button:focus {
background: green;
}
button:active:after,
button:focus:after {
display: block;
animation: shake 1s linear, revert 2s 1s;
position: relative;
}
@keyframes shake {
from {
content: "O";
font-family: FontAwesome;
transform: rotate(0deg);
}
to {
content: "O";
font-family: FontAwesome;
transform: rotate(360deg);
}
}
@keyframes revert {
0% {
content: 'ADDED TO CART';
left: -60px
}
50% {
content: 'ADDED TO CART';
left: 0px;
}
100% {
content: 'ADDED TO CART';
left: 0px;
}
}
<button></button>
<button></button>
In the above code the button changes to green
on press of tab which I want to avoid.Is there a pure CSS solution to it.
JSFIDDLE
The :active selector is used to select and style the active link. A link becomes active when you click on it. Tip: The :active selector can be used on all elements, not only links.
The :focus selector is used to select the element that has focus. Tip: The :focus selector is allowed on elements that accept keyboard events or other user inputs.
The :focus pseudo-class applies when an element is in a state that is ready to be interacted with, i.e. it has the focus of the input device.
The only standard we have is DOM Level 2 HTML, according to which the only elements that have a focus() method are HTMLInputElement , HTMLSelectElement , HTMLTextAreaElement and HTMLAnchorElement . This notably omits HTMLButtonElement and HTMLAreaElement .
use tabIndex="-1"
on button if you don't want the button to be focusable when pressing tab
<button tabIndex="-1"></button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With