Adjust the starting color of a mat-input in Angular 9

I am encountering an issue with the styling of a standard matInput field within a mat-form-field, all contained within a [formGroup] div. The dark background of the parent element is causing visibility problems with the default dark text color of the input field. Here's a snippet of the HTML code:

<div [formGroup]="myInfo">
  Year: &nbsp;
  <mat-form-field>
    <input class="myClass" style="color: white" matInput (keypress)="numericOnly($event)" placeholder='YYYY' 
           formControlName="yearControl">
  </mat-form-field>
</div>

After researching online and following some advice, I made changes in the .scss file like so:

.childClass {
  input { color: #fff !important; }
  label.mat-input-placeholder { color: #fff !important; }
  ::ng-deep .mat-form-field-label { color: #fff !important; }
  ::ng-deep .mat-form-field-ripple { color: #fff !important; }
  ::ng-deep .mat-form-field-underline { color: #fff !important; }
}

While this successfully updated the placeholder and entered text to white, the underline remained black. Any further suggestions would be greatly appreciated. Thank you.

Answer №1

The reason it isn't functioning correctly is due to the selector you are using.

::ng-deep .mat-focused .mat-form-field-label

This selector contains the class .mat-focused, which only works when the field is focused on.

To fix this, simply update the selector to:

::ng-deep .mat-form-field-label and adjust the placeholder color with:

label.mat-input-placeholder

Answer №2

I modified the input color to a brighter shade against a dark background like so:

I created my own custom input component called switched-input.component

HTML:

 // switched-input.component.html
 <mat-form-field>
   <input matInput placeholder="{{placeholder}}" [formControl]="inputControl"/>
 </mat-form-field>

SCSS:

// switched-input.component.scss
mat-form-field {
  width: 100%;
}
// text color of user input when input is selected
input {
  color: #fff !important;
}

TypeScript:

// switched-input.component.ts
import { FormHelper } from './../../../helper/form.helper';
import { Component, OnInit, Input, ViewEncapsulation } from '@angular/core';
import { FormControl } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'switched-input',
  templateUrl: './switched-input.component.html',
  styleUrls: ['./switched-input.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class SwitchedInputComponent implements OnInit {
  inputControl: FormControl;

  @Input() placeholder: string;

  @Input() set control(controlObj: FormControl) {
    this.inputControl = controlObj;
    if (this.placeholder && this.placeholder !== '') {
      this.placeholder = this.translateService.instant(this.placeholder);
    }
  }

  get control(): FormControl {
    return this.inputControl;
  }

  constructor(private translateService: TranslateService) {}

  ngOnInit() {

  }    
}

In order to change the underline and label color of the input fields, I added the following styles in my app's main styles.scss file:

// styles.scss
// underline color of mat input fields
.mat-form-field-underline {
  background-color: #fff !important;
}
// label color of mat input fields
.mat-form-field-appearance-legacy .mat-form-field-label {
  color: #fff;
}

https://i.sstatic.net/9PlHF.jpg

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjust the JavaScript variable upon pressing the "c" key

I'm trying to figure out how I can toggle the value of variable x in JavaScript when the key "c" is pressed. Specifically, I want x to change from 0 to 1 when "c" is pressed and revert back to 0 when it's released. I have already defined and name ...

The alignment of the floating left element is creating conflicts with the styling of other div

As I embark on a design project, I find myself juggling three divs for distinct products, alongside a payment method image div and a copyright div. The product divs are aligned to the left within their specific wrapper, ensuring they center and display sea ...

Bringing in manageable RxJS operators

RxJS 5.5 has undergone a significant change and has introduced lettable operators to replace the traditional operators we were using previously. In the article, there is a note that states: Lettable operators can now be imported from rxjs/operators, bu ...

Populate an Angular form using @Input to display data retrieved from a Firebase database through a service injection

I've encountered a challenge while working on an Ionic Angular project involving a form. The issue I'm facing is related to a component that serves as a form filled by an Input (Component). The problem seems to be that the form gets populated be ...

What steps should be taken to identify a new path following a call to router.navigate?

Trying to interrupt a route change using a router guard. When I use: this.router.navigate([“myApp/userProfiles”]); After calling this, it passes through the CanDeactivate interface of the guard. The guard then needs to determine the actual destinatio ...

Is there a way to align the image to the left within the div contained in my header section?

I am facing an issue with positioning the logo in the header section of my website. It appears correctly on the left side for tablet and mobile versions, but on desktop, it does not align to the corner as desired. I tried adding a right margin, but I belie ...

Div expanding beyond intended size when media reaches maximum 468 pixels

When zoomimg is set to width: 145px; height: 145px; it ends up pushing the text that should be inside its parent element out of the parent. If you want to see the Zoomimg ProjectKort divs, check out the code below: .projectkort{ margin: 10px 0px 10px 0 ...

I am encountering a continuous css ParserError while trying to compile my react project

I'm having trouble with the compilation of my React project. While everything runs smoothly with npm start, I encounter an error during compilation: npm run build <a href="/cdn-cgi/l/email-protection" class="__cf_ema ...

What is the best way to personalize Material UI elements, such as getting rid of the blue outline on the Select component?

Embarking on my journey of developing a React app, I made the decision to incorporate Material UI for its array of pre-built components. However, delving into the customization of these components and their styles has proven to be quite challenging for me ...

What is the reason behind Chrome removing an SVG image pattern while utilizing jQuery Draggable?

Currently, I am trying to encapsulate an SVG within a draggable div. The SVG contains a shape or path with an image fill on the face. Surprisingly, it displays perfectly and functions flawlessly in Firefox. However, when it comes to Chrome, the dragging op ...

Is there a way to automatically redirect my page after clicking the submit button?

I'm having trouble with the code below. I want it to redirect to NHGSignin.php if 'new horizon gurukul' is entered. When I click the Next button, it's supposed to take me there but it's not working as expected. Can anyone help me f ...

The Angular Material dialog fails to display content when triggered within an event listener in Google Maps

Within my project, I am utilizing Angular 6.0.6 and Angular Material 6.3.0. One issue I have encountered is with a dialog component that I have added to the entryComponents in the app module. Strangely, when I attempt to open this dialog within the rightcl ...

Switching out one block of HTML with another in JavaScript is a powerful way to dynamically update

I am working on creating a feature on a webpage where clicking a button will change the HTML code inside a specific div. Essentially, I want to be able to update the content of a div by simply clicking a link. Here are two different sets of HTML code: Co ...

A div with an absolute position containing a header, content, and footer

How can I place a header, content, and footer inside a div element with absolute positioning, where the position value cannot be changed? The header and footer have fixed heights, and the content should occupy the remaining space. The header should always ...

Navigate through the website by transforming the arrow and clicking the link

I am struggling with transitioning my arrow from › (right) to ˇ (down) when toggled. I can't figure out where to place it. If you look at Menu 1 > Submenu 1 > Text with hyperlink I want the 'Text with Hyperlink' to be clickable on ...

Issue with jquery .height(value) function not functioning properly on Internet Explorer

I have come across a challenge with the following code snippet: var winheight = $(window).height(); var headerHt = (0.11 * winheight); $(".header").height(headerHt) The purpose of this code is to dynamically adjust the size of .header and other elements ...

Hidden input field when inactive

Whenever I start typing in my input form, the text disappears after a moment. The only way to prevent this is by continuously pressing on a letter key on my keyboard. This issue began after I added a resizeInput attribute that adjusts the size of the inpu ...

Utilizing ngx-pagination for repetitive instances of a single component

I have encountered an issue with the ngx-pagination library in my reusable data table component. While it works perfectly for a single instance, using it multiple times causes the pagination to only function on one of the components. Is there a way to make ...

Tips for implementing form validation for a dropdown menu input

When it comes to displaying error messages for users who do not select a value in the drop-down list, the VMware Clarity documentation provides clear instructions for <input type="text"> elements (click here): To apply validation styling to input ...

Timestamp in Angular for September 8, 2019 at 10:

Is there a way to achieve a timestamp format like 201909081015 without using any hash or dash symbols? Does anyone have any suggestions on how to accomplish this specific format? ...