It is important for the CSS :active state to transition to an "inactive" state after a certain period of

For mobile, I utilized the :hover and :active pseudo-classes to create a transition effect when tapping on a tab that enlarges the elements for more information. However, I am seeking a way for the activated element to return to its original state after a certain period of time has passed. I have not been able to find a similar query.

Edit: It seems that the element remains in a "hovered" state permanently. I would like it to stop being hovered after a few seconds.

Edit2: http://jsfiddle.net/qw8vyrxd/

.box {
width: 40px;
height: 40px;
background-color: red;
}

@media screen and (max-width: 1000px){
.box:hover, .box:active{
width: 60px;
height: 60px;
}
}

On mobile devices, clicking on the red square causes it to expand due to the hover selector, but it does not shrink back unless the page is reloaded. I am looking for a solution where the hover effect only lasts for a couple of seconds.

Thank you

Answer №1

When using a touch screen, the state that remains on the element is :hover instead of :active. The :hover state persists until the user interacts elsewhere on the screen.

To address this issue, I applied the :hover style only to devices that support it by utilizing media queries like media queries hover:hover. For elements in :active state, no media queries were used as they would remain active as long as the user's finger is on the screen.

.box {
  width: 40px;
  height: 40px;
  background-color: red;
}

.box:active {
  width: 60px;
  height: 60px;
}

@media (hover: hover) {
  .box:hover {
    width: 60px;
    height: 60px;
  }
}
<div class="box"></div>

This approach effectively addressed the issue on my devices. Testing with real devices was necessary since DevTools tended to behave like a mouse even during simulated touch interactions.


Modified Approach

Given the difficulty of controlling pseudo states with JavaScript, I opted for a CSS-only solution. By setting the transition to 0s and then introducing a transition-delay of 2s, I implemented a workaround to make the element appear larger for a brief period on touch screens before reverting to its original size.

.box {
  width: 40px;
  height: 40px;
  background-color: red;
  transition: 0s height, 0s width;
  transition-delay: 2s;
}

.box:active {
  width: 60px;
  height: 60px;
  transition-delay: 0s;
}

@media (hover: hover) {
  .box {
    transition-delay: 0s;
  }
  .box:hover {
    width: 60px;
    height: 60px;
    transition-delay: 0s;
  }
}
<div class="box"></div>

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

Move divs that are currently not visible on the screen to a new position using CSS animations

Upon entering the site, I would like certain divs to animate from an offscreen position. I came across this code snippet: $( document ).ready(function() { $('.box-wrapper').each(function(index, element) { setTimeout(function(){ ...

Displaying the second div once the first div has loaded, then concealing the first div

Current Approach: There are two divs occupying the same space, with div1 set to display:block and div2 set to display:none When a tab is clicked, jQuery hides one div over a period of 2000ms and reveals the other div. Challenge: The goal is for the ...

What is the best way to organize divs in a grid layout that adapts to different screen sizes, similar to the style

Is there a way to align multiple elements of varying heights against the top of a container, similar to what is seen on Wolfram's homepage? I noticed that they used a lot of JavaScript and absolute positioning in their code, but I'm wondering if ...

Having trouble with the jQuery toggle functionality not working as expected

I'm implementing a function where a div should increase in height and opacity when clicked, and then revert back to its original state when clicked again. I used the toggle function for this purpose, but the issue is that the button disappears when th ...

Tips for enhancing a table with extra functionality in Angular 6

Hi there! I've created a table using Angular 6 and now I'm looking to enhance it with some extra functionality: Implement an overall table search feature Allow users to sort the table by columns Add a "Page x of n" option for pagination I woul ...

Customize bullet list icons to adjust in size based on content using css

In my CMS project, the CMS team has a special requirement regarding unordered and ordered lists. They want the size of the bullet list icon to adjust according to the text content within the list. The image below shows the default design of a list item: ...

Here, it is possible for the padding-right value to be equal to zero

I am relatively new to CSS and I am currently in the process of testing and experimenting to better understand how it works. For instance, in the code snippet provided, we see that the selector .box h3 has a padding: 6px 10px 4px 10px;. Therefore, the pad ...

Resize drop zone with drag and drop functionality

I am using jQuery for dragging and dropping elements, but I am facing an issue. When I resize the drop zone while starting to drag an item, it seems like the previous size of the drop zone is still being used as the space. Is there a way to fix this? ...

Issue with Material UI Menu positioning incorrectly when button is aligned to the right or left

When looking at the first image, it appears that the material-UI menu position is working correctly. However, when we change the button position to align-right, the menu position shifts slightly to the left, not perfectly aligning with the right side as in ...

Issue with Bootstrap 5 Card Header not extending to full width

I have implemented tables within cards using a CDN. While the table looks fine in wide widths, it does not resize to fill the entire width when squeezed. Below is the HTML code: <div class="card table-responsive"> <div clas ...

Loading screen featuring an outlined blueprint of design elements

Can anyone identify the technology or language used to create the preloader screen on websites like the one shown in this link? The elements have a sweeping shine effect while loading. This style of preloader screen can be seen on popular websites such as ...

A tutorial on implementing a "Back to Top" button that appears dynamically while scrolling in Angular

I've developed a scroll-to-top feature for my Angular project, but I'm facing an issue. The scroll icon appears immediately upon page load instead of only showing after the user scrolls down. Any ideas or suggestions on how to achieve this? Here ...

Discovering the proper method to reach the parent element in jQuery while within the click event

How can I use jQuery to access the parent element within a click event? $(document).ready(function () { $(".lv1 li").click(function (event) { this.parentElement.parentElement.textContent = this.textContent; $.ajax({ type: 'post&apo ...

Experiencing difficulties with JavaScript/jQuery when encountering the 'use strict' error

I have been working on a small function to change the background of an HTML file, but I keep getting the 'Missing 'use strict' statement' error message from JSLint despite trying multiple solutions. Can anyone suggest how to resolve th ...

After following the official guide, I successfully installed Tailwind CSS. However, I am facing issues with utilizing the `bg-black` className for styling the background,

Following the installation guide for Tailwind CSS, I ran the command npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p. Despite this, the background className (bg-black) is not working as expected. Here are the file paths: Directory ...

Rails pagination will only show the link to the current page

Is there a way to show only the current page link with the << Previous link hidden when on the first page and next >> link hidden when on the last page? It should look like this: On the first page: 1 | next >> On the last page (with 4 ...

How can I eliminate the border appearance from a textfield fieldset component in Material-UI?

I have been struggling to remove the border using CSS code I found on Stack Overflow, but unfortunately, the issue persists. If anyone could kindly assist me in fixing this problem, I would be extremely grateful. Can someone please advise me on the specif ...

The division is now appearing below the preceding division instead of following it

I am currently encountering an issue that I believe has a simple solution, but I am unable to find it. I have two divs in my code. The first div contains a blurred background image and content, which is working perfectly fine. However, the second div is n ...

Position a div to float in the top right corner without overlapping its sibling header

Is there a way to position a div in the top right corner of a section without overlapping the text of a h1? This is the HTML code: <section> <h1>some long long long long header, a whole line, 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6</h1> < ...

Layers of CSS images

Is there a way to create multiple layers for images on a webpage so they can move independently without affecting other elements? I attempted to use z-index, which did work, but it caused the page to increase in height. Additionally, when using jQuery.posi ...