in Education by
I've successfully floated labels above form input fields when focused, but I'm stumped on how to keep the labels floating if text is entered into the field and un-focused. There are tons of pure CSS examples online but none are clear to me what is keeping a filled field label floating. I'm sure it fairly easy but I'm fairly new to this. EDIT: I need to do this without using the "required" method mentioned in the comments below. My form is a combination of required and non-required fields. Here is one of many examples I've found: http://codepen.io/atunnecliffe/pen/gpKzQw Here is my jsfiddle: https://jsfiddle.net/yzzxrsnn/1/ Many thanks in advance. HTML:


CSS: .form-input { box-sizing: border-box; width: 100%; max-width: 400px; height: 55px; margin: 15px 0 15px 0; padding: 0px 10px 0px 10px; border: 3px solid #ccc; outline: none; border-radius: 8px; outline: none; resize: none; text-align:left; -webkit-appearance: none; background: transparent; font-family: 'Yanone Kaffeesatz', Arial, sans-serif; color: #000; background:#fff; font-size: 18px; -webkit-transition: border-color ease-in-out .5s,box-shadow ease-in-out .5s; transition: border-color ease-in-out .5s,box-shadow ease-in-out .5s; } .form-input:focus { border-color: #00bafa; } .form-input + .form-label { display: block; pointer-events: none; margin: 0 0 0 15px; line-height: 0px; margin-top: -43px; margin-bottom: 43px; transition: transform 1s; } .form-label { font-family: 'Yanone Kaffeesatz', Arial, sans-serif; color: #ccc; font-size: 18px; } .form-input:focus + .form-label { transform: translateY(-38px); font-family: 'Yanone Kaffeesatz', Arial, sans-serif; color: #00bafa; font-size: 18px; } .form-textarea { box-sizing: border-box; width: 100%; max-width: 400px; height: 150px; margin: 15px 0 15px 0; padding: 15px 10px 0px 10px; border: 3px solid #ccc; border-radius: 8px; outline: none; resize: none; text-align:left; -webkit-appearance: none; background: transparent; font-family: 'Yanone Kaffeesatz', Arial, sans-serif; color: #000; background:#fff; font-size: 18px; -webkit-transition: border-color ease-in-out .5s,box-shadow ease-in-out .5s; transition: border-color ease-in-out .5s,box-shadow ease-in-out .5s; } .form-textarea:focus { border-color: #00bafa; } .form-textarea + .form-label { display: block; pointer-events: none; margin: 0 0 0 15px; line-height: 0px; margin-top: -145px; margin-bottom: 145px; transition: transform 1s; } .form-textarea:focus + .form-label { transform: translateY(-30px); font-family: 'Yanone Kaffeesatz', Arial, sans-serif; color: #00bafa; font-size: 18px; } .form-textarea + .form-label:before { transform: translateY(-38px); pointer-events: none; font-family: 'Yanone Kaffeesatz', Arial, sans-serif; color: #00bafa; font-size: 18px; } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can do this by using jquery. Using jquery we can add class to a input and it's label if the value of that input is bigger than 0. $(document).ready(function() { var $input = $('.form-input'); $input.focusout(function() { if($(this).val().length > 0) { $(this).addClass('input-focus'); $(this).next('.form-label').addClass('input-focus-label'); } else { $(this).removeClass('input-focus'); $(this).next('.form-label').removeClass('input-focus-label'); } }); }); and some css .input-focus-label{ transform: translateY(-38px); font-family: 'Yanone Kaffeesatz', Arial, sans-serif; color: #00bafa; font-size: 18px; } .input-focus{ border-color: #00bafa; } Here is a example fiddle.Hope this helps you.

Related questions

0 votes
    I want to display the label of an input inside its input, so that when I click the input, the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    There is no way to have a tri-state check button (yes, no, null) in HTML, right? Are there any ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have created floating label in input field.In chrome view is working fine, but not properly viewed in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I gave this CSS class to some input fields .searchField { display: inline-block; } This is their ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    Kanika is a chartered accounted she looks after the accounts of various companies and keep their records in Microsoft ... the tasks. Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    b) List the things we should keep in mind while communicating orally. c) Raghav wants to update the changes in ... customise the TOC? Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    I'm using Zend Framework and I have a form input button with a style that I defined. I need to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    I'm using Zend Framework and I have a form input button with a style that I defined. I need to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have two integer values a and b, but I need their ratio in floating point. I know that a < b, and I want ... the following? c= a / b Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I have an XML file which is structured as follows: place1 location1 place2 location2 place3 location3 These ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I am writing down a simple schedule planner app using WinForms in Visual Basic .NET, and I have this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have two lists, same size, one is y_data and one is x_data x_data is a time hh:mm:ss during ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have used the following ggplot command: ggplot(survey, aes(x = age)) + stat_bin(aes(n = nrow(h3), y = ... but cannot figure out how. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I have a dataset with labels and datapoints, problem is that rather then a classification problem I want to ... quadratic estimation. Select the correct answer from above options...
asked Feb 3, 2022 in Education by JackTerrance
0 votes
    Scala is a pure functional programming language 1. True 2. False...
asked Mar 7, 2023 in Technology by JackTerrance
...