純粋なCSSフォームポップアップ

おそらく、あなたはすでにこのテクニックに出くわしているでしょう。 これは、テキストプロンプト(プレースホルダー)があるように見える入力フィールドですが、入力を開始しても消えず、横にプッシュされます。 私はこのアイデアが好きです。 Brad Frostは、この手法に関する非常に優れた記事を執筆し、すべての長所と短所を詳細に調べました。

この手法の使用例のほとんどは、JavaScriptに依存しています。 ある晴れた日、私はnest.comに行き、そこでこのテクニックを見、考えました:JavaScriptなしで同じものを実装することは可能ですか? そして、これはそれから来たものです。

nest.comのフォームは次のようになります。

画像

CodePenで私のバージョンを見ることができます 。 もちろん、テキストがスムーズに消えて下からラベルが進むネストのようにクールではありません。 もちろん、私はおそらく純粋なCSSで行っていたように、JavaScriptでも同じことを行うことができましたが、この記事の目的は原理を示すことであり、あなたは自分の美しさを作ることができます。

この手法を使用することが理にかなっている理由は2つあります。

  1. スペースを節約します。 入力フィールドとそのラベルが組み合わされているため、使用するスペースが少なくなります。 入力フィールドにフォーカスが移ると、両方を表示し続けるために、フィールド内のスペースの一部を使用するか、他の人を犠牲にしてこの特定の入力フィールドのスペースを一時的に拡張できます。
  2. 入力フィールド全体が1つの大きなボタンになります。 もちろん、通常の入力フィールドは既にクリックに応答しておりfor label for属性for指定すると、ラベルがアクティブになりますが、大きな長方形には非常に魅力的なものがあり、クリックして入力を開始できます。 これは、特にモバイルアプリケーションに適しています。

一般的なケースでは、常に表示されるラベルの方が優れていますが、正しく実装されていれば、この手法が適切な場合もあります。 ただし、アクセシビリティを台無しにするリスクは常に存在するため、注意してください。 このアプローチのもう1つの欠点は、ラベルを補完するプレースホルダーに情報を配置できないことです(たとえば、ラベルは「電話番号」であり、プロンプトは「(555)555-5555」です)。

トリック#1-ヒントとしてのタグ付け


アイテム
     (    ,           )   .       ,   ,         .     ,     ,     ,      . 

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

№2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
№3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

     (    ,           )   .       ,   ,         .     ,     ,     ,      . 

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

№2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
№3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

№2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
№3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

№2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
№3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

     (    ,           )   .       ,   ,         .     ,     ,     ,      . 

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

№2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
№3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

     (    ,           )   .       ,   ,         .     ,     ,     ,      . 

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

№2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
№3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

№2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
№3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

№2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
№3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

     (    ,           )   .       ,   ,         .     ,     ,     ,      . 

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>
, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

№2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
№3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

     (    ,           )   .       ,   ,         .     ,     ,     ,      . 

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

№2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
№3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

№2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
№3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

№2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
№3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

( , ) . , , . , , , .

画像
HTML:
<div> <input id="name" name="name" type="text" required> <label for="name">Your Name</label> </div>


CSS:
form > div { position: relative; } form > div > label { position: absolute; }

№2 - :focus
, for . , , :focus (+), , . , .

CSS:
input:focus + label { /* - */ }

, . , . : , - .

form.go-bottom label { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; transition: 0.2s; } form.go-bottom input:focus + label top: 100%; margin-top: -16px; }
№3 - :valid
- , , , . , CSS :valid , , . , , - . :

<input type="text" required>

, , . , :

form input:valid { background: white; }
- , .


. , :

画像
, . JavaScript: , , maman . , , , . CodePen.

Source: https://habr.com/ru/post/J216669/


All Articles