search
HomeWeb Front-endCSS TutorialIntroduction to floating labels in css (with case)

Introduction to floating labels in css (with case)

Oct 15, 2018 am 11:33 AM
csscss3 animationhtml5

This article brings you an introduction to floating labels in CSS (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

In the web project, there is a very important module which is the login/registration module. The main part of this module is a form form. This form form contains two important input groups (username/password). Each Each input group contains label and input, and there are various layout plans for label input. Different designers have different design styles, and different front-end engineers have different implementation methods. Through comparison, we found that the current solution focuses on both aesthetics and performance.

So, what are the layout plans for label and input?

Layout plan for label input

  1. Replace label and input (palcholder keyword prompt) are divided into upper and lower parts; // Used a long time ago, now occasionally use

  2. to divide label and input (palcholder keyword prompt) into left and right parts (The label occupies a certain width, and the fonts in the label adopt three common solutions: left-aligned, right-aligned, and aligned at both ends); // Case: Weibo login, jd wap login page, etc.

  3. The label and input (palcholder keyword prompt) are still divided into left and right parts. The difference is that the font in the label is replaced by an icon; // Case: segment fault community login page, etc.

  4. Only contains input (palcholder keyword prompt); // Case: Mobile Taobao login page, Nuggets development community login page, etc.

  5. Only displays input (palcholder key) word prompt), and the label is floated and hidden. When the focus event of the input is triggered, the label is displayed. // Case: the previous login page of mobile Taobao, or refer to JVFloatLabeledTextField, etc.

These solutions have their own advantages and disadvantages. Using the label font and replacing it with icons is more vivid, but the icon is added URL access; the number of fonts in the label is inconsistent, looks unsightly, and the number of words is the same. This solution can only be said to be satisfactory; directly discarding the label can make the interface simple and beautiful, but the label has the function of a label (will be discussed in detail below) label and placeholder); using floating label increases the animation effect, but requires the introduction of js. The performance of this solution is obviously higher than that of not using js (there is a solution that does not use js, but the compatibility is not very good ).

label vs placholder

label: Describes the role of the form element, used to specify the input is the unique field name

placeholder: It prompts the user to enter The format of the content

They seem to be similar, but their responsibilities are different. Many students have made big mistakes here.

If you need to know more about them, please refer to MDN

Animated label (no-js)

User interaction page At the same time, user interaction with animation is often easier to impress users. The following introduces a floating label implemented using pseudo classes.

HTML code:

<div>
    <input>
    <label>账号</label>
</div>

Basic layout css code:

.input-group {
    position: relative;
    margin: 100px 20px;
    font-size: 16px;
}

.input-group>input {
    display: block;
    box-sizing: border-box;
    width: 100%;
    padding: 16px;
    font-size: 16px;
    line-height: 1.0;
    border: none;
    border-bottom: 1px solid #cdcdcd;
    border-radius: 0.4em;

    transition: box-shadow 0.3s;
}

.input-group input::placeholder {
  color: #cdcdcd;
}

.input-group>input:focus {
    outline: none;
    box-shadow: 0.2em 0.8em 1.6em #cdcdcd;
}

.input-group>label {
    position: absolute;
    bottom: 50%;
    left: 0;
    z-index: -1;
    
    visibility: hidden;
    color: #050505;
    opacity: 0;
}

First, set the position of the label (posiion: absolute) and define its level (z- index: -1), visibility: hidden, transparency (opacity: 0);

Then, the placeholder style of the input is set, and the pseudo element::placeholder can be used to set its style;

Finally, a transition animation effect is set. When the input element label gains focus, the pseudo-class:focus is used to define the shadow style (box-shadow) and outline style (outline) when the input element label gains focus.

label floating effect style

 .input-group>label {
      ...

      -webkit-transform-origin: 0 0;
              transform-origin: 0 0;
      -webkit-transform: translate3d(0, 0, 0) scale(0);
              transform: translate3d(0, 0, 0) scale(0);
      -webkit-transition:
          opacity 0.3s,
          visibility 0.3s,
          transform 0.3s,
          z-index 0.3s;
          
              transition:
                  opacity 0.3s,
                  visibility 0.3s,
                  transform 0.3s,
                  z-index 0.3s;
 }

.input-group>input:focus ~ label {
    z-index: 1;
    visibility: visible;
    opacity: 1;
    -webkit-transform: translate3d(0, -36px, 0) scale(1);
        transform: translate3d(0, -36px, 0) scale(1);
}

In the collection that defines the label style, add its initial transform deformation effect, set the transition transition effect style, and then define when the input gets focus, its Just use the style of the sibling element label.

The effect of this label floating is different from that of JVFloatLabeledTextField. The former gets focus and the label starts to float immediately, while the latter starts when the user inputs content (that is, when the placeholder disappears).

To make the two have the same effect, we can use the feature that pseudo classes can be nested and modify .input-group>input:focus ~ label to .input-group>input:focus:not(:placeholder -shown) ~ label, here: placeholder-shown can define the visible and hidden effect of the placeholder, but its compatibility is not very good, ie/edge does not support it at all, chrome, safari, and Firefox can. Case: demo

The above is the detailed content of Introduction to floating labels in css (with case). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault思否. If there is any infringement, please contact admin@php.cn delete
The Ultimate Guide to Linking CSS Files in HTMLThe Ultimate Guide to Linking CSS Files in HTMLMay 13, 2025 am 12:02 AM

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

CSS Flexbox vs Grid: a comprehensive reviewCSS Flexbox vs Grid: a comprehensive reviewMay 12, 2025 am 12:01 AM

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

How to Include CSS Files: Methods and Best PracticesHow to Include CSS Files: Methods and Best PracticesMay 11, 2025 am 12:02 AM

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframes CSS: The most used tricks@keyframes CSS: The most used tricksMay 08, 2025 am 12:13 AM

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSS Counters: A Comprehensive Guide to Automatic NumberingCSS Counters: A Comprehensive Guide to Automatic NumberingMay 07, 2025 pm 03:45 PM

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),