Display the focus outline solely when tabbing, not when clicking with the mouse

I've been developing a ReactJS web application and I'm customizing the outline style for specific elements. The problem is that the outline is being displayed when a user clicks on it with a mouse. Is there a way to make the outline visible only when tabbing through the elements?

const CustomElement = styled.div`
 &:focus {
  outline: 1px solid red;
 }
`;

Answer №1

If you're looking for a great solution to this question, check out the following response: Guide on Enabling :focus only when using keyboard or pressing tab

It's a comprehensive answer worth checking out. Enjoy coding!

Answer №2

Utilize the focus-active pseudo-class. Take a look at the MDN documentation for more information.

const Test = styled.div`
 &:focus-visible {        /* <--- THIS IS WHERE IT CHANGES */
  outline: 1px solid red;
 }
`;

As mentioned by Sitian Liu, refer to this answer for further detail.

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

Encountering an issue while attempting to execute a test on Google Chrome using Java and Selenium

Could anyone assist me? I am facing difficulties while running an automated test. The errors shown in the attached images are puzzling me and I am unable to come up with a solution. If anyone has experienced this issue before, please lend me your experti ...

A guide to displaying a countdown timer in an Angular application during the app's loading process

Displaying a loader that shows the time in seconds while loading an app is my goal. Here is the code snippet: HTML <body> <div class="app-loader"> <div class="loader-spinner"> <div class="loading-text"></div> ...

Tips for reducing the size of input-group-addon in Bootstrap

https://i.sstatic.net/V7ywZ.png <div class="time input-group"> <span class="input-group-addon" id="basic-addon4">Start Time </span> <timepicker [(ngModel)]="mytime" required></timepicker></di ...

The mobile version of the navbar is causing me some trouble

I'm facing an issue with my navigation bar. It appears perfectly on desktop and even looks fine when I simulate it on smartphones and tablets using developer tools. But when I actually open the webpage on a mobile device, the navbar icon becomes extre ...

Is it possible to retrieve a list of controllers that are connected to an object in Three.js?

Is there a method in Three.js to retrieve the list of controllers linked to an object? Or perhaps just one controller linked to that object. I have connected a controller to an object, but I do not want the transformController to always be visible on it. E ...

List of bullet points styled with hyperlinks in ASP.NET:

<asp:BulletedList ID="BulletedList1" runat="server" DisplayMode="HyperLink" style="list-style :none" > Is there a way to dynamically add list items to the bulleted list above and inject individual styling to the href attribute of each item? ...

Express JS does not return form data, resulting in a null value

I am encountering an issue while trying to send data from React to Express. I have an array of images that need to be sent, but due to the inability to send a single image as a test, I will need to clarify the scenario. To simplify the testing process, I h ...

Having some trouble creating a ping command in discord.js that displays latency; encountered this error message

I encountered an issue while trying to create a ping command that displays latency. I am unsure why this error is showing up, here is the specific error message - TypeError: Cannot read properties of undefined (reading 'ping') at Object.execu ...

Utilize alternating colors from the Material UI palette to enhance the appearance of

Can someone help me understand how to utilize the different color variants in Material UI? For instance, the theme primary color has various options like 100, 200, 300, 400, 500, and so on. How can I access these colors from within my component? I attempt ...

React / Express - Issue: ENOENT: The file or directory specified does not exist: '/app/build/index.html'

Kindly review the following changes: I am desperately seeking help here. After countless hours of sifting through documentation and attempting various methods suggested on this platform, I am still faced with an error during Heroku's build process aft ...

Unlocking the secrets of the <g> element within an SVG with the power of ReactJS

Currently, I am facing a challenge while working on my project that involves utilizing this svg file: . I have managed to render the svg by using an Img tag. However, I am struggling to find a way to access the individual groups within the svg in order ...

Looking to target an element using a cssSelector. What is the best way to achieve this?

Below are the CSS Selector codes I am using: driver.findElement(By.cssSelector("button[class='btn-link'][data-sugg-technik='append_numbers']")).click(); driver.findElement(By.cssSelector("button[class='btn-link'][data-sugg- ...

Unable to display image in jqGrid binary format

In our system, we use a standard function to retrieve images stored as binaries in the database, and this function works seamlessly throughout the entire system. However, when implementing jqGrid, I encountered difficulties using the existing structure as ...

Node.js does not receive any content in the request from ReactJS

When I send a ReactJS request using the post method, the req. body object in NodeJS shows no values. However, when I tested the API using Postman, it worked as expected. I am using Axios to connect ReactJS to the API and Express in Node.js. function Create ...

When the image transitions, the second dot does not activate

Recently, I developed a small automatic image slider that transitions to a new image every 4 seconds by utilizing the following code: setInterval(displayImage, 4000);. The image transitions are functioning correctly; however, there seems to be an issue w ...

Angular 2 404 Error persists despite successful retrieval of data from Oracle database using Backend Nodejs URL entered directly into the browser

Recently, I've been working on displaying data in my Angular frontend that is fetched from an Oracle DB connected to my Node backend. When I access the physical API link, the data appears and is displayed in the backend console.log. I'm wonderin ...

What is the best method for iterating through an array and generating a glossary list organized by categories?

I have an array filled with definitions. How can I use Vue.js to iterate through this array and create a glossary list organized by letters? Desired Output: A Aterm: A definition of aterm B Bterm: A definition of bterm C Cterm: A definition of cterm ...

Using jQuery Ajax and PHP for Secure User Authentication

Hello everyone, I could really use some assistance right now. The issue I'm facing is that the error div is not showing the content I expect in the success function of my AJAX request. I've tried alerting the response, which returns true if the ...

Capture every URL path in NuxtJS page navigation

One interesting feature of NuxtJS is the ability to assign routes in a specific way. For instance, by naming a file pages/_book.vue, we can access the route localhost:3000/my-book with the parameter params.book set to "my-book". But what if some books ar ...

Creating a multidimensional array in React JS and rendering it on the screen

Is there a way to showcase the array of cards in two columns instead of one, with images displaying sequentially? return ( <div> { data?.length > 0 && data?.map(item=>{ return( ...