93

enter image description here

I use angular 7 with angular material and i want to remove the space bottom as you can see. I tried many way but no sucess.

user9714967
  • 1,534
  • 3
  • 13
  • 21

17 Answers17

138

You can add this in your css

::ng-deep .mat-form-field-wrapper{
   margin-bottom: -1.25em;
}

NOTE: As you remove the space you cannot put <mat-hint>hint</mat-hint> or <mat-error>error</mat-error> properly. Error and hint get inside the form-field.

Without using ::ng-deep( for Angular 8 )

Turn off encapsulation of your component inside which you change the padding.

You can do this by

import {Component,ViewEncapsulation} from '@angular/core';
@Component({
  selector: 'example',
  templateUrl: 'example.component.html',
  styleUrls: ['example.component.css'],
  encapsulation : ViewEncapsulation.None,
})
export class ExampleComponent {}

Wrap the component you want to style in a custom class. So it wont affect any other mat-form-field components. Let's wrap this with with my-form-field class for now

<mat-form-field class="my-form-field">
  <input matInput placeholder="Input">
</mat-form-field>
.my-form-field .mat-form-field-wrapper {
      margin-bottom: -1.25em;
}

You can add these css in global stylesheet without turning off view encapsulation. But the more elegant method is the above one.

coppereyecat
  • 161
  • 2
  • 13
Akhi Akl
  • 3,795
  • 1
  • 15
  • 26
  • Use !important othervise it won't work. => margin-bottom: -1.25em !important; – Isuru Dilshan Jul 24 '20 at 15:40
  • 1
    You don't need to put !important for this to work. since there is no margin-bottom in `.mat-form-field-wrapper` styles written by Angular material and even if they had any margin-bottom styling (not !important by default) because we are wrapping `mat-form-field` with a custom class, in here for example 'my-form-field' – Akhi Akl Jul 24 '20 at 17:06
  • @Isuru Dilshan If it is still not working. Can you provide me an example in Stackblitz or any other online IDE. – Akhi Akl Jul 24 '20 at 17:11
  • I work with Angular 10.0.3. Without !important it didn't work. But your answer is the only answer I found to fix this issue. Thanks for that – Isuru Dilshan Jul 24 '20 at 17:23
  • and also the latest indigo-pink.css do have following .mat-form-field--wrapper. This is the code **.mat-form-field-wrapper { padding-bottom: 1.34375em }** – Isuru Dilshan Jul 24 '20 at 17:33
  • 1
    I did not meant it don't have a css for `.mat-form-field-wrapper` I meant it does not have the css with any margin-bottom styling. And I am really confused why it doesn't work with Angular 10. I had used this with latest Angular material. Kindly check this [stackblitz](https://stackblitz.com/edit/angular-bsrbhw?file=src/app/form-field-overview-example.html) which uses latest angular material. – Akhi Akl Jul 24 '20 at 17:37
  • 1
    Now I get it. I miss understanding the padding and margin-bottom. You are correct – Isuru Dilshan Jul 24 '20 at 17:42
  • Great!!! Congrats!!! Thanks!!!. It works. Plus, do not affect the rest of the application. It is applied only to the components included with the specific class *.my-form-field* applied. It should be explained as a pattern in some article. – Montells Apr 07 '23 at 11:37
87

For anyone reading this in 2023: Angular 15 now added a new property to the form fields: subscriptSizing.

If you add subscriptSizing="dynamic" to mat-form-field, it'll remove the space until an error or hint actually needs to get displayed and only then expands. This causes a layout shift, but depending on your use case it is probably the better option than manually adding a margin as suggested (and necessary up to version 14) before.

For reference see https://material.angular.io/components/form-field/api#SubscriptSizing

You can also specify this globally as default via the MAT_FORM_FIELD_DEFAULT_OPTIONS injection token; see https://material.angular.io/components/form-field/api#MatFormFieldDefaultOptions

// in main.ts (Angular 15+)
import { MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material/form-field';

bootstrapApplication(AppComponent, {
  providers: [
    {
      provide: MAT_FORM_FIELD_DEFAULT_OPTIONS,
      useValue: {
        subscriptSizing: 'dynamic'
      }
    }
  ],
});

Dom
  • 333
  • 2
  • 5
Christoph142
  • 1,309
  • 11
  • 14
  • Is it possible to add this property to all form fields globally? – mbauer Feb 02 '23 at 14:24
  • 1
    @mbauer: didn't test it as I don't have access to my work device right now, but I'm pretty confident, you could just derive a custom component from the mat-form-field, set this property on it and exchange all usages of mat-form-field in your code with your inherited component. – Christoph142 Feb 03 '23 at 10:36
  • 5
    @mbauer Yes, you can add it in the MAT_FORM_FIELD_DEFAULT_OPTIONS in your app.module – Matt Harrison Mar 03 '23 at 00:58
  • Thanks @MattHarrison! I supplemented my answer with this info. – Christoph142 Mar 03 '23 at 12:08
  • So for anyone else also thinking that they can update to Angular 15 to get this new feature and finally stop wasting hours fiddling ccs overrides to the material components: In Angular 15, most if not all material components have been deprecated. And long story short, despite there being automated tool for migration, it's not at all a trivial effort to accommodate this change (for our site at least). And many other packages with Angular dependencies such as the mat theme generator have not had time to add Angular 15 support, so currently the material conversion tool breaks those. – user2027080 May 02 '23 at 18:23
  • 1
    This is the best answer. It's up to date and actually works. None of the other css tricks worked for me. I'm using Angular 15. – João Victor Aug 03 '23 at 18:27
  • This is the best way to solve the issue. I'm using Angular material ^15.2.8 – Anzil khaN Aug 09 '23 at 17:37
46

Try the following:

<mat-form-field style="margin-bottom: -1.25em">

(You can follow the discussion about this extra bottom space here: https://github.com/angular/components/issues/7975)

Emeric
  • 6,315
  • 2
  • 41
  • 54
16

In angular 9, I was able to remove the gaps only by adding the following into the component.scss (the other answers didn't worked in my case):

:host ::ng-deep .mat-form-field-wrapper{
  margin: 0 !important;
  padding: 0;
}

Also, I'm using appearance="outline", for other appearances, it will probably be necessary to change other css classes and properties, since it may have other elements.

Rafael Cerávolo
  • 673
  • 6
  • 13
6

My solution was to use an additional class.

HTML:

<mat-form-field class="no-padding">
    <input type="number" matInput placeholder="Speed" [ngModel]="speed"
           (ngModelChange)="onSpeedChange('speed', $event)"/>
  </mat-form-field>

SCSS:

.no-padding {
  .mat-form-field-wrapper {
    padding-bottom: 0 !important;
  }
}

The !important keyword is unfortunately necessary as it will otherwise just pull in the default instead.

TomDK
  • 1,321
  • 11
  • 16
  • 2
    I had to put this into `styles.scss` instead of `my.component.scss` to get this to work with angular 11 – brt Sep 09 '21 at 21:23
4

or simply:

html

<mat-form-field class="no-bottom">
...
</mat-form-field>

css

.no-bottom {
  margin-bottom: -1.25em !important;
}
Jonathan
  • 3,893
  • 5
  • 46
  • 77
3

I test by Angular 10, this works:

:host ::ng-deep .mat-form-field-wrapper{
  margin: 0 !important;
  padding: 0;
}

Also, if you need to apply ONLY on a special field, define a class:

    <mat-form-field appearance="outline" class="no-wrapper">
      <input matInput>
    </mat-form-field>

And put class name in your css:

    :host ::ng-deep .no-wrapper .mat-form-field-wrapper{
      margin: 0 !important;
      padding: 0;
     }
2

I am able to remove bottom space of mat-form-filed by using following css in style.scss

.mat-form-field-wrapper{
    padding-bottom: 10px;
}
2

What about the following template:

<mat-form-field appearance="standard" class="compact-input">
  <mat-label>Test</mat-label>
  <input matInput>
</mat-form-field>

and the following SCSS:

.compact-input {
  .mat-form-field-flex {
    padding-top: 0;
  }

  .mat-form-field-wrapper {
    padding-bottom: 0;
  }

  .mat-form-field-underline {
    bottom: 0;
  }
}

Only tested for standard appearance though.

Don't forget to add encapsulation: ViewEncapsulation.None to your component. If you put the SCSS in the global styles, you might need to add some !important's.

1FpGLLjZSZMx6k
  • 947
  • 1
  • 8
  • 26
2
.mat-mdc-form-field-subscript-wrapper {
    display: none;
}
J. Jerez
  • 704
  • 8
  • 6
  • This is a poor solution. You can achieve the same by setting the `subscriptSizing=dynamic` option on the form field. See https://stackoverflow.com/a/75107066/5470544 – JSON Derulo Apr 07 '23 at 11:03
  • @JSONDerulo there could be a custom control which uses `mat-form-field` (or whatnot) under the hood. In this case you cannot set properties of the element you don't have direct access to. So I would not call this solution "poor". Sometimes it is the only solution. – Alexander Jun 06 '23 at 21:52
1
.mat-form-field-infix{
    display: block;
    position: relative;
    flex: auto;
    padding: 0;
    border-top: 0px solid transparent !important;
}

you can add important in css

Tienanhvn
  • 239
  • 5
  • 9
1

You can add height:4em to mat-form-field as such:

    <mat-form-field
        class="height-4em"
        appearance="outline"
        >
        <mat-label>Favorite food</mat-label>
        <input matInput placeholder="Ex. Pizza" value="Sushi">
    </mat-form-field>
1

dont use ::ng-deep
this is simple.. add this to your css file

mat-form-field{
    margin-bottom: -1.25em
}
nisith
  • 11
  • 3
0

By changing the font size with material theme you will get smaller input

$typography: mat-typography-config(
  $input: mat-typography-level(14px, 1.125, 400),
);

@include mat-core($typography);
Wlada
  • 1,052
  • 14
  • 25
0

I had a similar problem w/ mat-fab button when using it together w/ mat-spinnner. Whenever the spinner would be "activated" the elements around would shift down. The problem was w/ .mat-fab .mat-button-wrapper which comes from an embedded wrapping span element of mat-spinner after rendering.

Setting,

::ng-deep .mat-fab .mat-button-wrapper {
  padding: 0;
  vertical-align: middle;
}

solved the problem. The vertical-align is not needed but aligns nicely the spinner w/in the mat-fab button.

vpap
  • 1,389
  • 2
  • 21
  • 32
0

My solution: Adding a custom class to mat-form-field and targeting the .mat-mdc-form-field-bottom-align class to give its display property a value of none did the trick in my case.

::ng-deep .my-form-field .mat-mdc-form-field-bottom-align {
    display: none !important;
}
<mat-menu #menu="matMenu">
  <ng-container *ngIf="currentPage === 1">
    <mat-form-field class="my-form-field" style="padding:10px;">
      <input matInput>
    </mat-form-field>
  </ng-container>
</mat-menu>
mdharr
  • 1
  • 1
0

enter image description hereWith Angular 16

   .mat-mdc-form-field-bottom-align::before {
      display: none !important;
    }

but set it in style.css on the root of the folder, not in css of the component..

Rickyc81
  • 23
  • 1
  • 1
  • 7