Double tap problem with pseudo elements on iOS links

Trying to achieve a scrolling line effect beneath anchor links for a client project led me to attempt some CSS3 coding, only to find myself stuck with an elusive bug after spending hours on it. The issue arises on mobile devices (iOS and Android) where, upon clicking a link, the animation runs but requires a second click to trigger the link itself (same issue on the provided codepen). Can anyone shed some light on this perplexing situation?

http://codepen.io/pablodancer/pen/ZLJVOP

li {
  display: inline-block;
  list-style-type: none;
}
li a {
  text-decoration: none;
  position: relative;
  line-height: 1;
  height: auto;
  padding: 0;
  margin-right: 8px;
  padding-bottom: 8px;
  z-index: 1;
}
li a:after {
  display: block;
  left: 0;
  bottom: 0;
  margin-top: 4px;
  width: 0;
  height: 5px;
  background-color: blue;
  content: "";
  z-index: -3;
  transition: width 0.3s;
}
li a:hover:after,
li.active a:after {
  width: 100%;
}
<ul>
  <li class="active"><a href="http:www.nme.com">nme</a></li>
  <li><a href="http:www.bbc,co.uk">bbc</a></li>
  <li><a href="">blah3</a></li>
  <li><a href="">blah4</a></li>
  <li><a href="">blah5</a></li>
</ul>

Answer №1

It seems that the issue with double tapping is specific to iOS devices. My solution is to hide the pseudo element on touch devices. There are two approaches that can be used:

(1) By using CSS media queries, this method is effective for iOS 9+ and Android 5+.

@media (hover: none) {
  li a:after {
    display: none;
  }
}

(2) Alternatively, a combination of Javascript and CSS can be used:

(function(html) {
  html.className += ('ontouchstart' in window) ? ' touch ' : ' no-touch ';
})(document.documentElement);

.touch li a:after {
  display: none;
}

If you want to maintain the active style, you can use the selector li:not(.active) a:after. Additionally, setting li {vertical-align: top;} can help align the items nicely.

Answer №2

The reason behind this issue lies in a unique behavior specific to WebKit on IOS.

A peculiar (yet frequently encountered) problem calls for an unconventional (but effective) solution. Here's how I managed to resolve it using only CSS, ensuring compatibility with various browsers.

The key lies in utilizing transforms in a clever way to prevent IOS/WebKit from considering the pseudo element as hidden, thus eliminating the need for double-tap behavior on hover:

li a:after {
  /* Maintain visibility and size of the element */
  display: block;
  content: '';
  width: 100%;
  height: 2px;
  ...
  /* Conceal with a transform */
  transform: scaleX(0);
  ...
  /* Apply a smooth transition */
  transition: transform 600ms ease;
}

li a:hover:after {
  /* Reveal the element by resetting the transform */
  transform: scaleX(1);
}

So far, this approach appears to be effective. Why does it work? Because the transformed element's size is not factored into the page reflow calculation :)

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

Combining various array values into a single key in JSON格式

Issue: I am working on a sign-up form for new users using HTML. The goal is to store multiple arrays (each containing "username: username, password: password, topScore: 0) within one JSON key ("user" key). However, the current functionality only allows f ...

Guide on fetching data from a different php file using jQuery's .load() method?

I am trying to use a basic .load() function from jQuery to load a div element with an id from another file in the same directory when changing the selected option in a selector. However, I am having trouble getting it to work. Nothing happens when I change ...

Unable to alter the background color in a table row when a condition is verified

Within my 'work' array that is generated from an Excel file, I have values for employee_id (referred to as id), projects, and hours. Additionally, I have another array called 'employees' which contains all the employees in my database. ...

What is the best way to position a button at the bottom of an image?

I am facing an issue with the following code snippet: * { box-sizing: border-box; } .wrapper { position: relative; /* border:1px solid blue; */ width: 250px; } .text { position: absolute; border:1px solid red; bottom: 0px; ...

Arranging Multiple Files in Sequence Using HTML5 File API Instead of Uploading All Simultaneously

I am currently working on a BackboneJS/Marionette App and I want to enable users to upload multiple files. Right now, the functionality works when users select multiple files simultaneously, but I would like to give them the option to select one file init ...

Capture information and store it in a database through the use of AJAX technology

I want to implement AJAX for real-time saving of user input comments directly to the database. Here is where users can input their comments: <div class="comment"> <form action="comment.php"> <textarea cols="35" name="comment ...

Footer remains attached to the bottom of the page while the content extends too far down

Having some trouble with my sticky footer - despite checking out various resources and examples, I can't seem to achieve the desired outcome. Update: I don't want the footer to be fixed. I want it to stay at the bottom of the screen if there isn ...

The POST method in XHR encounters issues on IOS when used with the Canvas to Blob script

Observation I've noticed a specific part of the code that's not functioning correctly only on IOS devices. xhr.open('POST', 'upload.php', true); xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); var da ...

Navigating pages in tinymce editor

Currently, I have implemented TinyMCE as the editor for my PHP-based website. However, I am facing a challenge in integrating pagination within the editor. My goal is to allow users to input long content that will be displayed across multiple pages inste ...

Hiding a Hide/Show DIV when hovering over the Google+ Follow Button

I'm currently working on a project that involves a hover drop-down panel with Google+, Facebook, Youtube, and Twitter social widgets. These widgets are listed for visitors to easily click on each one without the panel closing. Initially, I created a ...

Style formatting is applied to the addition of fresh content on a webpage

As I develop a front end page that pulls data from multiple sources, my goal is to allow for dynamic addition of new sources without the need for code changes. This includes the ability to add new source URLs on the fly. Additionally, certain data sources ...

Type of element returned

Is there a way to retrieve the element type? I'm interested in applying the style of a class to HTML5 elements without using the class attribute. <!DOCTYPE> <html> <head> <title>My page</title> </head& ...

Tips for personalizing PNG images within an SVG element

Looking to update the color of a PNG image within an SVG tag. The PNG images I have are transparent, and I want to customize their colors based on user selections. How can this be achieved? Is there anyone out there who can assist me? <HoodieSvg ...

Rotate Text in HTML <thead> Tag

I need assistance with rotating a table header. https://i.stack.imgur.com/hcvxx.png The HTML th elements within the thead section are described as follows: <th> <span class="rotate-all">Period</span> </th> <th> < ...

JavaScript radio button returning value as undefined issueThe problem with radio buttons

http://jsfiddle.net/jngpjbjm/ Take a look at the provided fiddle link. I'm having an issue where the radio button value is showing up as undefined. I can't figure out why this is happening. Any assistance would be greatly appreciated. <input ...

What is the best way to align a modal with a layout when it appears far down the components hierarchy?

Struggling with creating a React modal and facing some issues. Let's consider the structure below: React structure <ComponentUsingModal> <Left> <ButtonToActivateModal> ... </ButtonToActivateModa ...

How to Alter Button Color upon Click in React with Bootstrap?

I'm working on implementing a thumbs up and thumbs down feature similar to Reddit's upvote and downvote system. The goal is to change the color of the object to green when the thumbs up is clicked and to red when the thumbs down is clicked. How ...

Error alert: $.simpleWeather function not found

Currently, I am attempting to incorporate simpleWeather.js from the website simpleweatherjs.com into my own website. However, upon inserting the html code onto my website, an error message pops up: Uncaught TypeError: $.simpleWeather is not a function ...

Converting an Integer to a String and then manipulating the String format

<p class="card"> Credit Limit: <b style="color: #00e500 ;"> <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcf89c91b3b8b9b0f29faeb9b8b5a890b5b1b5a8f288b38fa8aeb5b2bb">[email protected]</a>().ToLocaleSt ...

Ways to align elements horizontally while preventing them from shifting positions

I'm faced with a layout challenge where I need to place 2 components side by side, but I want one component to stay on the right without affecting the orientation of the other component. Example: https://i.stack.imgur.com/T5Ram.png Currently, when I ...