angular 5 material - form fields stuck at 180px

后端 未结 6 1232
感动是毒
感动是毒 2020-12-29 01:26

Ive create a form in a dialog using material forms but I cant seem to get the inputs to be wider than 180px despite following numerous examples including https://material.an

相关标签:
6条回答
  • 2020-12-29 01:27

    The solution I found was this one into general style.css

    mat-form-field {
      margin-left: 2.5rem;
      width: calc(100% - 2.5rem);
    }
    
    0 讨论(0)
  • 2020-12-29 01:27
    ::ng-deep .mat-form-field-infix {
      width:800px !important;
    }
    

    This should work fine.Not sure if it can be done without custom CSS.

    0 讨论(0)
  • 2020-12-29 01:28

    It's that .mat-form-field.mat-form-field in the css that's causing an issue. As soon as I removed that, I could control the widths with .input-full-width width setting. I set up a demo here: StackBlitz.com

    <h1 mat-dialog-title>{{title}}</h1>
    <mat-dialog-content>
      <form novalidate #f="ngForm" class="form-full-width">
        <mat-form-field class="input-full-width">
          <input type="text" [(ngModel)]="data.name" name="name" matInput placeholder="Name">
          <mat-hint>Enter a unique name.</mat-hint>
        </mat-form-field>
        <mat-form-field class="input-full-width">
          <textarea [(ngModel)]="data.description" name="description" matInput placeholder="Description"></textarea>
          <mat-hint>You should describe the purpose/use of this thing.</mat-hint>
        </mat-form-field>
      </form>
    </mat-dialog-content>

    .form-full-width {
        min-width: 150px;
        max-width: 500px;
        width:100%;
    }
    
    .input-full-width {
        width:800px;
    }
    
    /* .mat-form-field.mat-form-field {
        width: auto;
    } */

    0 讨论(0)
  • 2020-12-29 01:29

    can you try using

    mat-form-field {
    width: 100%;
    }
    

    I had the same issue in a mat-card, this worked for me.

    0 讨论(0)
  • 2020-12-29 01:29

    Just like Jonesie said, this behavior is related to View Encapsulation. In this context .mat-form-field-infix takes 180px as default value probably beacause of this. The auto value lets the browser calculates the width instead of puting hardcoded value. The !important declarations forces the style override for this.
    I am using !important since it complies with the rules for the use of this property. See docs

    ::ng-deep .mat-form-field-infix {
        width: auto !important;
    }
    
    0 讨论(0)
  • 2020-12-29 01:37

    Seems like it's something to do with View Encapsulation. This thread explains it: https://github.com/angular/material2/issues/4034 but changing encapsulation for the component causes all sorts of compile failures.

    This article gave me the correct solution:https://material.angular.io/guide/customizing-component-styles

    I'll move my style to global scope...

    .formFieldWidth480 .mat-form-field-infix {
       width: 480px;
    }
    

    and in the component that opens the dialog:

    this.newDialog = this.dialog.open(NewDialogComponent,
      {
        width: '650px', height: '550px',
        data : newThing,
        panelClass : "formFieldWidth480"
      });
    

    I hope this saves someone else the day I lost...

    0 讨论(0)
提交回复
热议问题