floating label and placeholder overlapping

2019-08-28 08:01发布

<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>

enter image description here

here is 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;
            }
        }
    }

if i focus on text field it works fine. but when there is no focus it's overlapping. asdasd dasd dasdsad ad asd das dasd asd a das

2条回答
Summer. ? 凉城
2楼-- · 2019-08-28 08:31
<div>
  <mat-form-field appearance="legacy">
    <mat-label>Legacy form field</mat-label>
    <input matInput placeholder="Placeholder">
  </mat-form-field>
</div>

You can try this code using angular material you can simply solve this kind of issues.

查看更多
劳资没心,怎么记你
3楼-- · 2019-08-28 08:37

try this please:

npm install --save @angular/material @angular/cdk @angular/animations

in 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;
}

Typescript Component:

export class MyFormComponent {

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

hasUnitNumber = false;

constructor(private fb: FormBuilder) {}

  onSubmit() {
    alert('Thanks!');
  }
}
查看更多
登录 后发表回答