a:hover CSS style altering the behavior of buttons with images in a web browser

Earlier I attempted to ask this question from my phone, but it ended up being overly convoluted and confusing. Therefore, I have decided to start fresh after locating a working PC. Unfortunately, due to the sensitive nature of the project, I am unable to provide the full original code or images as they are offline. However, the simplified version below still presents the same issue that needs troubleshooting.

Below is the snippet of code in question: https://jsfiddle.net/mssdjrzk/

<!DOCTYPE html>
<html>
  <style>
    button {
      width: 24px;
      height: 24px;
      padding: 4px 0 0;
    }
    img {
      width: 16px;
      height: 16px;
    }
  </style>
  <body>
    <button type="button">
      <img src="https://cdn1.iconfinder.com/data/icons/famfamfam_mini_icons/action_refresh_blue.gif">
    </button>
    <hr>
    <a>Sample link</a>
  </body>
</html>

Upon hovering over the button, a default browser behavior triggers. In Internet Explorer 11, the button becomes highlighted.

Subsequently, I introduced additional CSS for a:hover: https://jsfiddle.net/yLrznyss/

<!DOCTYPE html>
<html>
  <style>
    button {
      width: 24px;
      height: 24px;
      padding: 4px 0 0;
    }
    img {
      width: 16px;
      height: 16px;
    }
    a:hover {
      color: red;
    }
  </style>
  <body>
    <button type="button">
      <img src="https://cdn1.iconfinder.com/data/icons/famfamfam_mini_icons/action_refresh_blue.gif">
    </button>
    <hr>
    <a>Sample link</a>
  </body>
</html>

When I hover over the button now, the behavior seems to be disrupted - it does not render correctly. Other events such as onclick work fine. Despite numerous attempts to troubleshoot and find a solution, my efforts have been unsuccessful. Here are some observations:

  • The element causing issues seems to be :hover. Whether applied to an element, class, or ID, its presence in the stylesheet causes problems. Even an empty a:hover{} will create issues, similar to span:hover{} when no <span> elements actually exist in the HTML.
  • This problem is specific to IE and does not occur in Chrome, where default hover behaviors for buttons are handled differently. Therefore, this is a browser-specific issue.
  • The problem only arises with buttons containing images, not those with text, empty buttons, or isolated images not enclosed within other elements.

My assumption is that the presence of :hover in the CSS stylesheet, even if empty, interferes with how IE renders the page and its behavior.

How can I prevent the button and/or its internal image from being affected, thus restoring the default IE button hover behavior? Any modifications are welcome as long as the desired hyperlink hover style is maintained without impacting the buttons.

The application utilizing this code will run on IE11 Windows 10 and solely supports solutions using HTML, CSS, or JavaScript without any external libraries available.

Answer №1

It appears that there are no anchor links within the button. The code I used to achieve the hover effect is as follows:

  button:hover {
      background: red;
    }

This successfully produced the desired hover effect.

Answer №2

After discovering a successful workaround, I realized that using the :hover selector was not an option for me, so I had to find a way to emulate it using JavaScript. I want to give credit to this solution on Stack Overflow that helped me achieve this.

<!DOCTYPE html>

<html>
  <style>
    button {
      width: 24px;
      height: 24px;
      padding: 4px 0 0;
    }

    img {
      width: 16px;
      height: 16px;
    }

    .isHovered {
      color: red;
    }
  </style>

  <script>
    document.addEventListener("DOMContentLoaded", function() {
      var links = document.getElementsByTagName("A");

      for (var i = 0; i < links.length; ++i) {
        (function() {
          var link = links[i];
          var hoverTimer;

          link.addEventListener("mouseover", function() {
            hoverTimer = setTimeout(function() {
              link.className = "isHovered";
            }, 0);
          });

          link.addEventListener("mouseout", function() {
            clearTimeout(hoverTimer);
            link.className = "";
          });
        }());
      }
    });
  </script>

  <body>
    <button type="button">
      <img src="https://cdn1.iconfinder.com/data/icons/famfamfam_mini_icons/action_refresh_blue.gif">
    </button>

    <hr>

    <a>Sample link</a>
  </body>
</html>

If anyone has a better solution or can explain why this issue is happening, please feel free to share your insights - especially if you can replicate it on your own machine.

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

How can I apply borders to individual columns within a Bootstrap table without affecting the rows?

My attempted solution involved adding th and tfoot. However, I faced issues with applying borders to only the td elements - border was not being applied at the bottom of the table, and the thickness and color of the borders were inconsistent between column ...

What is the procedure for transferring the inputted data from an HTML file to its corresponding TS file and subsequently to a different component file?

I have created two components, a login and a home-page. I am attempting to capture user input from the login template, pass it to the login component, and then display it on the home-page template using the home-page component. What is the best approach to ...

Creating a row of aligned vertical sliders using jQuery UI

I'm having trouble aligning multiple vertical UI sliders on the same line. Here's what I'm looking to achieve: 1. Have 4 vertical sliders displayed in a row. 2. Show numerical values as the user moves each slider. The code I'm currentl ...

Creating a custom AngularJS HTTP interceptor that targets specific URLs

Is there a way to configure an $http interceptor to only respond to specific URL patterns? For example, I want the interceptor to only intercept requests that match "/api/*" and ignore any other requests. ...

Using NodeJS to store the value from a callback function in a variable external to the function

I am facing a situation where I need to assign the value inside a callback function to a variable outside of it. Below is an example using child_process. callback.js let result; require('child_process').spawn('python', ['./hello.py ...

What steps should I follow to obtain code coverage data in my Aurelia application with the help of karma?

After creating my Aurelia app using the Aurelia CLI (au new), I wanted to set up code coverage, preferably with karma-coverage, but was open to other options as well. First, I ran npm install karma-coverage --save-dev and then copied the test.js task over ...

Why isn't the connect.use() function working in Node.js?

I have been studying and applying examples from a book to learn Node.js. While replicating one of the examples, which involved creating a middleware, I encountered an error when trying to run the JavaScript file. The error message stated "undefined is not ...

Warning: An unhandled promise rejection has occurred due to a TypeError, as the property 'title' cannot be read since it is undefined

I am encountering an error related to the router.post("/") function in my JavaScript, express, ejs, and MongoDB project. The main objective of this post request is to enable users to save a blog post, which will then redirect them to the main page ("/"). ...

Should I return X in async functions, or should I return "Promise.Resolve(X)"?

I've always found this to be a tricky concept to fully grasp. Let's delve into async functions in Typescript. Which implementation is accurate? async function asyncFunctionOne(string1: string, string2: string, string3: string) { var returnOb ...

Has CSS3 been recognized as an official standard?

Can you confirm whether CSS3 has been officially recognized as a W3C standard or is it currently in the status of "CR" (Candidate Recommendation)? ...

Issue: Unable to locate the module 'babel-code-frame' in VUEJS (ESLINT)

Here are the current versions: -npm: 6.14.4 -node: v10.19.0 -eslint: v5.0.1 -linux: ubuntu 20.04 This is my script: vue create vue1 cd vue1 npm run serve This is my package.json: { "name": "vue1", "version": "0. ...

The hyperlink function is malfunctioning when used with the embed tag in both Internet Explorer and Chrome browsers

<param id="movie" name="movie" value='<%= Me.ResolveUrl(Me.BannerPath)%>'> <a href='http://<%= BannerTargetUrl%>' target="_blank"> <embed wmode="transparent" src='<%= Me.ResolveUrl( ...

Clicking does not result in a change of the view

While experimenting with this code snippet : http://jsfiddle.net/9fR23/187/ Something seems off as the table elements are not being hidden when clicking on the "Flip!" div. The elements are supposed to be hidden based on the state change determined by th ...

What causes the image to remain unchanged while the rest of the divs update when navigating with the previous, next, or random buttons?

Whenever I interact with the navigation buttons - specifically previous, next, and random - the contents of all div elements change accordingly. The image element, however, remains unchanged. I am encountering an issue where the image does not correspond t ...

Utilizing D3 to fetch geographic data in the form of a TopoJSON file for U.S. counties

After retrieving a set of coordinates, the goal is to use D3 to find the corresponding county from a U.S. TopoJSON file. Here is an example code snippet: navigator.geolocation.getCurrentPosition(function(position) { let coordinates: [number, number] = [p ...

Arrange divs in a stack when one becomes too full

Struggling to find a pure CSS solution for this issue, so seeking advice. Imagine three divs aligned horizontally next to each other. If one div overflows onto the next line, I want them to switch to a vertical stack layout. How can this be achieved? I h ...

What is the best way to showcase a value in JavaScript using CSS styling?

I'm looking to customize the background, font style, and outline for both open and closed elements in the code snippet below: a.innerHTML = "We are Open now now."; a.innerHTML = "We are Closed, arm."; Additionally, I want to appl ...

Setting predefined data in an HTML field from XML within the Odoo platform

In the model.py file of my project, I have defined an HTML field as follows: from odoo import models, fields class TestModel(models.model): _name = 'test.model' content = fields.HTML() To display the data from this model, I have used &l ...

Using an AngularJS ng-repeat alias expression with multiple filters

As stated in the Angular ngRepeat documentation, the alias expression can only be used at the end of the ngRepeat: It's important to note that `as [variable name]` is not an operator, but rather a part of the ngRepeat micro-syntax and must be place ...

Trouble with DOM loading due to external JavaScript file reference

I'm having an issue with referencing the jQuery library in a master page. Here is how I am including it: <script type="text/javascript" src="../../Scripts/jquery-1.4.1.js"> After loading the page, I only see a blank screen. The HTML code is th ...