By default, buttons do not display outline styles when clicked

What is the reason default buttons do not display outline styles when clicked, while custom buttons do show an outline style on click?

Is it possible to remove the outline styles from a custom button on click while maintaining the outline styles when users navigate to it using the keyboard (similar to default buttons)?

.btn {
  display: inline-block;
  padding: 12px;
  background: cyan;
  border: none;
}
<div>
    <button type="button">Why are there no outline styles on click?</button>
</div>
<div style="margin-top: 24px">
    <button class="btn">Why do outline styles appear on click?</button>
</div>

UPDATE: I am unable to use outline: none for accessibility reasons. Reference:

Answer №1

While considering accessibility, have you thought about keeping the outline rather than removing it? You could enhance accessibility by replacing the outline with a different style such as background-color, color, or border.

Answer №2

Extracted and altered from a previous inquiry found here:

Reason behind pseudo element :focus on button when using TAB but not click?

This can be accomplished with the use of JavaScript

var submit = document.getElementById("submit");

var tabbing = false;

window.onkeyup = function (e) {
    if (e.keyCode == 9) {
        tabbing = true;
    }
}

submit.onfocus = function (e) {
    if (tabbing) {
        this.classList.add("tabbed");
    }
};

submit.onblur = function () {
    this.classList.remove("tabbed");
    tabbing = false;
};
#submit.tabbed:focus {
    outline: 2px solid green;
}

#submit{
  display: inline-block;
  padding: 12px;
  background: cyan;
  border: none;
  }

#submit:focus{
  outline: none;
  }
<label>Name:</label>
<input type="text" />
<br>
<label>Pass:</label>
<input type="password" />
<br>
<label>Save Password:</label>
<input type="checkbox" />
<br>
<br>
<button id="submit">Submit</button>

An outline will only be displayed when the user tabs, not clicks on the button.

Answer №3

To distinguish between focus triggered by a click and that triggered by a keypress, JavaScript will be necessary.

For more information, you can refer to:

  • Determine what triggered focus event?
  • Differentiate between focus event triggered by keyboard/mouse
  • Detect focus initiated by tab key?

There are various suggestions in those links - if you have the ability to include JavaScript on this page, you'll likely find a solution suitable for your needs.

Answer №4

To remove the outline attribute of a button when focused, you can use the following CSS rule: :focus {outline: none;}

.btn {
  display: inline-block;
  padding: 12px;
  background: cyan;
  border: none;
}
.btn:focus {
  outline: none;
}
<div>
    <button type="button">Why does the outline disappear on click?</button>
</div>
<div style="margin-top: 24px">
    <button class="btn">Why does the outline appear on click?</button>
</div>

Answer №5

Default settings for certain browsers include the following:

:focus {
  outline: -webkit-focus-ring-color auto 5px;
}

However, these defaults may need to be overridden as follows:

.btn:focus {
  outline: none;
}

Answer №6

To eliminate the outline, simply add this snippet of code:

.button{
   outline: none;
}

It is important to remember that when you use .button:active, "active" refers to an action being taken, such as clicking the button.

The key is to have the outline present by default and then remove it during actions or focus events.

Answer №7

Uncertain about your question regarding accessibility, but if you want to eliminate the outline, simply add this code:

button:focus {outline:0;}

Check out the demo here

No apparent method to distinguish between focus on a button by keyboard input or mouse click, as both actions trigger the button's focus event.

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

Adjust and resize video backgrounds within parent elements on iframes

Consider the code snippet below: <div class="viewport"> <iframe src="youtube.com/blabla"></iframe> </div> Accompanied by this CSS: .viewport { height: 100vh; width: 100%; } iframe { position: absolute; } If we t ...

Opting for CSS3 transitions over jQuery animate

Currently, I am working on animating a block of HTML that involves fading in (opacity) and slightly moving from the left (margin-left)... $('.latest-stats').delay(1000).animate({ opacity: 1, marginLeft: '55px' }, 1000, function () { ...

Is there a way to set the canvas size to match the exact window size in pixels without being affected by the browser's zoom factor?

I'm currently developing a website called sphere.mars2540.com. Is there a way in JavaScript or CSS to maintain a fixed canvas size regardless of the zoom level of the page, ensuring it always aligns with the actual pixels on the screen (even with 4k ...

How to update data in FullCalendar using Bootstrap modal

While using a FullCalendar within a modal, I encountered an issue where the information for a user would not refresh when opening another modal. Despite trying various solutions from Stackoverflow, none seemed to work. The only time it did function properl ...

Refresh an iframe using Javascript

Hey there, I could really use some assistance with this code. It works perfectly for reloading an iframe using an id, but I'm struggling to figure out how to make it work with a class so that I can reload multiple iframes. Alternatively, I'd like ...

How can I implement horizontal scrolling on a material-ui table containing numerous columns?

As I work with the React Material-UI framework, I am using this table example as a guide. However, I am facing an issue where my table becomes overcrowded when I have numerous columns, causing the content to be cut off. I recently came across the material ...

Emphasize an element when hovering over it

I am looking to create a custom hover effect for highlighting elements in a Chrome extension I'm developing. The goal is to achieve a similar behavior to using the magnifying glass tool in Chrome Dev Tools, where only the hovered element is highlighte ...

Is it possible to include line breaks within a CSS value?

(I am posting this here because I couldn't find a solution and eventually figured it out through trial and error. Hopefully, this information can help others with the same question.) Note: This is not the same as the discussion on Single-line vs mult ...

Delete the element that was generated using jQuery

Is there a way to add and remove an overlay element using JavaScript? I am struggling to get JavaScript to remove an element that was created with JavaScript. Any suggestions? Check out this JS Fiddle example Here is the HTML code: <div id="backgroun ...

Switching the parameter to navigate to the appropriate page

Hi there, I'm looking to implement a quick change using the onchange parameter. Can someone assist me in using the onchange parameter to navigate to a new page? So, if someone selects for instance Basic info, they will be directed to a new page custo ...

Having trouble adjusting the width of the text input in the inline search box form with an input group dropdown

Looking to create a search box similar to this design: https://i.sstatic.net/H93WQ.png What is the most effective way to implement this using Bootstrap? I tried using an inline form and input group dropdown list, but I encountered issues with the text i ...

I'm having difficulty grasping the concept of creating a responsive design

I've been working on making this video embed responsive, and I've got it working for tablets. Now, my next challenge is optimizing it for phones and handling browser window resizing dynamically. Here's the desired layout: https://i.sstatic. ...

How can I adjust the gravity or positioning of a tipsy tooltip in jQuery?

Is there a way to adjust the position or gravity of Tipsy? The plugin offers various options for gravity that can be set through the script: nw | n | ne | w | e | sw | s | se Currently, I have set it to s position, as shown in this demo: http://jsfiddle. ...

Steps for accessing the value from an input tag within the same form:

Exploring how to utilize the value "typed_from_user" in another tag within the same form, using only PHP & HTML without relying on JavaScript. Is this scenario feasible? For instance, let's say I wish to reuse the term 'pizza' <form> ...

Can 3D objects be easily moved by dragging and dropping in sap ui5?

Can a 3D object be generated in SAP UI5 allowing for interactive movement of objects? For example, creating a 3D model of a house where individual pieces like chairs can be manipulated. Is there a method to accomplish this task effectively? ...

Tips for resolving the issue of "Unable to assign property '_DT_CellIndex' to undefined in Jquery Datatable"

<?php if(mysqli_num_rows($result)>0) {?> <table class="table table-striped" id="example" align="center"> <tr> <thead> <th style=&quo ...

The functionality of Css translate is not functioning as expected

I'm experiencing an issue with the translation of a tooltip. It's supposed to appear and then translate upwards, but in my code it only appears... I can't figure out what's wrong in my code... Any ideas please? View demo: http://jsfidd ...

Ways to dynamically set a background image URL using solely HTML and CSS

I am looking for a way to dynamically assign the url of the background-image through CSS based on attributes defined in HTML tags. I have tried using both attr() and var(), but neither seem to work. I prefer not to use JavaScript as the URL may change late ...

Error in Compiling HTML Elements Collection<<Element>

Currently, I am developing an eCommerce application that features a popup window for users when they click on "Add to Cart." This popup allows users to select product variations and quantities before adding the item to their cart. The popup consists of a s ...

What is preventing the audio from playing in Safari?

Recently, I developed a small JavaScript program that is meant to play the Happy Birthday tune. This program utilizes setInterval to gradually increase a variable, and depending on its value, plays or pauses certain musical notes. However, I encountered ...