2

I'm using CSS to set my links to red when I hover over them. Clicking on them would open a new tab, but my links would remain hovered(red). Is it possible to unhover on a button click? This looks really bad, especially on mobile.

CSS:
a:hover {
color: red;
}

HTML:
<a href="www.example.com" target="_blank">Open site in new tab, remains red</a>
2
  • I believe its state is focus not hover. You could possibly add a different style for the focused state. Commented Jul 4, 2016 at 15:42
  • I think you must use a:focus{ some css } ! Commented Jul 4, 2016 at 15:44

2 Answers 2

0

The state of the link i.e. a tag is focus after we have just clicked on it and have not clicked anywhere else. So, most probably you want to change the style for :focus state of your anchor tag.

CSS:
a:hover {
color: red;
}
a,
a:focus
{
  color: green; /* or whichever is your default color */
}

/* You can also add same or different style for a:visited state, which applies to anchor tags which have been visited */

HTML:
<a href="www.example.com" target="_blank">Open site in new tab, remains red</a>
Sign up to request clarification or add additional context in comments.

6 Comments

Moreover you can style the a:visited-state if you want to.
@MohitBhardwaj I tried both focus and visited, but it doesn't seem to work that well for mobile. On iOS safari, if I open a link that to a site that has a mobile version(opening linkedin website opens the linkedin app instead) and switch back to Safari, the link would still remain red. Is there a way to unfocus after clicking?
@iamarnold That red color goes away if you click on some other link, or somewhere else on the code?
@MohitBhardwaj Yes it goes away if I click somewhere, but it doesn't look that good on mobile if the user has to tap on somewhere else for the focus to go away.
@iamarnold try with a:focus{ color: green !important; }. I think :focus should solve your problem, but something might be overriding your style. So, adding !important should help.
|
0

One way is on click you can add a class to the button that has the original background color when its not being hovered or clicked..

(This example uses Jquery)

$('.button').click(function() {
	$(this).addClass('button-onclick');
})
body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
}
.button {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 200px;
  height: 200px;
  background-color: red;
  border-radius: 100%;
}
.button:hover {
  background-color: green;
}
.button-onclick:hover {
  background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="button">clickers</div>

fiddle

https://jsfiddle.net/Hastig/hL4dt60k/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.