Trouble with CSS dropdown menu performance when using an iPad

My CSS-only dropdown menu works perfectly on desktop, but on mobile Safari on an iPad, when a top-level navigation link is clicked, the dropdown menu briefly appears before the user is taken to a new page.

I've researched extensively and found that iPads are supposed to treat the first tap as a hover due to the :hover style affecting the display property of the submenu. This should prevent the link from being followed on the first tap.

Other websites demonstrate this behavior correctly, as does other parts of my website, but for some reason, it doesn't work properly with my CSS dropdown menu.

You can view the problematic menu on my site, . On an iPad, tapping on a menu item like "Surfboard" immediately navigates to the Surfboard page rather than displaying the dropdown menu. Occasionally, the dropdown displays as expected without navigating, but this is rare and inconsistent.

Below is the relevant markup for the menu:

HTML

<ul class="mainNav-menu">
    <li>
        <a href="/sup-racks-bags/">SUP</a>
        <div class="mainNav-submenu">
            <ul class="mainNav-submenu-menu">
                <!-- Submenu items here -->
            </ul>
        </div>
    </li>
    <li>
        <a href="/surfboard-racks-bags/">Surfboard</a>
        <div class="mainNav-submenu">
            <ul class="mainNav-submenu-menu">
                <!-- Submenu items here -->
            </ul>
        </div>
    </li>
</ul>

CSS

.mainNav-submenu {
    display: none;
    position: absolute;
    left: 0;
    right: 0;
    top: auto;
    background-color: #8a8a8a;
    z-index: 20;
    padding: 1rem;
}

.mainNav-menu > li:hover .mainNav-submenu {
    display: block;
}

Various posts suggest adding onclick="return true" to fix the issue, but this didn't work for me. I'm also using fastclick.js, which I thought might be causing the problem, but removing it had no effect.

I tested a solution mentioned in this article, which worked correctly. Tapping showed a message, and a second tap navigated away.

The same behavior doesn't kick in for my navigation menu. Unsure if it's due to how it's marked up or styled. Any ideas on what might be wrong? Any suggestions would be appreciated.

UPDATE

Following Doc's answer below, I applied this solution:

.mainNav-menu > li > a {
  pointer-events: none;
}

.mainNav-menu > li:hover > a {
  pointer-events: all;
}

The fix is live on the site now. Still unclear why it was necessary and why it failed originally. Any insights are welcome.

Answer №1

It's baffling why simply tapping the a link causes it to be followed, but implementing this solution should rectify the issue.


To begin, ensure there is consistent text accompanying all tags, including the link. Maintain

<a href="/surfboard-racks-bags/">Surfboard</a>

and include

<p>Surfboard</p>

Verify that both elements share identical content and styling.

Next, hide the link by default using display: none, making it invisible. When the div is hovered over, reveal the link (perhaps with display: inline) while hiding the non-link element. Ideally, seamlessly switch from the p element to the styled a element as if the a were always present.

When accessed on a desktop, render the link inactive when the div is not being hovered over, displaying regular text instead. On hover, show the link for user interaction.

For iOS users, tapping the div should trigger the :hover effect rather than following the link since it was initially hidden. To prevent accidental clicks on the link, consider adding a transition delay such as transition: display 0s 0.1s to ensure proper visibility.

The drawback of this method is that iOS users may struggle to close the dropdown by clicking since it could inadvertently activate the link. However, clicking elsewhere on the screen should resolve this issue.

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

Suggestions for preventing the highlighting of the space between buttons on a webpage

html: <button id='automoney' onclick='minusTen()'></button> <button id='automoney2' onclick='minusHundred()'></button> <button id='automoney3' onclick='minusFiveHundred()& ...

Ways to add more spacing between list items without affecting the position of the bottom div

I'm attempting to achieve a layout similar to an Instagram post, where the list expands but does not push the footer down. In my case, adding margin-bottom to the comment class affects the height of the <div class="post-details-container" ...

Execute javascript on webpage after reloading

My goal is to dynamically refresh a website and run a JavaScript script on it, simulating the behavior of executing code in the Chrome console. How can this be accomplished? One method I am considering involves using PHP to set up a local server that red ...

Using the `timeIntervalSinceNow` function to retrieve the elapsed time value

Here is a snippet of my code: NSDate *currDate; if(someCondition) { currDate = [NSDate date]; } //perform some tasks //calculate the number of milliseconds elapsed since currDate was set if([currDate timeIntervalSinceNow] > MAX_MILLISECS) ...

Dimensions of the physical screens for the Iphone XS Max and Iphone XR

After updating my Apple devices, I decided to check the physical screen sizes in pixels of the new products. For the iPhone XS, the measurements are as follows: Width / height: 1125 / 2436 pixels Scaling Factor: 3 Physically: 375 / 812 points (similar ...

Solve the issue of aligning two contents side by side using Bootstrap

I'm struggling to display two content items in one row. Here's my code snippet: <div class="row"> <div class="form-group"> <div class="col-lg-1"> <label>Insured First ...

Welcome to the PHP ChatRoom where only messages are displayed without revealing the usernames! Join in

I have successfully created a Chat Room using PHP, but I am facing an issue where only the messages are being displayed without the corresponding usernames. Separate databases for usernames and messages have been set up, yet the usernames are not appearing ...

Looking to adjust the positioning of text being inserted into a column - unable to utilize text-align or float properties

I'm struggling to position my text in a specific area within a column using CSS. Our app displays a price when a quantity is selected, but I can't seem to center the $14 under 'Item Total'. Even using float: center; hasn't worked f ...

Issues arise with alignment of the button in an inline form when the Bootstrap navbar collapses

Currently delving into the world of Web Development and experimenting with BootStrap 4.0. I have a search field positioned to the right of the navbar, which looks fine in desktop view. However, as soon as the dimensions change to that of a phone, my nav li ...

When the modal is closed, the textarea data remains intact, while the model is cleared

My challenge involves a simple modal that includes a text area. The issue I am facing is resetting the data of the textarea. Below is the modal code: <div class="modal fade" ng-controller="MyCtrl"> <div class="modal-dialog"> <d ...

Can someone guide me on how to iterate through a form in jQuery starting from the bottom and moving upwards, beginning with the innermost element, when the ID of the deepest element is unknown

Within my MVC web application, I have implemented a dynamic form builder that organizes forms into sections and questions. Sections can contain either questions or more sections. The objective is to have certain sections open based on specific question in ...

Guide to triggering a click event programmatically at specific x and y coordinates using JavaScript

The event api allows for the creation of a synthetic MouseEvent that can be programmatically dispatched by a DOM element. By adding an event listener to the document, you can capture the event dispatched by the child element during the bubble phase. For ...

Using jQuery UI to style div elements with dynamic titles

Is it possible to style a standard div container using the same styling as jQuery dialogs? I am looking to create a div panel with text content but with the title bar styling similar to that of jQuery UI dialog boxes. Appreciate your assistance in advance ...

The required `React-Core` specification for `RNCPicker` version 1.6.0 with React Native 0.59 could not be located

Encountered an issue when attempting to run pod install for RNCPicker version 1.6.0 with React Native 0.59. Analyzing dependencies [!] Unable to find a specification for `React-Core` relied upon by `RNCPicker` Possible reasons: * outdated source reposito ...

Conceal Orbit Slider Images until every single one is fully loaded

While utilizing Foundation 5's Orbit slider, I have encountered a minor issue where the images flash before fully loading into the slider when the page is first loaded. Is there any method to prevent this flashing and only display the images once they ...

Load elements beforehand without displaying them using a div

In order to efficiently manipulate my Elements using jQuery and other methods, I am exploring the idea of preloading them all first. One approach I have considered is creating a div with CSS display set to none, and placing all the elements I need for my w ...

The steps to decline an incoming call on a third device using Twilio

Currently working with the android SDK and encountering a specific scenario: client_A initiates a call to client_B and client_B accepts it -> resulting in client_A and client_B being connected. client_C then tries to call client_A, who is already engag ...

How do I incorporate pagination into a PL-SQL dynamic content table in Oracle Apex?

I've successfully created a PL-SQL dynamic content report in Oracle Apex, but I'm struggling with implementing pagination. With too many rows to display at once, adding pagination will greatly enhance the user experience. Here is a snippet of my ...

What is the best way to add spacing between the fields within a Bootstrap 4 form row that spans across 2 rows when viewed on small displays?

Trying to create a bootstrap 4 form with fields in one row for larger screens and two rows for smaller screens. The attempt was made using the following code: <div class="form-group row"> <label class="col-sm-2 col-form-label">File:</lab ...

Remove Labels Below Bars in jQuery Flot Charts

Take a look at the JSFiddle link I am trying to eliminate the labels below the bars to get rid of the blank space between the chart and the end of the container. The height of the labels may vary, causing the gap to be bigger or smaller. Using display:no ...