Johnny
Johnny

Reputation: 9964

Why can't I focus on an anchor like this?

I had some CSS like this:

a {
    display: block
}

a:focus {
    background: #000
}

And an anchor like this:

<a href="">Hi!</a>

why can't I focus on it by clicking? I know it redirects but one would assume for a split second the background would go black. Whats up?

Upvotes: 1

Views: 320

Answers (2)

Ry-
Ry-

Reputation: 224904

Not all browsers give elements focus when clicked. I believe Internet Explorer does, but Google Chrome certainly doesn't. If you're looking to apply a style when the mouse is down on the element, consider :active instead:

a:active {
    background: #000;
}

You can compare the two live here. On this page, Tab once and the first one should get the focus and the border.

Upvotes: 3

Evert
Evert

Reputation: 8541

a:active would give you the desired result (I use this for button press background images)

Upvotes: 1

Related Questions