The CSS:
a:focus { opacity: .75 }
a:active { transform: translateY(4px) }
The intent:
Keyboard users tab to the link, using the :focus
style as a visual cue
They hit enter
to activate the link; the :active
style gives visual keypress feedback
The problem:
The :active
style triggers properly for a mouse click, but not a keypress. Can I fix this with just CSS?
Pure CSS Active Buttons are used to create an active button that works when the user clicks on the button. The button can be created using <a> and <button> tags. Pure CSS Active Buttons Class: pure-button-active: This class is used to create an active 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.
A CSS pseudo-element is a keyword added to a selector that lets you style a specific part of the selected element(s). For example, ::first-line can be used to change the font of the first line of a paragraph. Note: In contrast to pseudo-elements, pseudo-classes can be used to style an element based on its state.
Pseudo-classes enable you to target an element when it's in a particular state, as if you had added a class for that state to the DOM. Pseudo-elements act as if you had added a whole new element to the DOM, and enable you to style that.
Good question!
Yeah, you can't do this anymore.
A long time ago MSIE treated :active
like :focus
so there was sort of a way to accomplish this with hyperlinks (this was before gigabit internet speeds and when browsers didn't do a good job of showing load-in-progress except for a dumb waving flag or something).
Run the below Snippet to see the behavior in action:
input type='button'
can be activated with ENTER
or SPACE
SPACE
on a button will show the :active
style (except for FireFox; this looks like FF bug since it works OK for mousedown)ENTER
on a button will repeatedly trigger onclick
every time your keyboard sends the character.SPACE
on a button will trigger onclick
only if the SPACE
key is released while still focused on the button. (For example, you can simulate mouse click this way: tab to a button, hold down space, then hit tab again and release it to cancel the click.)ENTER
only.:active
style, using ENTER
(or touch) will not.document.getElementById('t1').focus(); // set focus for easier demo
:active
{
color: Red;
}
<input type='text' id='t1' value='Set focus and hit tab'/>
<a href='javascript:;' onclick='console.log("Hyperlink")'>Hyperlink</a>
<input type='button' value='Button' onclick='console.log("Button")'/>
So you are stuck using JavaScript or a plugin like Flash / Silverlight for this.
:focus will be the state entered when the keyboard user tabs to that element. Note that tabbing will cycle through the link tags on your page, so any css styling must be applied with the selector a:focus.
:active will be the state entered when the user hits the enter button on their keyboard.
Here's a little snippet of an example of how to apply css for both keyboard and mouse users:
a:hover .class,
a:focus .class {
background-color: rgba(243,113,89, 0.95);
}
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