Pure CSS to achieve input box placeholder animation and input verification

Pure CSS to achieve input box placeholder animation and input verification

For more exciting content, please visit https://github.com/abc-club/free-resources

background

Without further ado, can we use pure CSS to achieve the following effects:

The answer is yes.

This can be achieved with the help of css:placeholder-shown:valid:invalid pseudo-class and html5 input pattern attribute

The current compatibility of the :placeholder-shown pseudo-class is as follows:

:placeholder-show compatibility

Directly on the code! ☺️

Source code

https://jsbin.com/qenucaz/edit?html,css,output

html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div class="input-fill-box">
    <input class="input-fill" placeholder="Email" pattern="^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$" required>
    <a href="javascript:" class="clear">close</a>
    <label class="input-label">Email</label>
</div>
</body>
</html>

css:

.input-fill{
  width: 100%;
  margin: 0;
  font-size: 16px;
  line-height: 1.5;
  outline: none;
  padding: 20px 16px 6px;
  border: 1px solid transparent;
   background: #f5f5fa;
  border-radius:10px;
  transition: border-color .25s;
}
.input-fill:placeholder-shown::placeholder {
    color: transparent;
    
}
.input-fill-box {
  width: 50%;
    position: relative;
}
.input-label {
    position: absolute;
    left: 16px; top: 14px;
    pointer-events: none;
    color:#BEC1D9;
   padding: 0 2px;
    transform-origin: 0 0;
    pointer-events: none;
    transition: all .25s;
}
.input-fill:not(:placeholder-shown) ~ .input-label,
.input-fill:focus ~ .input-label {
    transform: scale(0.75) translate(0px, -14px);    
}
.input-fill:focus{
  border: 2px solid #1d31aa;
}

.clear{
  position:absolute;
  top:10px;
  right:-20px;
   display: none;
    transition: all .25s;
}
.input-fill::-ms-clear { display: none; }
.input-fill:not(:placeholder-shown) + .clear { display: inline; }

.input-fill:valid {
 border-color: green;
 box-shadow: inset 5px 0 0 green;
}
.input-fill:not(:placeholder-shown):invalid {
 border-color: red;
 box-shadow: inset 5px 0 0 red;
}

More

For more exciting content, please visit https://github.com/abc-club/free-resources

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  The concept of MySQL tablespace fragmentation and solutions to related problems

>>:  How to install docker under centos and remotely publish docker in springboot

Recommend

JavaScript canvas to achieve meteor effects

This article shares the specific code for JavaScr...

How to write the style of CSS3 Tianzi grid list

In many projects, it is necessary to implement th...

In-depth understanding of the creation and implementation of servlets in tomcat

1. What is a servlet 1.1. Explain in official wor...

Analysis and description of network configuration files under Ubuntu system

I encountered a strange network problem today. I ...

How to restore docker container data

The project test environment database data is los...

Solution to 700% CPU usage of Linux process that cannot be killed

Table of contents 1. Problem Discovery 2. View de...

Summary of some common methods of JavaScript array

Table of contents 1. How to create an array in Ja...

Detailed explanation of the fish school algorithm in CocosCreator game

Preface I recently wanted to learn CocosCreator, ...

Summarize some general principles of web design and production

<br />Related articles: 9 practical suggesti...

Use CSS to set the width of INPUT in TD

Recently, when I was using C# to make a Web progra...

A Brief Analysis of MySQL - MVCC

Version Chain In InnoDB engine tables, there are ...

Detailed explanation of the top ten commonly used string functions in MySQL

Hello everyone! I am Mr. Tony who only talks abou...

Copy fields between different tables in MySQL

Sometimes, we need to copy a whole column of data...

JavaScript Document Object Model DOM

Table of contents 1. JavaScript can change all HT...