Trigger CSS animation for a single iteration

In my CSS snippet, I have implemented an animation to move some label text when a user selects an input box.

input:focus ~ label, input.used ~ label {
    top: -20px;
    transform: scale(.75); left: -2px;
    color: #4a89dc;
}

While this animation works well, I am encountering an issue where the label text animates back into place and overlaps the old text when I select a different input.

I am looking for a solution where the label text either disappears completely or remains in its new position without returning to its inactive state. In other words, it should not revert to its original position.

Answer №1

To solve this issue, simply assign a class to the element and then apply the desired changes to that class.

For instance, you could utilize jQuery in the following manner:

$('label').on('focus',function(){
    $(this).addClass('newClass');
});

By adding the class 'newClass' to the element, you can manipulate it as needed:

.newClass {
    left: 10px;
}

This will shift the element by 10 pixels to the left. Alternatively, you can achieve the same result using vanilla JavaScript:

document.getElementById('label').addEventListener('focus', function () {
 if (this.classList.contains('item')) {
     this.classList.add('item-off');
  } 
});

Make sure to add the 'item' class to your label before implementing this functionality.

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

Arrange all absolute divs equidistant from each other

I'm struggling with positioning multiple absolute divs inside a relative container using the CSS 'left' property. I want these inner divs to be evenly spaced regardless of how many there are, without setting fixed values for each. Currently ...

Inheriting the viewBox attribute with SvgIcon

I have a collection of unique custom SVGs, each with its own distinct viewBox. First Custom SVG <svg viewBox="-4 -4 24 24"><path something/></svg> Second Custom SVG <svg viewBox="-5 -7 24 24"><path something ...

Keep the footer fixed at the bottom until the document height exceeds the viewport height

UPDATE Originally flagged as a duplicate, I mistakenly labeled it before actually testing the solution in the linked question. The provided answer did not meet my specific requirement of having a sticky footer that only floats to the bottom of the page wh ...

PHP - Truncated html string when appending

I have a situation where I am using PHP to generate HTML for the MPDF library in order to create a PDF file. However, when I include a loop within my PHP-generated HTML, it seems to be cutting off the initial part of the string. Below is the code snippet: ...

Talwind Flexbox Grid Design

Currently, I am exploring the world of website layout creation using Tailwind Flexbox Grids as I believe it can add significant value. However, I have encountered some queries: How does one go about constructing a layout like this? Referencing this speci ...

Steps to implement the selection of multiple items from a drop-down or list on a React.js website

Hi there, I am a newcomer to the world of React JS web development. I am looking for guidance on how to implement a feature where users can select multiple cities from a drop-down or list, similar to the examples shown in image1. Once the user has made th ...

Is there a way to remove a specific row from both an HTML table and a MySQL database using CodeIgniter?

Just diving into CodeIgniter and successfully retrieved data from MySQL to display in an HTML table. Now, the challenge is to delete specific rows in MySQL. Imagine a scenario where there are 10 rows in the table, each accompanied by a checkbox, and a Del ...

Expanding to a full width of 100%, images on desktop screens are displayed larger than their original

I am facing a challenge with my three images lined up in a row, each with a width of 323px. In order to make my website responsive, I decided to switch from using width: 323px; to width: 100%;. While this adjustment worked well on mobile devices, I encoun ...

Differences in behavior of multiple select with track by in Angular versions above 1.4.x

I recently upgraded my product from Angular 1.2.x to 1.4.x. Since updating to angularjs 1.4.x, I've encountered an issue: What I have: I included code snippets for both angular 1.2.0 and 1.4.8. You can check out the comparison on JSFIDDLE. Explanat ...

Incorporate 3 additional compound filters with shuffle.js

Is there a way to add a third compound filter to the existing shuffle.js code provided below? // ES7 will have Array.prototype.includes. function arrayIncludes(array, value) { return array.indexOf(value) !== -1; } // Convert an array-like object to a r ...

What are some techniques for utilizing CSS with form.ImageField in Django?

When attempting to apply CSS to form.ImageField in Django similar to CharField, I encountered an error. File "/Users/hogehoge/Desktop/hoge/hoge/forms.py", line 42, in PostForm photo = forms.ImageField(label='Image', validators=[fi ...

Error: html2canvas encountered an uncaught undefined promise

Currently, I am working with an HTML canvas tag: <canvas id="myCanvas"></canvas> I have successfully created a drawing on this canvas, and it looks exactly how I wanted it to. However, when trying to convert it to a PNG file using html2canvas ...

Triggering a d3.js event when two radio buttons are both selected

I am currently working with a sample code that displays data based on the selected radio button: http://bl.ocks.org/juan-cb/1984c7f2b446fffeedde To enhance this functionality, I decided to add two sets of radio buttons instead of one by including the fol ...

Bootstrap-select has a consistent behavior of opening downwards every time

I am currently using Bootstrap-select to showcase my dropdown menus. However, I have noticed that when there is not enough space below, the dropdowns are displayed upwards. Here is how they appear: https://i.sstatic.net/kgeC9.jpg Instead of this behavior ...

The issue with the Hellip symbol not functioning correctly persists within the URL location

I'm having trouble with the address provided in my ePub. It's telling me that ... (ellipsis) is not valid: http://www.connectingforhealth.nhs.uk/…/infogov/…/infogovleaflet.pdf Is there a way to properly include this URL? ...

Troubleshooting Flexbox in Internet Explorer 11: Issue with 100% width not functioning properly within nested flex containers with a 'column'

Is there a way to ensure that an image within nested flex containers has a width of 100% in IE11, even when the container has flex-direction: column? I've tried setting the width to 100% with a max-width of calc(100% - 0.1px), but unfortunately it doe ...

Creating an image and video slider using bootstrap techniques

Currently, I am attempting to create an image and video slider using bootstrap. However, the slider I have created is not functioning as expected. For instance, the slider is not pausing for videos, and the videos are not autoplaying. I attempted to sear ...

javascript Incorrectly using location.reload will not refresh the page as intended

My goal is to refresh a page every time a user clicks a button so the page reverts back to its original source code. However, I am facing an issue where the location.reload() function is being executed after the code instead of at the beginning. btn.addEve ...

What is the process for choosing a table column by clicking on the "select" option?

Welcome to my project! Here is an example table that I'm working on. I have a question - how can I make it so that when I click on "choose me", the entire column is selected? Can you help me with this? <table> <tr> <th>plan A&l ...

Angular 7 routing glitches causing page refresh issues

Here is the issue I'm facing: I am looking for a way to switch between tabs in my navigation bar without having to refresh the entire website. I have just started working with Angular and I believe that the router should be able to route to a new pag ...