The floating label failed to appear when the input was focused

After searching online, I came across an example of how to create a floating label. I followed the instructions exactly as provided in the example, but when I tested it on my website, it did not work the way it was supposed to. The label would disappear whenever the cursor was focused on the input field. However, it worked correctly when the label was placed outside of the input field. Am I missing something?

Here are the resources I used:

Tutorial

JSFiddle Example

Below is the code snippet:

Index.cshtml

<div class="container-fluid">
    <div class="container">
        <div class="row">
            <div class="form-group input-group col-xs-12 col-sm-4 col-md-4 col-lg-4">
                <span class="input-group-addon"><i class="fa fa-user"></i></span>
                <label class="control-label" for="txtUsername">Username</label>
                <input id="txtUsername" class="form-control" type="text" />
            </div>
            <div class="form-group input-group col-xs-12 col-sm-4 col-md-4 col-lg-4">
                <span class="input-group-addon"><i class="fa fa-key"></i></span>
                <label class="control-label" for="txtPassword">Password</label>
                <input id="txtPassword" class="form-control" type="password" />
            </div>
            <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
                <button id="showOverlay" class="btn btn-default form-control" data-toggle="popover" data-content="Submit">Log In</button>
            </div>
        </div>
    </div>
</div>

Site.css

.control-label {
    font-size: 14px;
    font-weight: 400;
    opacity: .6;
    pointer-events: none;
    position: absolute;
    transform: translate3d(-340px, 8px, 0) scale(1);
    transform-origin: left top;
    transition: 300ms;
    z-index: 3;
    color: black;
}

.form-group.focused .control-label {
    transform: translate3d(-340px, 0, 0) scale(0.75);
}

.form-control {
    align-self: flex-end;
}

Site.js

$('.form-control').on('focus blur', function (e) {
    $(this).parents('.form-group').toggleClass('focused', (e.type === 'focus' || this.value.length > 0));
}).trigger('blur');

Your help will be greatly appreciated.

Thank you.

Answer №1

If you're wondering why it was not working, the reason is because I had forgotten to include the z-index in the CSS for when the control is focused:

Here is the corrected CSS (I'm only showing the solution):

.form-group.focused .control-label {
    z-index: 5;
    opacity: 1;
    transform: translate3d(-340px, 0, 0) scale(0.75);
}

Appreciate your understanding.

Answer №2

I have resolved the issue within the CSS code. Please review the changes made!

.control-label {
    font-size: 14px;
    font-weight: 400;
    opacity: 0.4;
    pointer-events: none;
    position: absolute;
    transform: translate3d(10px, 7px, 0) scale(1);
    transform-origin: left top;
    transition: 300ms;
    z-index: 3;
    color: black;
}

.form-group.focused .control-label {
    opacity: 1;
    transform: translate3d(-100px, 10px, 0) scale(0.75);
}

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

event handling for multiple dropdown lists using jQuery and Ajax

I have a situation where I am using two drop down menus to filter some results. When the user changes the option in the first drop down, it should dynamically update the choices in the second one. I managed to replace the options in the second drop down, b ...

From JSON to HTML: the beauty of nesting

I believe there might be a foolish error lurking in my code, waiting to be uncovered. I am working with JSON data sourced from an external site at . The goal here is to extract specific data elements, such as stationShortCode, type, commercialTrack, and s ...

Different methods to incorporate Glyphicons without relying on Bootstrap CSS

For a recent client project, I utilized basic HTML and CSS. However, the client expressed a desire for Glyphicons to be included in the website menus. Attempting to incorporate Glyphicons with Bootstrap CSS posed challenges as it affected other aspects of ...

Disabling the global ajax datatype causes issues with Rails remote links

I have successfully developed a rails application that submits a form remotely and then displays form validation errors if the validation process fails. The form itself is working perfectly. However, I've come across an issue where the way I've ...

Persist user input even after reloading the page

In order to enhance the user experience, I would like to implement a feature where the data entered by the user is preserved even if they refresh, reload, or close the page. This includes retaining the selections made in the select elements. Additionally, ...

Struggling with using the $.get method in JavaScript

As a JavaScript beginner, I've encountered an odd issue while working on a weather application. I initially managed to fetch weather data from this source using latitude and longitude values. However, after making some code modifications for additiona ...

JS Plugin Loading Problem

What combination of libraries/frameworks would work best for optimizing performance in an HTML5/CSS3/JS app with moving elements? I have done some research, but I am venturing into unfamiliar territory when it comes to performance. Are there key principles ...

What events occur before they are detectable in JavaScript?

Below is a code snippet for handling events related to an image: HTML <!DOCTYPE html> <html> <head> <title>Image Event Handling</title> </head> <body> <img src="someImagePath.jpg" class="img-1"/> &l ...

I'm having trouble getting my dropdown content to align properly with my centered search bar. Any suggestions on how to fix this issue?

How can I align the dropdown content to the center in its td element? I haven't included all of my code here, so feel free to ask if you need more information. The search bar is supposed to be at the center of this table navbar. This is the HTML: &l ...

Excessive space being taken up by the final character in the Material-UI Autocomplete arrow endAdornment

<Autocomplete {...defaultProps} id="disable-close-on-select" disableCloseOnSelect renderInput={(params) => ( <TextField {...params} label="disableCloseOnSelect" variant="standard" /> )} /> Click h ...

Tips for making content vanish or remain concealed behind a see-through header during page scrolling

I made a JavaScript fiddle http://jsfiddle.net/claireC/8SUmn/ showcasing a fixed transparent header. As I scroll, the text scrolls up behind it. How can I make the text disappear or be hidden behind the transparent div when scrolling? Edit: I should also ...

Guide to making the top margin of Chinese characters consistent on all web browsers

Currently, I am working on a website that features flashcards for Chinese character learning. The issue I am facing is that Internet Explorer displays fonts slightly higher on the line compared to other browsers I have tested. Although this may seem insign ...

When reopening the modal in Bootstrap V5 with jQuery, the close event may not trigger as expected

When trying to implement special functionality in my modal close event using sweetalert2, I encountered an issue where the close event does not trigger again after closing the modal for the first time. Check out a live example here: https://codepen.io/th ...

Adjust the size of the flex item based on the background image dimensions

Within a flexbox, there are flex items displayed Each flex item should appear as a clickable folder with specific text inside It seems that the only CSS way to achieve this is by using a background image. However, I want to accomplish the following: Cons ...

Issue with resizing elements using jQuery

I am currently working on a basic animation that will only trigger if the screen width is greater than 800px. If the width is less than 800px, the animation should not occur and the default CSS should be retained. However, I am facing some issues: When ...

Display each table within a modal that is triggered by a separate table loop

I have a modal that includes a table. The modal is loaded from a looped table, and despite my confident code, the display seems off. When I run the code, this odd result occurs. Click the link to view the image. <tbody> <?php ...

Tips for removing a DOM element in Selenium using Java

Recently, I've been attempting to remove an element from a website using Selenium and Java with the xpath of the element readily available. WebElement m = driver.findElement (By.xpath ("//*[contains(text(),'discord.gg/')]")); The specific e ...

Using JQuery to swap out all URLs on a webpage?

Seeking guidance and examples on replacing all the links in the head of a document using jQuery. I am currently working with an app that requires my site to have SSL. To address this requirement without purchasing an SSL certificate or a static IP, I am a ...

Utilizing radio buttons for displaying or hiding a div element, along with implementing validation using jqueryvalidation.org

I am currently working on a functionality where I need to toggle the visibility of specific divs based on user input. Additionally, I only want to validate the fields within the visible div and not the hidden ones. If the user selects "Yes" in the radi ...

Contrasting date and time components within a JQuery date time picker

Utilizing Jquery, I have implemented two date time pickers. Upon clicking the show button, I require the difference between the two dates to be displayed in the textbox identified as result. For example: Time1 = 04/30/2014 10:27 am Time2 = 05/01/2014 1 ...