Bizarrely rapid transitions from standard to hovering appearance in CSS

After creating a custom styled button with a hover effect, I noticed an issue where if you happen to hover over a specific spot on the button, it quickly switches between its normal and hover states.

https://i.sstatic.net/uPq1F.gif

This is my code:

button {
  height: 34px;
  padding: 0 16px;
  border-radius: 4px;
  margin: 8px;
  border: none;
  background: red;
  color: #fff;
  transition: background .2s, transform .2s;
  outline: none;
  cursor: pointer;
}

button:hover {
  box-shadow: 4px 4px 0 0 blue;
  transform: translate(-1px, -1px);
}

button:active {
  background: green;
  box-shadow: 2px 2px 0 0 blue;
}
<button>I am a button</button>

Code

I'm looking for a solution to fix this problem. Any ideas?

Answer №1

The issue lies in the hover transform effect causing the button to constantly move back and forth as you hover over it. A potential solution is to eliminate the transform effect altogether.

If removing the transform effect is not feasible, an alternative approach would be to encircle the button with a transparent border when hovered. This way, you will need to move further away from the button to cease hovering, creating a more stable hover behavior.

button {
  height: 34px;
  padding: 0 16px;
  border-radius: 4px;
  margin: 8px;
  border: none;
  background: red;
  color: #fff;
  transition: background .2s, transform .2s;
  outline: none;
  cursor: pointer;
}

button:hover {
  box-shadow: 4px 4px 0 0 blue;
  transform: translate(-1px, -1px);
  border: 1px solid transparent;
}

button:active {
  background: green;
  box-shadow: 2px 2px 0 0 blue;
}
<button>I am a button</button>

Answer №2

To solve this issue, consider adding a container with a slight padding and monitoring the hover event on it instead of the button itself. This stationary container should prevent any shifting problems.

#buttonContainer {
  display: inline-block;
  padding: 2px;
}

button {
  height: 34px;
  padding: 0 16px;
  border-radius: 4px;
  margin: 8px;
  border: none;
  background: red;
  color: #fff;
  transition: background .2s, transform .2s;
  outline: none;
  cursor: pointer;
}

#buttonContainer:hover button{
  box-shadow: 4px 4px 0 0 blue;
  transform: translate(-1px, -1px);
}

button:active {
  background: green;
  box-shadow: 2px 2px 0 0 blue;
}
<div id="buttonContainer">
    <button>I am a button</button>
</div>

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

Make the background image draggable while maintaining its size with background-size:cover

I'm working on a fixed div with an image as the background, set up like this: <div style=' width:230px; height:230px; background-repeat: no-repeat; background-image:url(img/myimage.jpeg) background-size: cover;'&g ...

Creating a rotating circle in CSS with ion-slides: A step-by-step guide

Hello, I am attempting to develop a circular object that will rotate in the direction it is dragged. Within this circle, there are elements positioned along the border so that these elements also spin accordingly. To illustrate, below are the specific elem ...

Left-align the tab elements

I am looking to customize this example for Vertical tabs. Sandbox: https://codesandbox.io/s/v0p58 My goal is to have the tabs aligned on the left side. I attempted to include: alignItems: 'left' tabs: { borderRight: `1px solid ${theme.palet ...

If there is a lack of text at the beginning, then insert the

I am currently trying to figure out a solution to automatically add our domain name if it is not included when entering the username. In the code snippet below for my form, I want the script to check if "domainname\" is present before the username. I ...

"Why is it that only one of the two similar spans with onclick event is not functioning properly

Encountering a perplexing issue that has me stumped. Here's the code in question: js: function Test() { alert("test"); } html: <span id='bla' onclick='Test()'> Click me </span> <hr> <span id='bla2& ...

Utilizing Regular Expressions in Vb.net for extracting telephone numbers

I have developed a code to extract mobile numbers from a web link. I have three links in a list box and I am extracting their source code using the following code. However, when I try to use RegEx to Extract Phone Numbers, I keep getting the same number re ...

Display the menu and submenus by making a request with $.get()

My menu with submenu is generated in JSON format, but I am facing issues displaying it on an HTML page using the provided code. Can someone please assist me in identifying what mistakes I might be making? let HandleClass = function() { ...

Greek symbols do not display correctly in SQL Server when accessed through PHP

At my server database, there is a table with collation set to greek_ci_ai, but I am facing an issue where Greek characters are displaying as question marks(????). I have attempted using header("Content-Type: text/html; charset=iso-8859-7") and <meta ...

Guide on how to use JavaScript to swipe through loaded epub files in both lateral directions

Hello everyone, I have a question about implementing swipe functionality on a loaded epub using JavaScript. I successfully loaded the epub into a div and now I need to enable swipe left and right gestures on that div. I found a jQuery script that should a ...

How can I apply a blue border to highlighted elements in the live preview?

Upon clicking on the div or element within the brackets, the live preview in Chrome briefly flashes blue and then fades away. However, it does not create a thin blue border around the element like I have seen in other people's videos. Can someone plea ...

Dynamic JavaScript button control based on time

I am currently working on an app and my goal is to have a feature where the user clicks a button, it will then disappear and only reappear after 24 hours. Here's my progress so far. <div class="buttons"> <p><button onclick="hide(); ...

Issue with jQuery DataTable: Unable to use column-level filters at the top and maintain a fixed height simultaneously

I am having an issue displaying data in a jQuery DataTable with a column level filter at the top, fixed height, and scroller enabled. Initially, I was able to display the column level filter at the top and it was functioning properly. However, when I set t ...

Creating a specific quantity of divs with JavaScript

For the past few hours, I've been working hard to solve this problem that has really left me stumped. As far as I can tell, everything is correct. The create function is supposed to generate a certain number of divs specified by the user in an input b ...

Webpack is failing to recognize certain CSS files

My development stack includes Vue.js 2.5.15, Webpack 4.12.0, css-loader 0.28.11, ASP.Net Core 2.1 in Visual Studio 2017. Starting with the Visual Studio asp.net core template project for Vue and Typescript, I prefer to have individual small CSS files with ...

Issue with displaying jQuery 3.5.1 mobile CSS formatting on live server preview using Visual Studio Code

Despite my best efforts, I can't seem to make the jQuery CSS stylesheet work properly. I've been using the live preview extension in VSCode and got the CSS directly from the jQuery website. <!DOCTYPE html> <html> <head> < ...

Dynamic script appending encounters unforeseen challenges

After attempting to add a script either on click or after a time delay, I am encountering an issue where the script is not being appended. Strangely, if I remove the setTimeout function, the script gets added successfully. The same problem persists with ...

How can I ensure that the background color of my HTML email blends seamlessly with the color of the email client's background?

As I embark on creating an HTML email template, I am venturing into the realm of developing for emails, which I know can be quite temperamental. The challenge I'm facing is in making sure that the background of my HTML email matches that of the email ...

Guide to setting the ng-selected value within AngularJS

I am currently working on a project where I have a select element in my AngularJS application. I am populating the options using JSON values with ng-repeat, and I want to display the first value from the JSON as selected by default. I have tried two diffe ...

Adjust the sizing of all fonts following the switch in font styles

Seeking help for adjusting font-size across a responsive web page design using different font families. font-family:Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif; To achieve responsiveness, various media queries were applie ...

Overlaying images on top of text

Is there a way to overlay an image on text, like a pattern image? Similar to applying color with a hex value in CSS: color: #ffffff; Any ideas on how this can be achieved? I want to have a pattern over text. https://i.sstatic.net/JaNZU.jpg Appreciate ...