Customize the behavior of the focus-visible feature on input elements

In my design, I want to remove the outline ring on an input element when it's focused via mouse interaction but still keep it visible when focused using the keyboard.

This similar question was raised about a week ago here, unfortunately with no solution provided.

However, my scenario has some nuances.

Input HTML:

<form class="custom-form">
  <label class="custom-label" for="my-input">
    <span>My Input</span>
    <input id="my-input" class="custom-input" name="my-input" type="number" min="0" max="100" value="00.00" step="1.00" required 
      aria-valuemin="0" aria-valuemax="100" aria-required="true" aria-invalid="false">
  </label>
</form>

Input TailwindCSS:

.custom-form {
  @apply flex flex-row;
}
.custom-form .custom-label {
  @apply flex flex-col space-y-1;
}
.custom-form .custom-label > span {
  @apply pl-1;
}
.custom-form .custom-input {
  @apply max-w-[100px];
  @apply mr-2 px-1 py-[3px];
  @apply text-base;
  @apply border rounded border-gray-200 dark:border-neutral-600 focus:border-neutral-600 dark:focus:border-neutral-100;
  @apply outline-none;
  /*@apply focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2;
  @apply focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-100;*/
  @apply bg-neutral-100 dark:bg-neutral-700;
  @apply transition-all duration-75;
}
.custom-form input[type=number].custom-input {
  -webkit-appearance: textfield;
  -moz-appearance: textfield;
  appearance: textfield;
}

I've already set up a border on the focus state following W3's Relationship to Non-text Contrast Success Criterion.

This same focus style is implemented across my other UI elements, yet they maintain the outline ring when navigated with the keyboard.

For example:

Button HTML:

<button class="custom-button">
  Button
</button>

Button TailwindCSS:

.custom-button {
  @apply px-3 py-1 rounded;
  @apply bg-neutral-100 hover:bg-neutral-200 active:bg-neutral-300;
  @apply dark:bg-neutral-900 dark:hover:bg-neutral-700 dark:active:bg-neutral-600;
  @apply border border-gray-200 dark:border-neutral-600;
  @apply focus:border-neutral-600 dark:focus:border-neutral-100;
  @apply focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2;
  @apply focus-visible:outline-outline-light dark:focus-visible:outline-outline-dark;
  @apply transition-all duration-75;
}

Essentially, I aim to achieve the same styling behavior of the button on the input element.

Practical and viable alternatives:

  1. Omit outlining rings entirely and solely rely on border styling through focus state.

Though feasible, I prefer keeping the outline for quick keyboard navigation.

  1. Add outlines to other UI elements as needed.

Outlining everything necessitates additional spacing to prevent overlap and detracts from the minimalist aesthetic I'm aiming for.

  1. Set the default outline to none in CSS and utilize a JavaScript Tab key listener on each input element. When an element gains focus this way, "enable" the outline.

I could consider this last option as a final resort, but I'm unsure of the performance implications, given there are around a dozen input fields per form at most.

Can the behavior of :focus-visible be modified at all? It seems browsers handle this differently based on what I've read.

Answer №1

If you want to get rid of the blue outline when an element is in focus, simply include the following CSS:

input:focus {
  outline: none;
}

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

Choose the "toolbar-title" located within the shadow root of ion-title using CSS

Within Ionic, the ion-title component contains its content wrapped in an additional div inside the shadow-dom. This particular div is designated with the class .toolbar-title. How can I target this div using a SCSS selector to modify its overflow behavior? ...

The problem of creating a custom pop-up with jQuery

I'm running into an issue while trying to develop a custom CSS and jQuery popup. Despite my efforts, the popup isn't functioning as intended. Take a look at my code below and point out where I may have gone wrong. What I aim to achieve is a popup ...

The nth-child selector is not functioning correctly with material-ui components

Trying to figure out how to give two paragraphs different background colors using nth-child. Here's the JSX: <div className={classes.paragraphs}> <p>Test 1</p> <p>Test 2</p> </div> And the CSS: const useSty ...

What could be causing the issue of dynamic meta open graph tags not functioning properly?

For my current project, I am utilizing Strapi as the CMS and Next.js. My task is to generate meta open graph tags dynamically. However, after deployment, when I attempt to share my website link on social media platforms such as Facebook or WhatsApp, the ti ...

What is the best way to extract the content within a nested <p> element from an external HTML document using the HTML Agility Pack?

I'm currently working on retrieving text from an external website that is nested within a paragraph tag. The enclosing div element has been assigned a class value. Take a look at the HTML snippet: <div class="discription"><p>this is the ...

Dynamic Data Drive Multi-Series Line Graph

I am currently working on creating a D3 line chart. I have taken the code from the block builder and adjusted it with my own data. While the code is functional, I'm facing an issue where the labels are not showing up when I hover over the line. My ma ...

Concealing overflow for text content through CSS styling

I am currently working with an element that contains both an image and text. View the Fiddle here Note: To see the full grid, resize the preview accordingly. The CSS I have written is as follows: .gridster .gs-w .item{ position: relative; wi ...

Implementing Bootstrap 3.1 to prioritize main content at the top on mobile devices

Below is the grid structure that I am working with <div class="row"> <div id="A" class="col-md-3"> <div class="alert alert-info">A</div> </div> <div id="B" class="col-md-6"> <div class=" ...

Struggling to use the innerHTML method to update a div within another div element?

<!DOCTYPE html> <html> <head> <title>Business Information Card</title> <script type="text/javascript"> window.onload = init; function init(){ var button = document.getElementById("populate ...

Adjusting the dimensions of the "overflow avatar" to be consistent with the other avatars displayed in AvatarGroup

When setting the max prop of the AvatarGroup, the additional avatar does not match the height of the other avatars. <AvatarGroup max={3}> <Avatar alt="Remy Sharp" src="/static/images/avatar/1.jpg" sx={{ width: 24, height: 24 ...

Validating radio buttons in Ionic framework

I am currently working on implementing a form in my application using Ionic and AngularJS. I have added validation to all the fields, but I am facing an issue with the radio button validation. The error message is being displayed correctly when no radio bu ...

Guide to showcasing associated information in HTML using Angular 7

I am a beginner with Angular 7 and I am attempting to showcase a product's color on the HTML side using Angular 7 but have not been successful. Below are my tables; Product Id Name Color Id Name ProductColorRelation Id ProductId ColorId In ...

Limit the y-axis on CanvasJS visualization

Is it possible to adjust the minimum and maximum values on the y-axis of the graph below: new CanvasJS.Chart(this.chartDivId, { zoomEnabled: true, //Interactive viewing: false for better performance animatio ...

Vertical alignment with flexbox not functioning properly in Internet Explorer 11

I am attempting to use flex to center an icon within two divs. Below is the code snippet: .div-1 { width: 50px; height: 50px; display: flex; border-radius: 50%; background-color: #228B22; } .div-2 { margin: auto; } i { color: #ffffff; } &l ...

Unable to set the value of an HTML date field using Selenium on mobile devices

I am facing a challenge in setting the value of a date field on an HTML page using Selenium. The date field on the page is structured like this: <input aria-invalid="false" id="datepicker-date-input" type="date" min="2 ...

What is causing all webkit browsers to overlook the z-index in my code?

I have removed all unnecessary elements from my webpage to focus on reproducing a specific issue. HTML: <html lang="en-us"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initi ...

Guide to adding a new font to your Ember project

I am currently in the process of developing a website utilizing the MEEN Stack framework (Mongo, Ember, Express, Node). One challenge I am encountering is importing the Lato font family into my project to serve as the main font for the entire site. Despite ...

The phenomenon of an invisible Absolute or relative position leading to grid components overlapping in Next.js

I've been struggling with this issue for over 48 hours now. I've attempted to troubleshoot by commenting out everything except the affected components and even swapping entire components around, but the problem persists. Oddly enough, rearranging ...

retrieving the selected date from the calendar widget

I'm utilizing jQuery's datepicker to collect date inputs from users. The form is structured as below: <form id="myform" action="/graphs/get_builds" method="post"> Start: <input type="text" id="start" /> End: <input type="t ...

Failed to load CSS and JS files

https://i.sstatic.net/tzp6n.png When attempting to launch my project on the server, I am facing issues with the CSS and JS files not loading properly. Below is the code snippet I have been using to include CSS/JS files: <script src="js/jquery.min.js" ...