How to move placeholder to top on focus AND while typing?

前端 未结 7 1221
野的像风
野的像风 2020-11-29 17:38

I want the placeholder to move to the top when the textbox is on focus and also while the user is typing.

I\'m not sure if this is just html/css or any javascript to

相关标签:
7条回答
  • 2020-11-29 17:46

    You can try the below code. It only uses HTML and CSS and does not reply on Javascript or component libraries:

    .text-field {
            position: relative;
            margin: 10px 2.5px 20px 2.5px;
        }
    
    input {
            display: inline-block;
            border: thin solid #fafafa;
            border-bottom: solid medium #999;
            color: #444;
            background-color: #fafafa;
            padding: 10px 10px 10px 10px;
            border-top-left-radius: 10px;
            border-top-right-radius: 10px;
        }
    
        input:focus {
                border: thin solid #32cd32;
                border-bottom: solid medium #32cd32;
                background-color:#fff;
            }
    
    label {
            color: #999;
            position: absolute;
            pointer-events: none;
            left: 10px;
            top: 10px;
            transition: 0.2s;
        }
    
    input:focus ~ label, input:valid ~ label {
            top: -10px;
            left: 15px;
            font-size: small;
            color: #32cd32;
            background-color:#fff;
            padding:0 5px 0 5px;
        }
    <div class="text-field">
                    <input type="text" required>
                    <label>Input field 1</label>
                </div>
    
                <div class="text-field">
                    <input type="text" required>
                    <label>Input field 2</label>
                </div>

    0 讨论(0)
  • 2020-11-29 17:48

    .user-input-wrp {
    	position: relative;
    	width: 50%;
    }
    .user-input-wrp .inputText{
    	width: 100%;
    	outline: none;
    	border:none;
    	border-bottom: 1px solid #777;
     	box-shadow: none !important;
    }
    .user-input-wrp .inputText:focus{
    	border-color: blue;
    	border-width: medium medium 2px;
    }
    .user-input-wrp .floating-label {
    	position: absolute;
    	pointer-events: none;
    	top: 18px;
    	left: 10px;
    	transition: 0.2s ease all;
    }
    .user-input-wrp input:focus ~ .floating-label,
    .user-input-wrp input:not(:focus):valid ~ .floating-label{
    	top: 0px;
    	left: 10px;
    	font-size: 13px;
    	opacity: 1;
    }
    <h1>The floating label</h1>
    <div class="user-input-wrp">
      <br/>
      <input type="text" class="inputText" required/>
      <span class="floating-label">Your email address</span>
    </div>

    Modified the code from @user1846747 a little.

    0 讨论(0)
  • 2020-11-29 17:52

    span{
      display:block;
      }
    input:focus::-webkit-input-placeholder { color:transparent; }
    input:focus:-moz-placeholder { color:transparent; } /* FF 4-18 */
    input:focus::-moz-placeholder { color:transparent; } /* FF 19+ */
    input:focus:-ms-input-placeholder { color:transparent; } /* IE 10+ */
    <script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script>
    $(document).ready(function() {
      $('input').focus(function(){
        placeholder = $(this).attr('placeholder');
        if(placeholder != undefined){
          $(this).parent().prepend('<span class="input-placeholder">'+placeholder+'</span>');
        }
      });
      $('input').blur(function(){
        $(this).parent().find('.input-placeholder').remove();
      });
    });
    </script>
    <div>
      <input type="text" class="inputText" placeholder="Email adress" required/>
    </div>

    0 讨论(0)
  • 2020-11-29 18:04

    Only using HTML and css

    .searchformfld{
        position: relative;
        margin: 5px 0px;
    }
    .searchformfld label{
        position: absolute;
        padding-left: 10px;
        top:15px;
        cursor: text;
    
    }
    .searchformfld input:focus + label,.searchformfld input:not(:placeholder-shown) + label{
        opacity:1;
        transform: scale(.9) translateY(-100%) translateX(-10px);
        color:#000;
    }
    .searchformfld input:focus{
        border:1px solid #000;
        outline-color: #000;
    }
    .searchformfld{
        padding: 15px;
        margin:15px 0px;
    }
    .searchformfld input{
        width:100%;
        padding-left: 10px;
    }
    .searchformfld label,.searchformfld input{
        transition: all 0.2s;
        transition-timing-function: ease;
        transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
        opacity:0.5;
    }
    <div class="searchformfld">
                <input type="text" class="candidateName" id="candidateName" name="candidateName" placeholder=" "/>
                <label for="candidateName">Candidate name</label>
            </div>

    0 讨论(0)
  • 2020-11-29 18:06

    That site isn't moving the placeholder, but placing a div (.floating-label) over the input, so when the input is focused the div just animates to be over the input. The key part here is using pointer-events: none; in the floating div, so when you click it the event goes through it to the input box behind it.

    0 讨论(0)
  • 2020-11-29 18:10

    if your Floating label is not working when required attribute is removed, try this: using input:not(:placeholder-shown)

    input:focus ~ .floating-label,
    input:not(:placeholder-shown) ~ .floating-label{
      top: 8px;
      bottom: 10px;
      left: 20px;
      font-size: 11px;
      opacity: 1;
    }
    
    .inputText {
      font-size: 14px;
      width: 200px;
      height: 35px;
    }
    
    .floating-label {
      position: absolute;
      pointer-events: none;
      left: 20px;
      top: 18px;
      transition: 0.2s ease all;
    }
    <div>
      <input placeholder="" type="text" class="inputText" />
      <span class="floating-label">Your email address</span>
    </div>

    0 讨论(0)
提交回复
热议问题