浮动标签和占位符重叠(floating label and placeholder overlapp

2019-10-30 09:02发布

<div class="form-group form-default form-spacing">
     <input class="form-control" placeholder="Name" type="text" required>
     <span class="form-bar"></span>
     <label class="float-label">Name</label>
</div>

这里是CSS,

.form-material {
    .form-group {
        position: relative;

        &.form-static-label .form-control {
            ~.float-label {
                top: -14px;
            }
        }
    }

    .form-control {

        &:focus {
            border-color: transparent;
            outline: none;
            box-shadow: none;

        }

        &:focus,
        &:valid {
            ~.float-label {
                top: -14px;
            }
        }
    }

如果我专注于文本字段,它工作正常。 但是,当不存在焦点是重叠的。 asdasd DASD dasdsad广告ASD DAS DASD ASD一个DAS

Answer 1:

<div>
  <mat-form-field appearance="legacy">
    <mat-label>Legacy form field</mat-label>
    <input matInput placeholder="Placeholder">
  </mat-form-field>
</div>

您可以使用角材料试试这个代码,你可以简单地解决这类问题。



Answer 2:

试试这个请:

NPM安装--save @角/材料@角蛋白/ cdk @角/动画

在app.module.ts:

import {MatFormFieldModule} from '@angular/material/form-field';

HTML:

<form [formGroup]="addressForm" novalidate (ngSubmit)="onSubmit()">
  <mat-card class="shipping-card">
    <mat-card-header>
      <mat-card-title>Address, Neighborhood or ZIP</mat-card-title>
    </mat-card-header>
    <mat-card-content>

      <div *ngIf="hasUnitNumber">
        <div class="col">
          <mat-form-field class="full-width">
            <textarea matInput placeholder="first Name" formControlName="firstName"></textarea>
          </mat-form-field>
        </div>
      </div>

    </mat-card-content>
    <mat-card-actions>
      <button mat-raised-button color="primary" type="submit">Submit</button>
    </mat-card-actions>
  </mat-card>
</form>

CSS:

.full-width {
  width: 100%;
}

.shipping-card {
  min-width: 120px;
  max-width: 620px;
  margin: 20px auto;
}

.mat-radio-button {
  display: block;
  margin: 5px 0;
}

.row {
  display: flex;
  flex-direction: row;
}

.col {
  flex: 1;
  margin-right: 20px;
}

.col:last-child {
  margin-right: 0;
}

打字稿组件:

export class MyFormComponent {

addressForm = this.fb.group({
    firstName: [null, Validators.required],
  });

hasUnitNumber = false;

constructor(private fb: FormBuilder) {}

  onSubmit() {
    alert('Thanks!');
  }
}


文章来源: floating label and placeholder overlapping