Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How make :focus, :active be the same as :hover

Is there any simple way make :focus & :active behave as :hover?

Usually i write:

.class a:hover, .class a:active, .class a:focus {...}

But i would like to write just a single :hover rule and :focus & :active will look the same

.class a:hover {...}
like image 928
Rina_Gorsha Avatar asked Sep 16 '15 23:09

Rina_Gorsha


People also ask

Can hover and focus states be the same?

Focus states cannot be the same as the hover states. However the hover state should be included with the focus styles. Be aware of the thickness of lines used for a focus state.

Is active or Focus same?

:focus is when an element is able to accept input - the cursor in a input box or a link that has been tabbed to. :active is when an element is being activated by a user - the time between when a user presses a mouse button and then releases it.

What is the difference between active and hover?

Hover: It is the state that occurs by putting your cursor over the button. You cannot see this state using the keyboard. Active: Very simply, it is the state of an element that is active. For example, in our example, it is the state of interacting with the button.

How do I turn off hover on focus?

To remove the CSS hover effect from a specific element, you can set the pointer-events property of the element (the hover behavior of which you want to disable) to “none”.


2 Answers

There is currently no better way to do so in CSS2/3.

However, you might want to use cssnext to use the @custom-selectors and :matches() syntax today.

With @custom-selectors:

@custom-selector: --enter :hover, :focus, :active;

a:--enter { ... }

With :matches():

a:matches(:hover, :focus, :active) { ... }
like image 122
Linmic Avatar answered Oct 10 '22 15:10

Linmic


:hover, :active and :focus exist as three separate pseudo-classes for a reason. An element that matches one of these pseudo-classes isn't automatically going to match the other two (even though they're not mutually exclusive, i.e. an element can match two or three of them at the same time).

It's not possible to force an element to match a dynamic pseudo-class either through CSS or programmatically. If you want to apply styles to elements in all three of these states, you will have to specify them.

Selectors 4's :matches() will alleviate the repetitiveness:

.class a:matches(:hover, :active, :focus)

but it is currently not implemented unprefixed anywhere just yet.

like image 20
BoltClock Avatar answered Oct 10 '22 14:10

BoltClock