After reloading the page in Firefox, the :hover effect is activated immediately, unlike in Chrome on Mac OS X

Kindly test this codepen on both a) Firefox and b) Chrome. Follow these steps:

  1. Hover over the link with your mouse
  2. Click on the link and do not move the mouse cursor afterwards
  3. Wait for the page to reload.

If you haven't moved the mouse cursor, it will still be hovering over the link after the page reloads.

Firefox will now display the styles assigned to the :hover pseudo-class.

On Chrome (Mac OS X), the element will show its non-hovered state, which is preferable in this case.

Is there anyone who knows which browser handles this correctly, and how to make one browser behave like the other?

In my current situation, I'm looking for a way to prevent :hover from being activated immediately after a page reload. I would prefer to avoid using JavaScript for this purpose.

Just to provide all the necessary information, here's the code snippet from the demo:

<a href="https://codepen.io/connexo/pen/pEJbqj" target="_top">This Codepen</a>
a {
  color: #333;
  background-color: #ddd;
  display: inline-block;
  line-height: 40px;
  padding: 20px;
  text-decoration: none;
  transition-duration: .4s;
  &:before {
    content: "non-hovered";
  }
  &:hover {
    background-color: #a00;
    color: white;
    &:before {
      content: "hovered state";
    }
  }
}

Edit: I was just informed by a colleague that Chrome behaves differently on OS X compared to Windows. Can someone provide more details about this issue?

Answer №1

Understanding Appropriate Behavior

It is challenging to determine the correct behavior as the W3C specification does not provide intricate details on the workings of the :hover pseudo class:

The :hover pseudo-class is active when a user hovers over an element with a pointing device, but it does not necessarily activate it. For instance, a visual user agent might apply this pseudo-class when the cursor (mouse pointer) hovers over a box generated by the element.

Learn more about user action pseudo-classes like :hover, :active, and :focus

Both behaviors mentioned seem plausible under the given circumstances.

Preventing Immediate Triggering of :hover After Page Reload

In this scenario, if you wish to delay the hover state activation until the user moves the mouse, you can follow these steps:

  • Add the CSS property pointer-events: none; to the link on page load to disable any mouse events initially
  • Attach a move event to the body that triggers once the user moves the mouse and then unbinds itself
  • In the move event, set pointer-events: auto; on the link to enable mouse events

$("a").css("pointer-events", "none");
$("body").one("mousemove", function() {
  $("a").css("pointer-events", "auto");
});
a {
  color: #333;
  background-color: #ddd;
  display: inline-block;
  line-height: 40px;
  padding: 20px;
  text-decoration: none;
  transition-duration: .4s;
}
a:before {
  content: "non-hovered";
}
a:hover {
  background-color: #a00;
  color: white;
}
a:hover:before {
  content: "hovered state";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" target="_top"></a>

This demonstration is most effective when viewed on either Codepen or JS Fiddle since a Stack Snippet is collapsed by default.

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

Renaming file names for Bootstrap 4.5 demonstrations

Trying to utilize a Bootstrap 4.5 example, but running into issues when renaming the HTML or CSS files - it breaks the formatting link for the page. Is there a way to duplicate the original example files and modify the names (such as "my_page.html" or "m ...

The button mysteriously vanishes on Github Pages, yet remains visible when viewing the

I recently completed a project on Github and created a corresponding Github Page for it. The page features multiple buttons with different functions, but oddly enough, one specific button is not showing up on the Github Page. Surprisingly, it appears perfe ...

Troubleshooting Material UI Widget Components

After working diligently on the autocomplete component, I am pleased with how the feature turned out. However, when this component is rendered as a widget on other pages, I'm encountering some style issues due to global styling overrides or additions ...

Setting a defined width and using block display will not solve the issue of the margin:auto not working

rough sketch of my desired design I am trying to center a label on the page. The parent container is set to a width of 100% and the label itself is displayed as a block element with margin set to auto. When I set the width of the label to a smaller value, ...

Create a layout with a single column that features a dynamic image, paired with another column housed within a modal using Bootstrap

I'm facing a design issue with a row that has two columns - one with an image and the other with a list of users in a modal window. As more users are loaded when scrolling down the modal, I want the image to remain fixed in its position. Here's t ...

Ways to retrieve base64 encoded information from an image within an HTML document

On my registration form, users have the option to select an avatar from 2 choices: Select a default avatar Upload their own custom avatar This is how I have implemented it in my HTML page. <img id="preview" src="img/default_1.png"> The chosen av ...

Organizing JSON Data into Paragraphs

I'm working on a basic web application using Angular JS. Within my JSON object, I have several paragraphs that I need to split onto separate lines. I tried looking for a way to force a line break within the JSON object, but couldn't find a solut ...

Redirect after the user submits the form by using `window.location.href` to change the current

I attempted to test this straightforward code, but was surprised when it didn't function as expected. Here is the code snippet: <form method="get" action="" > <input type="submit" value="validate" onclick="redirect(); alertIt(); "/> & ...

Exploring json data with angularjs

I am facing a challenge with handling a complex JSON object by looping through it and displaying the data in a form. To tackle this, I have created a small service similar to the one shown below: service.myApprovalResults = function(){ var url = " ...

Why does the font on my website appear altered compared to the font in the ttf/woff files?

This is an example of the font's appearance. Here's how the font appears in the .ttf file format. And this is the visual representation of the font on my website. #top { margin: 30px 30px 0px 150px; border: ...

Transform the color of links in a Sankey diagram on Google upon clicking a node

Currently, I am in the process of creating a Sankey diagram using Google charts. As of now, I have implemented the following CSS: svg path:hover { fill: red; } However, I have noticed that this code only changes the color of the links when hovering over ...

Brainstorm: Creative ways to showcase excess content within a div

I'm struggling to find a suitable title for this issue. Essentially, I have a box-shaped div that expands into a rectangle and reveals content when clicked. The problem arises when I animate the div's width during expansion. Instead of positioni ...

arrange and position elements using bootstrap's grid system

I'm currently utilizing bootstrap for my web application and aiming to have my divs display in a specific layout: https://i.sstatic.net/pJHmn.png Below is the snippet of my HTML code: <div class="row"> <div class="col-lg-8 col-md-12 co ...

Unique rephrased text: "Amongst a backdrop devoid of white,

In a div with a colored background, I noticed that adding an input element results in an unsightly border. <div class="input-group"> <input type="search" style="height:30px;"> </div> Is there a way to remove this border? ...

Vertically aligned text and a colorful sidebar enhance the accordion-style slideshow

Currently, I am in the process of creating a website for a health organization and have successfully implemented an accordion image slideshow feature. Here is what I have accomplished so far: However, my goal is to enhance the slideshow by incorporating a ...

Creating a visually appealing layout with rows of images of varying heights to ensure consistent spacing between text

As part of my Photo Library project, I am designing a display page that showcases four key attributes of each photo - File Name, Title, Date Taken, and Photographer Name. Additionally, there will be a preview thumbnail of the photo itself. While I aim for ...

Issues with CSS overlays arise when attempting to display a welcome page on top of a flash component

Can anyone help with fixing the issue of my overlay being hidden behind the flash element? The blue bar with the 'Continue' button still shows up on top. I need some CSS changes to make sure that the overlay appears above everything else below it ...

Transforming an HTML document into a C# string representation

I need help creating a basic HTML file and then dynamically loading it as a string in C#. I attempted using File.Open, but the file is located separately from the binary code. Is there an easy way to instruct the build process to load it into a string? ...

How to insert a custom logo into a bootstrap navbar on a Ruby on Rails website

I have encountered a similar issue to this, but I haven't been able to resolve it. I am currently working on my first application using rails and bootstrap. I've successfully created a navbar with a name and two buttons (you can view the screensh ...

Using jQuery to submit data via POST method without having the page reload

I have a link in the footer that, when clicked, jumps to the header with a # in the address bar. Is there a way to prevent this and stay in the footer? Here is the code snippet: <a href="#" class="clickIn" id="1" attrIn="Like"><span style="color ...