skip to Main Content

I am trying to achieve a floating placeholder functionality. Everything works fine with the code I have written except for the fact that when there is an email field and I try to add a wrong entry which is not of an email format and i shift focus from the input box, the floating text still appears. You can check the codepen link

Belew is my code:-

.-input-container {
    max-width: 540px;
    padding-bottom: 30px;
    margin-top: 30px;
}

input.-textbox {
    width: 100%;
    border-radius: 40px;
    background-color: #f5f5f5;
    border: 1px solid #dddddd;
    padding: 16px 30px;
    font-size: 16px;
    color: #555555;
    font-family: OpenSans;
    font-weight: normal;
    font-style: normal;
    font-stretch: normal;
    line-height: 1.88;
    letter-spacing: normal;
    text-align: left;
}

input.-textbox:focus{
    outline: none;
    box-shadow: none
}

input.-textbox:focus ~ .floating-label,
input.-textbox:not(:focus):valid ~ .floating-label {
  top: 8px;
  bottom: 10px;
  left: 45px;
  font-size: 10px;
  opacity: 1;
  line-height: 2.1;
  font-family: OpenSans;
}

input.-textbox ~ .floating-label {
  position: absolute;
  pointer-events: none;
  left: 45px;
  top: 18px;
  transition: 0.2s ease all;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.css" rel="stylesheet"/>
<div class="-input-container col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 ">
  <input type="email" class="-textbox form-control" required="">
  <span class="floating-label">E-postadress</span>
</div>

3

Answers


  1. Try this:

    .-input-container {
        max-width: 540px;
        padding-bottom: 30px;
        margin-top: 30px;
    }
    
    input.-textbox {
        width: 100%;
        border-radius: 40px;
        background-color: #f5f5f5;
        border: 1px solid #dddddd;
        padding: 16px 30px;
        font-size: 16px;
        color: #555555;
        font-family: OpenSans;
        font-weight: normal;
        font-style: normal;
        font-stretch: normal;
        line-height: 1.88;
        letter-spacing: normal;
        text-align: left;
        &:focus{
          outline: none;
          box-shadow: none
        }
    }
    <div class="-input-container col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 ">
      <input type="email" class="-textbox form-control" placeholder="E-postadress" required="">
    </div>
    Login or Signup to reply.
  2. Use javascript in it. Try this

    $('input').on('focusin', function() {
      $(this).parent().find('span').addClass('test2');
    });
    
    $('input').on('focusout', function() {
      if (!this.value) {
        $(this).parent().find('span').removeClass('test2');
      }
    });
    .-input-container {
      max-width: 540px;
      padding-bottom: 30px;
      margin-top: 30px;
    }
    
    input.-textbox {
      width: 100%;
      border-radius: 40px;
      background-color: #f5f5f5;
      border: 1px solid #dddddd;
      padding: 16px 30px;
      font-size: 16px;
      color: #555555;
      font-family: OpenSans;
      font-weight: normal;
      font-style: normal;
      font-stretch: normal;
      line-height: 1.88;
      letter-spacing: normal;
      text-align: left;
    }
    
    input.-textbox:focus {
      outline: none;
      box-shadow: none
    }
    
    input.-textbox:focus~.floating-label,
    input.-textbox:not(:focus):valid~.floating-label, {
      top: 18px;
      bottom: 10px;
      left: 45px;
      font-size: 10px;
      opacity: 1;
      line-height: 2.1;
      font-family: OpenSans;
    }
    
    input.-textbox~.floating-label {
      position: absolute;
      pointer-events: none;
      left: 45px;
      top: 18px;
      transition: 0.2s ease all;
    }
    .test2{
     margin-top:-20px;
     line-height:2.1;
     font-size:10px;
      font-family: OpenSans;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.css" rel="stylesheet" />
    <div class="-input-container col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 ">
      <input type="email" class="-textbox form-control" required="" id=spa>
      <span class="floating-label">E-postadress</span>
    </div>
    Login or Signup to reply.
  3. It is simple with some Javascript and minor changes in styles. Try this one.

    $(document).ready(function () {
        $('.-textbox').click(function (){
            $(this).siblings('span').addClass('active');
        });
        $('.-textbox').blur(function(){
            if($('.-textbox').val()=== ''){
                $(this).siblings('span').removeClass('active');
            }
        });
    });
    .-input-container {
        max-width: 540px;
        padding-bottom: 30px;
        margin-top: 30px;
        position: relative;
    }
    
    input.-textbox {
        width: 100%;
        border-radius: 40px;
        background-color: #f5f5f5;
        border: 1px solid #dddddd;
        padding: 16px 30px;
        font-size: 16px;
        color: #555555;
        font-family: OpenSans;
        font-weight: normal;
        font-style: normal;
        font-stretch: normal;
        line-height: 1.88;
        letter-spacing: normal;
        text-align: left;
        &:focus{
            outline: none;
            box-shadow: none
        }
    }
    input.-textbox ~ .floating-label {
        position: absolute;
        pointer-events: none;
        left: 45px;
        top: 18px;
        transition: 0.2s ease all;
    }
    input.-textbox ~ .floating-label.active{
        top: 8px;
        bottom: 10px;
        left: 45px;
        font-size: 10px;
        opacity: 1;
        line-height: 2.1;
        font-family: OpenSans;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="-input-container col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 ">
        <input type="email" class="-textbox form-control" required="">
        <span class="floating-label">E-postadress</span>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search