Disabling hover effects for mobile devices

I've been searching for a solution to this problem, but nothing seems to work for me.

In mobile viewports, I have a dropdown menu with styles applied on :hover, which shouldn't be active due to usability reasons.

The structure of the dropdown is similar to the following:

<div className={ styles.dropdownWrapper }>
        <span className={ styles.dropdownLabel }>{ label }</span>
        <object className={ styles.dropdownArrow }></object>
        <div className={ styles.dropdownOptions }>
            <div className={ styles.dropdownItem }>
                <span>Settings</span>
            </div>
            <div className={ styles.dropdownItem }>
                <span onClick={ () => console.log("logout") }>Logout</span>
            </div>
        </div>
    </div>

Styled as follows:

The code works correctly in Firefox desktop and mobile, as well as Chrome desktop, but not Chrome mobile. The issue seems to be that the :hover effect persists, causing the dropdown items to stay hidden instead of appearing when clicked.

I initially tried a solution from here, but it's no longer supported by Firefox desktop.

My latest attempt was updating the CSS like so:

@media (hover: hover) {
  .dropdownWrapper:hover .dropdownOptions {
    top: 100%;
    display: block;
    position: absolute;
    padding: 1em 1em 0.5em 1em;
    background-color: #36394f;
    border-radius: 2px;
    min-width: 100px;
  }
}

This fixed the issue in Chrome mobile but still doesn't work in Firefox desktop.

If anyone has any helpful advice, it would be greatly appreciated!

Answer №1

Implementing a mobile-first approach is highly recommended. This strategy involves setting up CSS rules specifically for mobile devices and then adjusting or adding to them for larger screens.

To clarify, begin by defining your CSS rules for mobile devices, and then utilize a media query to cater to larger screens.

@media only screen and (min-width: 769px) { }

Answer №2

@media only screen and (min-width: 769px) { 
 .dropdownWrapper:hover .dropdownOptions {
  top: 100%;
  display: block;
  position: absolute;
  padding: 1em 1em 0.5em 1em;
  background-color: #36394f;
  border-radius: 2px;
  min-width: 100px;
 }
}

Feel free to incorporate this code snippet into your project :)

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

Can you explain why the .js code is causing such high CPU usage in popular video players?

Why is the .js code running continuously even when the video is paused and there is no user interaction? I observed this phenomenon on a Windows 10 Atom Tablet, particularly when in energy-saving mode. The CPU usage for video playback and decoding almost ...

Guide on establishing a reference within a wrapped component in React

App.jsx <Provider store={store}> <Main {...this.props} ref={this.props.setRef}> </Provider> In my App.jsx file, I am setting a reference in the Main component. This will allow me to call a method from the Main component within another ...

How come my function does not properly update the visibility of the elements with each subsequent call?

I am currently implementing form validation and updating the display of a notification based on the status code received from an http request with the following code: function checkValidEndpoint() { var xmlHttp = null; var myurl = "/restyendpoint/ ...

A single click does not lead to the link when I press the button

I am facing an issue with a button on a form that should redirect the user to another webpage once the data is validated. The href attribute of the button will only have information if the data is valid; otherwise, it will be null. However, the problem is ...

poor scrolling performance when transitioning focus between elements in the interface

Looking for some assistance? Check out this codepen. I'm currently working on a search bar widget that allows navigation from the input field to the search results, displayed as a series of divs. The navigation is controlled via jquery, where the foc ...

Error: Promises must be managed correctly

I've been working on a timer-based function that is supposed to run once a week and create almost identical copies of existing documents. However, every time I try to execute it, I encounter the error message "Promises must be handled appropriately." ...

Most effective technique for resizing HTML elements

Currently, I am exploring different methods to scale HTML elements for a large screen, such as a kiosk. I have been using CSS3 scale() to achieve a relatively cross-browser scale, but I am curious if there are better options available. My main focus is on ...

What is the best way to showcase HTML content in columns with a horizontal scrolling feature?

I am trying to showcase a list of basic HTML elements such as divs, paragraphs, etc. within a fixed height container, arranged in columns with consistent width. I want them to be horizontally scrollable, similar to the layout displayed in this image. Thi ...

Enhancing Accessibility of the 'Return to Top' Link

Currently, I am designing a web page that requires users to scroll extensively. To enhance the user experience, I have included a back-to-top link at the bottom of the page for easy navigation back to the top. This is the HTML markup I have implemented: ...

What could be the reason behind the failure of my inner div to successfully implement the flex-row

I'm currently trying to position two divs next to each other horizontally, and my concept was to utilize flexbox, but including the property flex-row doesn't seem to yield any results and I can't comprehend the reason behind it. How can I a ...

Is there a way to eliminate the “Cancel” choice from the Draw button in the Leaflet.draw plugin?

My objective is to eliminate the ability to cancel a drawing, as illustrated in the image provided. Below is the code I have been working with: ...

Utilizing Jquery and Ajax to create a dynamic dropdown selection feature based on dependencies from a JSON file

Could you assist with the code snippet below? I have a form where each dropdown list depends on the selection above it. Based on the user's selection, the appropriate data should display in the subsequent dropdown list. I am trying to make dropdown l ...

Variety in Fashion

Recently, I came across a website where the html5 range input was styled with a customized handle. Is there a way to achieve this using CSS3 properties, or would it be better to create an image overlay and manage it with JavaScript? ...

Show varying HTML headers on the initial page as opposed to subsequent pages

Currently, I am utilizing an HTML to PDF Library known as Nreco for converting HTML Pages into PDF format. The task at hand involves implementing a consistent header on every page, with the exception of the first page that should display the Recipient Addr ...

A helpful tip for disabling by overriding webkit-transform without removing the line

I needed to make some changes to a css file but didn't want to alter it directly for personal reasons. Instead, I created a new css file where I overrode the specific lines that needed changing. However, now I'm wondering how I can cancel out thi ...

Leveraging ng-selected in AngularJS to effortlessly select multiple options from a collection

Two arrays of objects are causing me some confusion, with one array being a subset of the other: $scope.taskGroups = [ {id: 1, name: 'group1', description: 'description1'}, {id: 2, name: 'group2', description: 'descr ...

What is the procedure for making the menu background color transparent?

I need help changing the background color to transparent on my menu in desktop view. ul.dropdown-menu li { background-color: transparent !important; } <div class="collapse navbar-collapse navbar-ex1-collapse"> <ul id="menu" class="menu"> ...

I need to mass upload a collection of resumes stored in a zip file, then extract and display the content of each resume using a combination of HTML

I recently used a service to extract and retrieve the contents of a zip file. I am trying to read the content of the files and integrate them into the scope of my Angular project. Any suggestions would be greatly appreciated. Below is an outline of my func ...

Having trouble with my Min value Button in my HTML and Javascript code

I've encountered an issue with my code - I'm trying to find the minimum value from a list using a for loop, but it's not always working as expected. <script> /* Example one */ function findMin() { list = numbersBox.value.sp ...

When Node.js is executed as a script, how is the context of `this` determined?

When using the node REPL: $ node > var x = 50 > console.log(x) 50 > console.log(this.x) 50 > console.log(this === global) true It all seems clear when working in this environment. However, things take a turn when running a script: $ cat test ...