CSS Link Style ?

Using CSS, links can be styled in many different ways.

We can change the style of CSS link as:

:hover
:active
:visited
:focus
:focus-within
:focus-visible

1. :hover

The :hover CSS pseudo-class triggered when the user hovers over an element with t…


This content originally appeared on DEV Community and was authored by Suprabha

Using CSS, links can be styled in many different ways.

We can change the style of CSS link as:

  1. :hover
  2. :active
  3. :visited
  4. :focus
  5. :focus-within
  6. :focus-visible

1. :hover

The :hover CSS pseudo-class triggered when the user hovers over an element with the cursor.

a:hover {
    color: red
}

If you are using :link, :visited, :active. Remember to add hover after link and visited according to LVHA-order: :link — :visited — :hover — :active.

2. :active

The :active CSS pseudo-class starts when the user presses down the primary mouse button.

a:active {
    color: blue;
}

The :active pseudo-class is commonly used on <a> and <button> elements.

3. :visited

The :visited CSS pseudo-class represents links that the user has already visited.

a:visited {
    color: yellow;
}

4. :focus

The :focus CSS pseudo-class represents an element (such as a form input) that has received focus. It is generally triggered when the user clicks or taps on an element or selects it with the keyboard's Tab key.

input:focus {
    background-color: green;
}

NOTE ? Never remove the focus outline

Checkout the codepen where you play and understand how hover, active, visited and focus are working.

5. :focus-within

The :focus-within CSS pseudo-class represents an element that is itself matched by the :focus pseudo-class or has a descendant that is matched by :focus.

div:focus-within {
    background: cyan;
}

6. :focus-visible

The :focus-visible pseudo-class applies to provide a different focus indicator based on the user’s input modality (mouse vs. keyboard).

.focus-visible:focus-visible {
    background: pink;
    color: red;
}

Why do we need :focus-visible?

:focus also do that same right? ?
But there is one problem. So imagine you wanted to remove outline of some button or links. Then it would be little bit problematic for those user who tries to access through keyboard.

Here we can use :focus-visible, which applies when you actually want a visual indicator to help the user for focus to see.

Checkout the codepen here ?

Reference ?

Buy Me A Coffee


This content originally appeared on DEV Community and was authored by Suprabha


Print Share Comment Cite Upload Translate Updates
APA

Suprabha | Sciencx (2021-06-25T06:10:26+00:00) CSS Link Style ?. Retrieved from https://www.scien.cx/2021/06/25/css-link-style-%f0%9f%8e%a8/

MLA
" » CSS Link Style ?." Suprabha | Sciencx - Friday June 25, 2021, https://www.scien.cx/2021/06/25/css-link-style-%f0%9f%8e%a8/
HARVARD
Suprabha | Sciencx Friday June 25, 2021 » CSS Link Style ?., viewed ,<https://www.scien.cx/2021/06/25/css-link-style-%f0%9f%8e%a8/>
VANCOUVER
Suprabha | Sciencx - » CSS Link Style ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/25/css-link-style-%f0%9f%8e%a8/
CHICAGO
" » CSS Link Style ?." Suprabha | Sciencx - Accessed . https://www.scien.cx/2021/06/25/css-link-style-%f0%9f%8e%a8/
IEEE
" » CSS Link Style ?." Suprabha | Sciencx [Online]. Available: https://www.scien.cx/2021/06/25/css-link-style-%f0%9f%8e%a8/. [Accessed: ]
rf:citation
» CSS Link Style ? | Suprabha | Sciencx | https://www.scien.cx/2021/06/25/css-link-style-%f0%9f%8e%a8/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.