Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS pseudo class :focus after button click

Tags:

html

css

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

like image 227
shubham agrawal Avatar asked Apr 21 '17 10:04

shubham agrawal


People also ask

How do you keep active CSS style after clicking a button?

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.

How do you focus a button in CSS?

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.

Which pseudo-class activates once the user is focused on the element by using keyboard controls?

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.

Which elements can have focus?

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 .


1 Answers

use tabIndex="-1" on button if you don't want the button to be focusable when pressing tab

<button tabIndex="-1"></button>
like image 135
FIx Avatar answered Oct 01 '22 20:10

FIx