Preventing placeholder from reverting back on transition (CSS)

My goal was to create an input field with a transition effect on the placeholder text. To achieve this, I utilized a <span> tag to hold the placeholder and applied a transition using the :focus CSS selector. The implementation works smoothly, but there are a couple of issues that I encountered. After entering text and clicking outside the input area, the placeholder transitions back and overlaps with the entered text. I want to prevent the placeholder from transitioning back if the text area contains text. Additionally, there is an issue where clicking on the placeholder does not select the text field. I'm seeking assistance in resolving these bugs. Below is the code I used:

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Input</title>

  <style>
    body {
      margin: 0px;
    }
    
    #top-heading {
      padding: 1rem;
      text-align: center;
      margin: auto;
      margin-bottom: 1rem;
    }
    
    textarea {
      width: 85%;
      display: block;
      height: 10vh;
      margin: 1.5rem;
      margin-top: 0rem;
      padding: 0.5rem;
      font-size: large;
    }
    
    span {
      position: absolute;
      top: 6rem;
      padding-left: 2.7rem;
      color: rgb(121, 120, 120);
      font-size: larger;
    }
    
    textarea:focus~span {
      color: black;
      font-size: 1.091rem;
      transition: .6s ease;
      transform: translate3d(-1.15rem, -1.9rem, 2rem);
    }
  </style>
</head>

<body>
  <h1 id="top-heading">Input</h1>

  <textarea id="txt-input"></textarea>
  <span>Enter the input</span>
</body>

</html>

Answer №1

Could you please review the code provided below? It should be effective for your needs. Instead of applying the textarea:focus~span selector, try assigning the transition: .6s ease; property to the span tag directly.

For further reference, you can visit the following link: https://jsfiddle.net/yudizsolutions/kjfc0z37/

<!DOCTYPE html>
<html lang="en">

  <head>
    <title>Input</title>

    <style>
      body {
        margin: 0px;
      }

      #top-heading {
        padding: 1rem;
        text-align: center;
        margin: auto;
        margin-bottom: 1rem;
      }

      textarea {
        width: 85%;
        display: block;
        height: 10vh;
        margin: 1.5rem;
        margin-top: 0rem;
        padding: 0.5rem;
        font-size: large;

      }

      span {
        position: absolute;
        top: 6rem;
        padding-left: 2.7rem;
        color: rgb(121, 120, 120);
        font-size: larger;
        transition: .6s ease;
      }

      textarea:focus~span {
        color: black;
        font-size: 1.091rem;

        transform: translate3d(-1.15rem, -1.9rem, 2rem);
      }

    </style>
  </head>

  <body>
    <h1 id="top-heading">Input</h1>

    <textarea id="txt-input"></textarea>
    <span>Enter the input</span>
  </body>

</html>

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

Troubleshooting the Owl carousel's responsiveness with a div width of 1170px

Whenever my display size is less than 1170px, the width of the owl carousel div overflows. How can I fix this issue? jQuery(document).ready(function($) { "use strict"; // CUSTOMERS TESTIMONIALS CAROUSEL $('#customers-testimonials&a ...

Positioning a Three.js static CSS2DObject with respect to the camera's perspective

I am currently working on a unique program visualizer that utilizes Three.js to showcase various aspects of a program to the user. One crucial feature of this visualizer is allowing users to click on specific objects to view additional information about th ...

Troubleshooting: Navbar dropdown menu in AngularJS gets hidden within UI layout

After spending some time trying to understand why the NavBar drop-down is getting overlapped inside the ui-layout, I realize that I must be overlooking some basic steps. This issue seems so simple, yet it is eluding me. To see the details of the problem, ...

Ways to notify Google that this content is integrated into a different article

Following each article on my website, there are random previews for other articles. However, these previews are quite large, featuring a headline, subheadline, and six rows of text. This has caused confusion as Google sometimes mistakes them for being part ...

What is the best way to design a grid consisting of 14 divs that span the entire width of the browser screen, incorporating dividers to separate different groups, utilizing a combination of html,

I am interested in creating a layout with two rows of divs that span the entire width of the screen, with a total of 14 divs in each row. These divs are fixed in size and grouped by color, with dividers separating each color group. I am hoping to achieve ...

Alter the background color of a table cell in Angular HTML depending on a boolean value

Attempting to use Angular ng-class to set the background color of a table cell. I'm referencing code snippets from these resources: Angular: How to change the color of cell table if condition is true Change HTML table cell background color using ...

How come the picture extends beyond the borders of its parent container when I expand the width?

Despite my efforts, I haven't been able to figure this out successfully. My goal is to align the text elements next to the image just like in the example picture below. Initially, I achieved this using absolute positioning. absolute positioning Howe ...

Error with setting innerHTML property of a null nested div

This issue arises when the element is referenced before the DOM has completely loaded. To combat this, I have ensured that the necessary script runs only after the window has finished loading. Interestingly, if the getElementById() call is for a standalon ...

Checkboxes within Angular's reactive forms are a powerful tool for creating dynamic user

Currently, I am working on a contact form that includes checkboxes for users to select multiple options, with the requirement of selecting at least one box. My challenge lies in figuring out how to pass all the selected checkboxes' data to an API usin ...

Pictures piling up vertically instead of lining up horizontally

Is there a way to display these images in one row horizontally instead of on separate lines? The text under the images is causing them to wrap because of the width. How can I ensure that the title text has the same width as the image? <img src="image.p ...

JavaScript quiz featuring randomized questions with selectable radio buttons

I need assistance in creating a feature on my webpage where users can select the number of questions they want to answer (ranging from 1 to 10). The selected number of questions should then be displayed randomly, without any duplicates. I am not very famil ...

The JQuery chosen dropdown experiences a visual issue when placed inside a scrollbar, appearing to be "cut

Good day, I've been using the jQuery "chosen" plugin for a dropdown menu, but I encountered an issue with it being located in a scrollable area. The problem is that the dropdown items are getting cut off by the outer div element. I have provided a si ...

The hover effect is failing to impact a child element during a transition

I created a link with an arrow next to it that should move 20px to the right when hovered over, using a transition of 1s. However, for some reason the transition is not affecting the arrow. Can anyone please assist me in figuring out why this is happenin ...

The HTML button triggers a function to execute on a different webpage when clicked

I'm facing a straightforward issue that I can't seem to figure out due to my limited experience with Angular and web development. The problem revolves around two components, namely home and dashboard. In the home.component.html file, there's ...

Implementing personalized SVG icons for list items

Here is some HTML code along with SCSS that I need help with: <div class="risk"> <ul> <li><span>Aggressive</span>Lorem ipsum dolor ...</li> <li><span>Growth</span>doloremque autem...</li ...

How to truncate lengthy text in a table on mobile screens using Bootstrap 4

I am currently working on a responsive table that needs to display correctly on both desktop and mobile devices. However, I have run into an issue where long text in the table gets truncated on mobile devices, compromising the responsiveness of the design. ...

What enhancements does HTML5 provide for accessibility?

In what ways does HTML5 improve accessibility compared to HTML 4.01 or XHTML 1.0 Strict? ...

Issue encountered when toggling flip switch in Jquery Mobile

I am trying to create a calculator for my app using Jquery Mobile, but I am facing an issue with the flip toggle switch. Here is my script: <script type="text/javascript"> function dosis() { $peso = $('#peso').get(0).value $dosis = ...

When interacting with elements with the "user-select: none" property, WkWebView still allows text selection

When using an iOS iPad app with a single fullscreen WKWebView, an issue arises when long-pressing on an area with user-select:none - the first text in the next available area without user-select:none gets selected instead of being ignored. Is there a way t ...

Attempting to update the content of a specific div element on a webpage without disrupting its current position

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>ajax div example</title> <script type="text/javascript" src="jscripts/jquery-1.6.4.min.js" ...