Android device automatically opens link when CSS :focus is triggered

When using a website, I apply :hover for web and :focus for touch devices on a link. However, on Android devices, when I touch the link, it triggers the :focus and then automatically opens the link.

My goal is for it to trigger the :focus and only open the link if the user clicks on it again. Can this functionality be achieved using pure CSS?

Below is a snippet of my :hover and :focus code:

#menu li:hover ul.sub-menu, #menu li:focus ul.sub-menu{
    display:block;
}

This issue seems to only occur on Android devices, as it works perfectly on iOS.

Answer №1

To detect if a user is using a touchscreen device, you will need to utilize some JavaScript (specifically jQuery, which is likely already integrated into your website) along with Modernizr. While there are alternative methods for checking touch compatibility, in my experience, Modernizr provides the most reliable outcomes.

Start by incorporating Modernizr into your project. You can either download it directly from their official website or use a content delivery network like cdnjs.com.

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/modernizr/2.7.1/modernizr.min.js"></script>

Next, add the following JavaScript to your website:

$(document).ready(function(){
    if(Modernizr.touch){
        $('#menu-mainmenu').on('click', '> li', function(e){
            if(!$(this).data('open')){
                e.preventDefault();
            }
            $(this).data('open', true);
        });
    }
});

If a user is using a touch-enabled device and clicks on a main menu item, a submenu will appear (thanks to the :focus styling). However, the link will be prevented from opening due to e.preventDefault(). Subsequently, the 'open' data attribute is set to true. Consequently, if the user taps the link again, the conditional check will fail, and the link will open normally. Although I haven't tested this extensively, it should function as intended...

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

Expanding the clickable area of a link using the CSS :before pseudo-element selector

I am currently working on a project involving an image slider and I am facing a challenge with the "previous" and "next" arrows. My goal is to have each arrow occupy half of the slider area, but adjusting padding or margins tends to change their position o ...

Upon integrating expo into a minimalist react native project, I encountered the following error

Upon installing expo within my react native project, I encountered an issue while attempting to create a release build with the command ./gradlew bundleRelease. The error message received is as follows. Has anyone else faced this problem? > Task :expo ...

Extract information from a constantly changing HTML webpage using Java

Is there a way to retrieve data from the Live Cyber Attack website? I am looking to extract specific information such as Time, Type of Attack, Attacking Country, and Target Country. I need this data to be dynamic for analysis purposes. Currently, I am wo ...

Executing various onclick functions - Text Display

I've developed a code that includes onclick events to change the color of buttons and prevent them from being clicked twice. Additionally, I am looking to have data added to a table column/row when a button is clicked. For example, clicking button 3 s ...

The alignment and sizing issues with stacking icons using fontawesome are still unresolved

I have been exploring the use of stacked icons with fontawesome, following a helpful blog post by Dave Gandy. Although stacking is working well, I am facing an issue aligning a stacked icon with a non-stacked icon (which is fa-5x). It's challenging t ...

Vue.js - Including new information in an array and passing it to the props (assistance needed)

I am facing an issue where I want my component to be able to add a new sidebar menu item every time the user clicks an add button. Essentially, my component should appear when the user is defining their own sidebar menu items. Here is the Vue.js code that ...

Encountered an issue launching the virtual device in Eclipse

After setting up my Eclipse with ADT and SDK, I decided to create a new android project. I proceeded by right clicking on the project name and selecting Run As -> Android Application. However, I encountered an error message in the console that looked li ...

The debate between centralization and specification: the ultimate Javascript/jQuery best practice for web applications

Picture a scenario where a web application consists of numerous page groups (pg1, pg2, ...) and some of these page groups require specific JavaScript code that is only relevant to them, not the entire app. For instance, certain visual adjustments on window ...

The FormData function is unable to retrieve the input fields of a form

My Unique Code <!DOCTYPE html> <html> <body> <div id="custom-form-sample"> <form enctype="multipart/form-data"> <input type="text" name="custom-text" /> <input type="radio" name="custom-radio" ...

Is there a way to validate form elements in HTML prior to submitting the form?

In my form, I am utilizing PHP for validation and processing, but I am interested in verifying elements as they are being filled out. Is there a way to check the current value of an element before the form is submitted? ...

Is there a workaround utilizing calc() and vh specifically for Chrome browsers?

Is it possible to find a CSS-only solution for incorporating vh in calc() functions specifically for Chrome browsers? I am trying to implement calc(100vh - 25px) as the top margin in order to make a 25px bar stick to the bottom of the page, but I am facin ...

When table cells are added dynamically, they are not encompassed by the solid fixed title bar when scrolling

My webpage has a fixed title bar with a high z-index that overlaps all other elements on the page when they are scrolled up, except for the dynamically created table cells. What attributes or styles do I need to set in order to hide the table cells behin ...

Capturing Moments with Android Photo Upload

Seeking help with replicating an iOs app that has a photo upload feature utilizing the provided code snippet: -(void)writePhotoToDirectory{ //Implementation details } I need to understand how this code functions as I want to replicate the same f ...

What are the steps for implementing this javascript code in an HTML document?

Recently, I've been seeking advice on how to address the issue of wanting a button click to automatically select the search box on my website. Suggestions have pointed towards using Javascript for this functionality, with the following code recommend ...

Having trouble with integrating user input from HTML into a JavaScript file to execute a GET request

I am currently working on a project to create a website that integrates the google books API for users to search for books. To start, I have set up a server using express in index.js at the root of the project directory, and all my static files are stored ...

Modify the innerHTML to adjust font size when a button is clicked in Ionic 5, or eliminate any unnecessary spaces

I have been experimenting with changing the font size of a variable in .html when the variable contains whitespace. In my .ts page, I use the following code to remove the whitespace: this.contents = this.sanitizer.bypassSecurityTrustHtml(this.product[&apos ...

Offspring of div should have equal height without the top and bottom padding

Looking to ensure that the height of child divs matches the height of the parent divs. Currently, the divs expand as the page is resized. This is for search results, so there are multiple identical code blocks. function matchHeight() { $result = $(&a ...

Discovering the dimensions of a disabled attribute within a table using XPath in Selenium with Java

I'm attempting to determine the number of columns in a specific table, but some are disabled - I'd like to know if it's possible to get the count without including the disabled ones (only counting the visible columns). As you can see in the ...

I am currently working on developing an application that is designed to automatically place sequential phone calls from a predefined list of numbers

I am currently working on an application for a friend who works in sales. The goal of this app is to automatically make phone calls one after another, without any interruptions. Once a call ends, the app will immediately dial the next number on the list. T ...

Creating a Select Directory feature with React - A Step-by-Step Guide

I am in need of uploading all files from a folder to the server. I have been attempting to use a Select Directory window instead of selecting individual files. The standard method: <input type="file" webkitdirectory directory/> has not worked for ...