Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom styles on inputs with angular material 2

I am trying to style an input so that it is a certain width. I am using Angular Material 2 but for some reason the styles in the css isn't being applied to the input tag. Here is a working plunker of my issue along with the affected code:

@Component({
  selector: 'my-app',
  template: `
  <div>
    <form>
      <md-input [(ngModel)]="cycleTime" type="number" name="email">
        <span md-prefix>Cycle Time:</span>
        <span md-suffix>&nbsp;minutes</span>
      </md-input>
    </form>
  </div>
  `,
  styles:[`
      input {
        width: 50px;
        text-align: right;
      }
    `],
  directives: [MdButton, MdInput]
})
export class AppComponent { 
  cycleTime = 1

  constructor() {}
}

How can I style the input that is being created by Angular Material 2?

like image 865
Jarod Moser Avatar asked Jul 18 '16 18:07

Jarod Moser


People also ask

How do you set global angular material typography styles?

You can create a typography config with the define-typography-config Sass function. Every parameter for define-typography-config is optional; the styles for a level will default to Material Design's baseline if unspecified. @use '@angular/material' as mat; $my-custom-typography-config: mat.

How do you set the value in matInput?

The matInput API page will have MAT_INPUT_VALUE_ACCESSOR, which is a token. It is mainly used to add an object into matInput where the object is of the value you want to set. Directives such as MatDatepickerInput can be used too. Here, the matInput assigns the task of choosing the value.

What is matInput?

matInput is a directive that allows native <input> and <textarea> elements to work with <mat-form-field> . Basic Inputs.

What is MatFormFieldControl?

MatFormFieldControl. An interface which allows a control to work inside of a MatFormField .


1 Answers

You can try to override styles of MdInput component like this:

:host input.md-input-element {
  width: 50px;
  text-align: right;
}

Plunker example

Or this way:

/deep/ input {
  width: 50px !important;
  text-align: right;
}

:host-context .md-input-infix input {
  width: 50px;
  text-align: right;
}
like image 59
yurzui Avatar answered Oct 06 '22 12:10

yurzui