Administer styles to individual Angular Material components using external CSS files

I am attempting to customize a specific mat input element in my application using an external CSS file.

For instance, I have imported textfield.css and style.css files for this purpose.

Let me provide some code snippets below:

<mat-form-field>
    <input class="txtfieldwithBorderGreen" formControlName="password" matInput type="Password" placeholder="password">
    
</mat-form-field>

Here is the HTML file with the applied class

"txtfieldwithBorderGreen"
:

I aim to configure the properties of any text field by simply applying this class wherever necessary.

Thus, I created a textfield.css file and included it in the style.css file as shown below:

.mat-form-field-appearance-legacy .mat-form-field-label {
    background-color: green !important;
}

The textfield.css file is imported into styles.css file as follows:

@import 'globalcss/textfield.css';

html, body { height: 100%; padding: 0; margin: 0; }
body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }

This styling will be implemented on all matInputs, but I specifically want to target one input. Hence, I added the following code:

.txtfieldwithBorderGreen .mat-form-field-appearance-legacy .mat-form-field-label {
    background-color: green !important;
}

However, it seems that the above code is not functioning properly. When I remove .txtfieldwithBorderGreen, the styling works fine across all mat inputs. Any insights on where I may be making a mistake or if any adjustments are needed?

P.S. My objective is to create predefined CSS classes like

textfield-smaller textfield-big, etc., and easily assign the class name to the mat inputs without needing to modify the respective CSS files each time.

Any assistance would be greatly appreciated!

Answer №1

Begin by creating a separate file named textfield.css

Next, import it into your style.css like so:

@import 'globalcss/textfield.scss';

Place that file in the globalcss folder located under src, like this:

src=>globalcss=>textfield.scss

Now you can style any matInput element by simply adding the class "small-text-field" as shown below:

<mat-form-field class="small-text-field"> 
    <input matInput placeholder="Username">
</mat-form-field>

The contents of the textfield.css file should look something like this:

.mat-form-field.smallTextfield .mat-form-field-wrapper {
    font-size: 10px;
    width: 125px;
}

Applying this class to any matInput element throughout the application will have the desired effect. Keep in mind that ng::deep is deprecated, and instead use the following directive within the ts file:

encapsulation: ViewEncapsulation.None

This directive should be included inside the component declaration:

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css'],
  encapsulation: ViewEncapsulation.None
})

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

Most Effective Method for Switching CSS Style Height from "0" to "auto"

After coming across a question with no answer that suited my needs, I decided to share the solution I created. In my case, I had a hidden generated list on a page which was initially set with a CSS height of "0" and then expanded upon clicking by transit ...

Ways to avoid text covering the button using CSS

Take a look at this image: https://i.sstatic.net/U7wGV.png In the image, you can see that I have entered text in the input box. However, since there is also a button placed inside the box, the text gets hidden behind it. Is there a way to prevent this i ...

Plugin for creating a navigation bar with a jQuery and Outlook-inspired style

Our team is considering transitioning our asp.net forms application to MVC. Currently, we utilize outlook-style navigation with ASP controls such as listview and accordion, using code referenced here. Are there any jQuery/CSS controls available for MVC t ...

Issues with CSS transitions not functioning properly following the use of toggleClass()

I recently implemented a toggle-menu feature, which you can see in action on this demo. To enhance the toggle-menu, I added a CSS transition effect to div.nav-menu, utilizing max-height:0; and increasing it to max-height:480px;. However, after clicking t ...

Angular Materials auto-complete - Presenting choices depending on certain criteria

I have implemented 4 components, each containing a material autocomplete feature with the same set of options. Whenever I select an option in one component, it sets isAvailable to false and disables that option in the other components. The array below show ...

Anti-aliasing with @font-face in Internet Explorer versions 7 through 9

Having some trouble getting @font-face to display correctly in IE, and I haven't found a solution yet. I've looked into this resource: but unfortunately it didn't work on my Windows 7 IE. Does anyone have a better suggestion? ...

Automatically tally up the pages and showcase the page numbers for seamless printing

I've been tackling a challenge in my Vue.js application, specifically with generating invoices and accurately numbering the pages. An issue arose when printing the invoice – each page was labeled "Page 1 of 20" irrespective of its actual position in ...

The current height of the div is not meeting the desired specifications outlined in

I am encountering some issues with nested 'div' elements. Within a parent div, I have two child 'div's. <div class="case-study-video-container"> <div class="video-holder"> <?php putRevSlider( '' ); ?> ...

Utilize Angular to input data into a CRUD table and enhance its value

While working on a CRUD table today, I encountered several errors that are too numerous to mention in the title of the question. Please forgive this oversight. Despite being a simple table and not an Angular material table, I am struggling with debugging i ...

Angular and the collection of viewChild elements

I have come across this code snippet that is functioning quite well... import { Component, ViewChild, ElementRef } from '@angular/core'; import { NavController } from 'ionic-angular'; declare var google; @Component({ selector: &apo ...

Finding the value of an item within a JSON sub-array based on the most recent date that falls within a specified timeframe

I am currently developing a billing application using Angular. The items array that I am working with is structured like this (showing only one item here):- items: [{ _id: "5ee2298f59ce2e1747cb5539" createdAt: "2020-06-11T ...

Checkbox designed in a non-traditional style that requires a label in order to complete the

My Bootstrap modal features multiple-choice options for users, presented as checkboxes. However, I want to customize the appearance so that when a user selects an option, it is highlighted rather than shown as a checkbox. To achieve this look, I enlarged ...

Ways to mimic an object and confirm that a specific parameter was passed to the mimic

I am currently experimenting with testing an HttpInterceptor, using ng-mocks: @Injectable() export class AuthTokenInterceptor implements HttpInterceptor { constructor(private store: Store) {} intercept(req: HttpRequest<any>, ...

Learning how to utilize localStorage in conjunction with a color picker to modify the --var of :root

After spending an entire afternoon on my JavaScript issue as a beginner, I have a specific goal in mind: allowing the user to select and change the main color of the page. To implement this, I inserted a color picker in my HTML: <input type="color ...

In Chrome, the computed style of background-position is returned as 0% 0%

Let's say I have an element and I am interested in finding out its background-position: To achieve this, I use the following code snippet: window.getComputedStyle(element).getPropertyValue('background-position') If the value of background ...

Getting started with testing in Angular 2

When I first started coding my app, unit tests were not on my mind. Now, I realize the importance and need to write them. However, while attempting to write tests using Jasmine only, I encountered an error when trying to import components - "system is no ...

Why is there a thin line still visible on the other sides when a transparent background is used, but only on mobile devices, with a curved border on one side?

Recently, I've delved into the world of creating CSS art and stumbled upon a peculiar issue with CSS borders that has left me puzzled. When I apply a border to only one side of an element with a transparent background and rounded edges, I notice a fa ...

How to Perfectly Align a Gif

I'm trying to understand the functionality behind this code snippet. Can anyone shed some light on it? img{ display:block; margin:auto; } I attempted to center a gif using this code block after my teacher suggested trying align:center; without succe ...

Running complex operations within a sorting function just once

I am facing the challenge of sorting an array of objects based on multiple date fields, with the added complexity of excluding certain dates depending on the category. In order to optimize performance, I want to minimize the number of times the getUsefulJo ...

When selecting a new tab in HTML, the current page position remains unchanged. However, I would like the page to automatically scroll to the

In the design of my website, I have incorporated buttons that are linked to various pages using navigation tabs. However, when these buttons are clicked, the view maintains its position on the new page (e.g., halfway scrolled through the page). Instead o ...