Text overlapping when input is selected

Whenever I input text, it ends up overlapping with the placeholder text once the input is in place.

https://i.sstatic.net/zoYUS.png

This is the HTML code for my Form:

<form action="./includes/login.inc.php" method="post">
    <div class="group">
        <input type="text" name="emailuid"/><span class="highlight"></span><span class="bar"></span>
        <label>Email</label>
    </div>
    <div class="group">
        <input type="password" name="pwduid"/><span class="highlight"></span><span class="bar"></span>
        <label>Password</label>
    </div>
    <div class="btn-box">
        <button class="btn btn-submit" type="submit" name="login-submit">Sign in</button>
    </div>
</form>

Here is the stylesheet specific to this form:

form {
  width: 320px;
  margin: 45px auto;

}
form h1 {
  font-size: 3em;
  font-weight: 300;
  text-align: center;
  color: #2196F3;
}
form h5 {
  text-align: center;
  text-transform: uppercase;
  color: #c6c6c6;
}
form hr.sep {
  background: #2196F3;
  box-shadow: none;
  border: none;
  height: 2px;
  width: 25%;
  margin: 0px auto 45px auto;
}
...

I am aiming for the bottom "Password" field look when you deselect from the box. The text should move back down to the bottom and overlap the input as you are deselecting.

Your assistance is greatly appreciated.

Answer №1

To implement this functionality using jQuery, utilize the .focus() and .blur() functions. Begin by establishing a class with the style for your label when it is focused. Upon input focus, check if it contains a value. If so, retain the focused class; otherwise, remove it. Refer to the code snippet below:

css

label.focused {
    top: -14px;
  font-size: 12px;
  color: #FF3B3F;
}

jQuery

$('input').focus(function() {
  $(this).siblings('label').addClass('focused');
}).blur(function () {
  if($(this).val().length == 0) {
    $(this).siblings('label').removeClass('focused');
  }
});

Full code

$('input').focus(function() {
  $(this).siblings('label').addClass('focused');
}).blur(function () {
  if($(this).val().length == 0) {
    $(this).siblings('label').removeClass('focused');
  }
});
form {
  width: 320px;
  margin: 45px auto;

}
form h1 {
  font-size: 3em;
  font-weight: 300;
  text-align: center;
  color: #2196F3;
}
form h5 {
  text-align: center;
  text-transform: uppercase;
  color: #c6c6c6;
}
form hr.sep {
  background: #2196F3;
  box-shadow: none;
  border: none;
  height: 2px;
  width: 25%;
  margin: 0px auto 45px auto;
}
form .emoji {
  font-size: 1.2em;
}

.group {
  position: relative;
  margin: 45px 0;
}

textarea {
  resize: none;
}

input,
textarea {
  background: none;
  color: #4F4F4F;
  font-size: 18px;
  padding: 10px 10px 10px 5px;
  display: block;
  width: 320px;
  border: none;
  border-radius: 0;
  border-bottom: 1px solid #4F4F4F;
}
input:focus,
textarea:focus {
  outline: none;
}
/*input:focus ~ label,
textarea:focus ~ label {
  top: -14px;
  font-size: 12px;
  color: #FF3B3F;
}*/

label.focused {
    top: -14px;
  font-size: 12px;
  color: #FF3B3F;
}

input:focus ~ .bar:before,
textarea:focus ~ .bar:before {
  width: 335px;
}

input[type="password"] {
  letter-spacing: 0.3em;
}

label {
  color: #4F4F4F;
  font-size: 16px;
  font-weight: normal;
  position: absolute;
  pointer-events: none;
  left: 5px;
  top: 10px;
  -webkit-transition: 300ms ease all;
  transition: 300ms ease all;
}

.bar {
  position: relative;
  display: block;
  width: 320px;
}
.bar:before {
  content: '';
  height: 2px;
  width: 0;
  bottom: 0px;
  position: absolute;
  background: #FF3B3F;
  -webkit-transition: 300ms ease all;
  transition: 300ms ease all;
  left: 0%;
}

.btn {
  background: #fff;
  color: #959595;
  border: none;
  padding: 10px 20px;
  border-radius: 3px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  text-decoration: none;
  outline: none;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
  -webkit-transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}
.btn:hover {
  color: #752021;
  box-shadow: 0 7px 14px rgba(0, 0, 0, 0.18), 0 5px 5px rgba(0, 0, 0, 0.12);
}
.btn.btn-link {
  background: #FF3B3F;
  color: #d3eafd;
}
.btn.btn-link:hover {
  background: #0d8aee;
  color: #deeffd;
}
.btn.btn-submit {
  background: #FF3B3F;
  color: white;
}
.btn.btn-submit:hover {
  background: #752021;
  color: white;
}
.btn.btn-cancel {
  background: #eee;
}
.btn.btn-cancel:hover {
  background: #e1e1e1;
  color: #8b8b8b;
}

.btn-box {
  text-align: center;
  margin: 50px 0;
}
<form action="./includes/login.inc.php" method="post">
    <div class="group">
        <input type="text" name="emailuid"/><span class="highlight"></span><span class="bar"></span>
        <label>Email</label>
    </div>
    <div class="group">
        <input type="password" name="pwduid"/><span class="highlight"></span><span class="bar"></span>
        <label>Password</label>
    </div>
    <div class="btn-box">
        <button class="btn btn-submit" type="submit" name="login-submit">Sign in</button>
    </div>
</form>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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

Samsung mini devices experiencing issues displaying Unicode symbol (25be)

I'm currently facing an issue with an iconfont that I have included in my website. I am using Unicode symbols for the characters of the icons, and most of them are displaying correctly. However, there are a couple that are showing up as blank on the s ...

"Exciting developments in Angular 17 with the introduction of the new @

I need to output elements from an array of strings starting at index 1. arr = [ "str1", "str2", "str3", "str4", "str5" ] The desired output is: str2 str3 str4 str5 To achieve this, use a new @for loop in ...

Troubleshooting IE7's overflow problem

In my website, I have a ul element with overflow-y set to auto for displaying a nice scroll bar with all the li elements. However, in IE7, this setting is ignored and the list overflows outside the container. To see the issue directly, you can visit http: ...

transferring background-image in html between elements

Imagine having a three-level hierarchy HTML code structured like this: <section> <div id="outer"> <div id="inner"> </div> </div> </section> If we set background-image:someImage.jpg to the section, and backg ...

Retrieve the HTML code from a file and store it in a JavaScript variable

My PhoneGap application is able to load an external page in the inAppBrowser and inject a bottom bar onto that page using executeScript(). This allows me to have a bar with basic controls on the specific page. I have successfully implemented this functiona ...

I am having trouble getting this code to display divs in two separate columns. How can I go about achieving that?

Having trouble getting the divs to split into columns properly. Not sure why it's not working? For example, col-lg-8 and col-lg-4 should be splitting into two for larger screens. <!DOCTYPE html> <html> <head> <link href="h ...

Arrange objects to have identical beginning points

Here is how my html code is structured: <div class="text-center"> (bootstrap class) <p>Username</p> <p>Name:</p> <p>Last name:</p> <p>Email:</p> </div> Link to jsfiddle I am attempting ...

The Carousel Slider seems to encounter issues when trying to implement it with *ngFor in Angular 8

Currently tackling an issue in Angular 8 where my slider functions perfectly with static data. However, upon attempting to loop through some dynamic data, the 'left' and 'right' buttons on the carousel cease to work. The images also fai ...

Ensuring Your IMG is Perfectly Centered in DIV Across all Browsers

I have tried all the tips from the top Google results, but I am still struggling to center an image within a div. For example, the popular tricks mentioned in this link: or http://www.w3.org/Style/Examples/007/center.en.html do not seem to work in IE 8. ...

Challenges arise when transferring data retrieved from a table into a bootstrap modal window

I have been attempting to transfer values from a table into a modal. Initially, I successfully displayed the <td> values in an alert when a button was clicked on a specific row. Now, I am aiming to take it a step further by having these <td> va ...

What is causing the lack of HTML formatting in the emails being sent by my PHP contact form?

As I work on creating my custom contact form, I've encountered an issue with the emails not appearing in HTML format during testing. It's puzzling as other sources seem to send HTML formatted emails to my email service provider without any proble ...

Retrieve data from an SQL database and populate an HTML dropdown menu in a web page using PHP

I am a beginner in the world of JavaScript and facing some challenges. I am working with PHP 7 and attempting to retrieve data from an SQL database, specifically a table. My goal is to extract a single column from this table and display it in a dropdown me ...

Transformation of HTML occurs in React components upon saving in Visual Studio Code

As I embarked on my journey to learn React, I hit a roadblock early on. Searching for a particular setting, I somehow ended up selecting something different, like NodeJS. Now, the issue at hand is: Behold the First Image: https://i.sstatic.net/ETmDk.png ...

What are the steps to adjust the width of an HTML table cell?

Here is the code snippet I am currently working with: $(document).ready(function () { $(".stripeMe tr").mouseover(function () { $(this).addClass("over"); }).mouseout(function () { $(this).removeClass("over"); }); $(".stripe ...

Creating a modal popup when a button is clicked

After searching online for a way to make my sign up modal pop up when the signup button in my navbar is pressed, I was unable to find any information on how to achieve this. I suspect it involves JavaScript, but my knowledge of JS is limited. I attempted ...

Tips for altering the text color of the highlighted row in a Material UI table

Trying to modify the color of the row text and the background color when selected. I have managed to adjust the background color, but I am encountering difficulty changing the text color. <TableRow className={classes.tableBody} > tab ...

Issues with font compatibility across different domains

Apologies for the lengthy post, but some background information is necessary before I can pose my question as the situation is quite convoluted. I am faced with the challenge of incorporating two different fonts into a newsletter. The first font, Cyclone ...

Struggling with sending mail using javascript and ajax?

Having trouble sending emails through my website using AJAX. It seems like the data is not being utilized or sent properly. I managed to get it working some time ago, but haven't checked on it since and now it has stopped functioning for some unknown ...

Using inline JavaScript to style an element when clicked

Looking for assistance with styling a link. I've connected an external stylesheet to my HTML file, so that part is all set. The goal is to modify the text-decoration when the onclick event occurs. Here's what I currently have: <a class="dopel ...

I am encountering issues with certain sections of my HTML and PHP coding

There was a PHP Parse error: syntax error, unexpected ',' found in the index.php file on line 20 Here is the code snippet that caused the error: <?php $title = "Home"; $page = "index"; $return = TRUE; require( "./configuration.php" ); includ ...