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

前端 未结 7 1257
野的像风
野的像风 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;
        }

提交回复
热议问题