Including a tab index within an anchor element is preventing the active class from being deactivated

I've successfully created a button-like effect on an <a> element by adding a tabindex, allowing users to navigate to it using the tab key.

Here's the scenario: When a user clicks on the button and then moves the mouse away, the active style remains until they click outside of it. However, if I remove the tabindex and repeat the process, the effect disappears as expected.

How can I maintain the same behavior even with the tabindex in place?

<a class="button" tabindex="0">CALCULATE</a>
.button {
    background-color: rgb(13, 93, 166);
    display: inline-block;
    padding: 20px;
    border: 1px solid blue;
}

.button:active, .button:focus, .button:hover {
    background-color: rgb(96, 178, 212);
}

Check out this JSFiddle link for reference

Answer №1

If you want to proceed without the focus, simply remove it.

To eliminate the focus, make sure to overwrite it if it is part of the base class.

.button {
  background-color: rgb(13, 93, 166);
  display: inline-block;
  padding: 20px;
  border: 1px solid blue;
}

.button:active,
.button:hover {
  background-color: rgb(96, 178, 212);
}


/* Override the focus */

.button:focus {
  background-color: none;
}
<a class="button" tabindex="0">CALCULATE</a>

Answer №2

If you're looking for a solution that meets your specific needs, it's likely that just using CSS won't be sufficient. Incorporating some jQuery or JavaScript may be more effective. For reference and guidance, feel free to check out: http://jsfiddle.net/S7kJ2/

$('.button').click(function(){
    if($(this).hasClass('active')){
        $(this).removeClass('active')
    } else {
        $(this).addClass('active')
    }
});
.button { 
    float:left;
    color: #333;
    width: auto;
    text-align: center;
    padding: 0 10px;
    background: #f0f0f0;
    line-height: 1.5em;
    height: auto;
}
.button.active {
    background: #ea0000;
    color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="button ">My button</div>

Alternatively, you can also visit: Keep button in active state until clicked upon again

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing Bootstrap 4 to strategically adjust element positioning based on device size and enhance responsiveness

I utilize bootstrap4 to develop a responsive layout that prioritizes mobile devices, featuring elements with different positioning. I have set up multiple rows and columns which I adjust based on the screen size categories. My curiosity lies in whether th ...

Fixing the error: Severity notice, there is an undefined variable called 'table'

My controller logic is as follows: public function index() { if ($this->session->userdata ( 'logged_in' )) { $condition = array( 'qes_status'=>'Y' ); $data = array(); $ ...

JavaScript: How to Build a Digital Grocery List with Browser Storage

Struggling with a tough exercise question, I could use some help deciphering it. https://i.stack.imgur.com/V5he2.png Here is how I've started the code: <!DOCTYPE html> <html> <head> <title></title> <script> fun ...

Is it permissible to use DEFER or ASYNC when including a stylesheet?

I am curious if script files are able to utilize the DEFER and ASYNC keywords on a resource include. Are these keywords also compatible with stylesheet (CSS) includes? The syntax I imagine would be: <link rel="stylesheet" type="text/css" href="./path/ ...

Always keep your phone in landscape orientation for optimal website viewing

Currently, I am facing an issue with my website where it functions perfectly on mobile devices in landscape orientation but elements get distorted when viewed in portrait mode. Is there a method to ensure that the website is always displayed in landscape ...

Improving the functionality of the JavaScript key press function

I have a small javascript snippet on my website that allows me to navigate the site using the arrow keys on my keyboard. document.onkeydown = function(evt) { evt = evt || window.event; switch (evt.keyCode) { case 37: ...

hover-triggered keyframe animation

Recently, I've been experimenting with CSS keyframe animation using the code below: .pulse-animation { margin: 50px; display: block; width: 52px; height: 52px; border-radius: 50%; background: #ff8717; cursor: pointer; animation: pul ...

Inject pure HTML code into a react div

I have extracted raw HTML content stored in the database and I am attempting to display it within a div element. const style = { border: '2px solid #eee', height: '270px', width: '100%', overflow: 'auto&ap ...

Conceal the slider thumb within the material-ui framework

I've been attempting to conceal the slide thumb, initially without using a library. However, I started considering utilizing material-ui as it might make the process easier. Hence, I'm seeking assistance here. Below is my code snippet: import * ...

Incorporating Local Storage into a To-Do List Application

I followed a tutorial from w3schools to create a custom task list, tweaking the code to fit my requirements. My goal is to ensure that when I click the save button in the toolbar at the top and then refresh the page, the added tasks are preserved. I&apos ...

CSS - Implementing responsive design to ensure optimal viewing experience on all devices

Card Matching Game Project Codepen Check out my CSS here, and find the rest of the code on Codepen since it's quite lengthy. const cards = document.querySelectorAll('.memory-card'); let hasFlippedCard = false; let lockBoard = false; let ...

Methods for verifying if a file is included in another file, while disregarding any whitespace adjustments

Let's say I have multiple CSS files (a.css, b.css, ..., e.css) and after concatenating and compressing them, I get a new file called compressed.css. Now, I need to verify if each of the original CSS files is included in the compressed.css file. Are th ...

Tips on adjusting the width so it aligns with the text rather than the entire width using Bootstrap

My title is currently taking up the entire row, but I want it to only be as wide as the text itself and not extend to the width of the parent row. Any suggestions on how to achieve this? <head> <meta charset="UTF-8" /> <met ...

Dispense CSS using a React element

My React component index.js contains styling in style.css. I am looking to share this component on npm, but I am unsure of how to simplify the process for other developers to use the styling. After exploring different options, I have come across three ap ...

Include content within the navigation bar of Bootstrap 3.3

I am trying to insert text at the end of the header in a jumbotron template: <div id="navbar" class="navbar-collapse collapse"> <form class="navbar-form navbar-right"> <div class="form-group"> <input type="text ...

Issue with navigation links on HTML website not functioning as expected

I would like the dropdown menu items to be clickable. For example: Menu item: Services Sub items: - branding } These already have working links - marketing } However, when I try to replace '#' with a link for Services, it d ...

The navbar collapse feature in Bootstrap 4 is not functioning properly when using JQuery, popper, and bootstrap together

Hello there! I'm currently learning web development and attempting to create a button that collapses the navbar. I've gone through answers to similar questions but haven't found the solution that works for me. I've ensured that I have ...

Updating an image source using JavaScript without relying on a default image source specified in the HTML code

Struggling to change the image source path using JavaScript? Want to change the page color randomly after each load? I created SVGs in different color types - blue, red, yellow... but can't get it to work. The path seems correct when tested normally i ...

Is there a way to make sure that my submit button aligns perfectly with the text beneath it

I am new to coding and I need help aligning my submit button with the text "You can contact Armando through his freelance portfolio on Upwork by clicking...". Can anyone assist me with this? .topnav { background-color: #333; overflow: hidden; } .to ...

Modifying the border color of a row may result in unexpected shifting of cells

To enhance the appearance of certain rows in my table, I have customized their styles for highlighting purposes: table.setRowFactory(tv -> new TableRow<TableBean>() { @Override public void updateItem(TableBean item, boolean empty ...