Tips for altering text size within Angular Material form fields with floating placeholder

Looking to customize the font size of the placeholder text in an Angular Material form field? You can set two different font sizes: one for when the placeholder is normal and one for when it floats. Here's how you can achieve this:

 <mat-form-field class="form-group example-full-width">
    <input matInput id="Fname" placeholder="First" class=" " formControlName="FirstName">
    <mat-error *ngIf="firstName.invalid" class="">First name is required</mat-error>
    </mat-form-field>

Answer №1

Using a floating text as a label allows for easy css targeting. Here's a potential solution:

.custom-label {
  font-size: 18px;
}

Adjusting the label when it floats:

.custom-label.float {
  font-size: 12px;
}

Answer №2

The solution provided earlier is no longer effective. Here is the updated approach I used:

.mat-form-field-can-float.mat-form-field-should-float .mat-form-field-label,
.mat-form-field-can-float .mat-input-server:focus + .mat-form-field-label-wrapper .mat-form-field-label {
  transform: translateY(-1.34375em) scale(1) !important; /* originally scale(0.75) */
}

Answer №3

This method has been successful for me, you might want to give it a try:

HTML

<div>
    <mat-form-field>
        <mat-label>Email Address</mat-label>
        <input matInput placeholder="Ex. <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c79647d716c70795c79647d716c7079327f7371">[email protected]</a>">
    </mat-form-field>
</div>

SCSS

.mat-form-field{
    font-size: 17px;
    width: 280px;
    &.mat-focused{
        font-size: 20px;
    }
    .mat-input-element{
        width: 100%;
        font-size: 14px;
    }
    &::placeholder{
        font-size: 14px;
    }
 }

.mat-form-field-should-float {    
  font-size: 20px;
}

Answer №4

Check out the Form Field section in the official Angular Material documentation (since version 5) for a recommendation on adjusting the font size. Here's what they suggest:

The font size of mat-form-field is inherited from its parent element. You can override this by using CSS to set an explicit size. It is recommended to have at least 1 element + 1 class specificity.

mat-form-field.mat-form-field {
  font-size: 16px;
}

You can also adjust the font size of the label in the form field:

mat-label.mat-label {
  font-size: 16px;
}

Alternatively, if you prefer keeping your styles neat and organized, you can create a simple CSS class to apply new font properties:

.html

`<mat-form-field class="new-form-theming">..`.

.css

.new-form-theming {
  font-size: 24px;
  line-height: 28px;
  font-weight: 500;
}

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

Building an array from scratch in Angular

Below is the URL to access the code: https://stackblitz.com/edit/ng-zorro-antd-start-xz4c93 Inquiring about creating a new array. For example, upon clicking the submit button, the desired output should resemble the following structure: "tasks": [ { ...

Adjusting grids in Bootstrap according to device orientation

I have a vision for a webpage layout that will feature two columns in landscape mode and switch to one column in portrait mode. Here is an outline of what I have in mind: Landscape: <body> <div class="container-fluid"> <div cl ...

Angular 2: Assigning a class to a D3 element using the component's style

When creating a component in Angular 2, the `app.component.css` file defines a class called `h-bar`: https://i.sstatic.net/AG1ER.png In the `app.component.ts` file, d3 is utilized to create elements that should apply the `h-bar` class from the `app.compo ...

Using an alias to call a function defined in a separate module in TypeScript

The following code snippet is from the v4.js file located inside the uuid folder within Angular's node_modules: var rng = require('./lib/rng'); var bytesToUuid = require('./lib/bytesToUuid'); function v4(options, buf, offset) { ...

What are the implications of utilizing a query string in a POST request?

In our system, POST requests are sent from the frontend to the backend. Instead of using the body to pass data to the server, these requests utilize query strings in the URL params. It is important to note that these requests only contain string parameter ...

Displaying the count of items outside the Angular 2 ngFor loop

As a beginner in Angular, I've encountered a small issue. How can I display the actual number of items outside of an *ngFor loop? Currently, I'm using filter and pagination pipes in this manner: *ngFor="let item of items | filterBy: ['name ...

Using TypeScript with Angular: encountering a ReferenceError stating that the System object is not defined in the System

I attempted to follow a tutorial to set up Angular 2 with TypeScript from the following link: https://angular.io/guide/quickstart However, I encountered the following error: ReferenceError: System is not defined System.config I am uncertain why this e ...

angular table cell with a show more/less button

I'm trying to create a button that can hide/unhide text in a table cell if the length is greater than a certain number. However, the current implementation is not working as expected. The button ends up opening all texts in every cell, and it only wor ...

Angular causing WKWebview to freeze

Situation Our Angular+Bootstrap app is functioning smoothly in Desktop on all operating systems, as well as Android and iPhone devices. There are no visible JavaScript errors or CSS warnings. Dilemma However, an issue arises intermittently while using t ...

Link causing chaos in CSS animation

I've created a CSS animated banner. You can view the result here. However, I'm facing an issue where adding a hyperlink to some of the pictures is causing the animation to mess up. The first picture has a href attribute, making it smaller and not ...

Having difficulty adjusting the size of the ng-image-slider

I am facing some challenges in reducing the size of my video images. I have tried using the following link: https://stackblitz.com/edit/angular-jgtecl?file=src%2Fapp%2Fapp.component.html, as well as this slider: https://github.com/sanjayV/ng-image-slider ...

Bootstrap implementation for personalized notifications

I am utilizing bootstrap notifications to display important messages to my users. Within my code, there is a DIV element named <div class="notifications bottom-right"></div> Theoretically, the library should be handling the JavaScript. I ha ...

Utilizing ellipses within a list item to create a visually appealing design

I've been struggling with this problem for hours, but I can't seem to find a solution. How can I ensure that the text in a list item respects the size of its parent container and uses an ellipsis when necessary? Here's the scenario: <?P ...

Implementing Jquery Table Selection with Custom CSS Class

When I attempted to use the selectable feature on a table with a CSS class applied, the selection did not work. However, when I removed the CSS class, the selection worked perfectly fine. I suspect that the issue lies within the CSS definition, but I' ...

Utilizing Compass SCSS with Blueprint for Optimal Overflow Settings

I've been working with blueprint through Compass, but I'm facing an issue where longer pages don't have scroll bars. It seems like the default setting for overflow is 'hidden'. Since this appears to be the standard, I assume there ...

Using JavaScript to show a prompt message inside an h1 tag within a newly created div

I have developed a basic JavaScript program that opens a prompt dialog when the div tag is clicked, allowing the user to enter text. The program then creates a new div element and displays the entered text above it. However, I am facing an issue where I wa ...

Guide to sending back an Observable within Angular 4

Inside my authProvider provider class, I have the following method: retrieveUser() { return this.afAuth.authState.subscribe(user => { return user; }); } I am looking to subscribe to this method in a different class. Here is an example ...

Best practice for showcasing an MVC Form with the use of Bootstrap

I am facing an issue with my form layout while trying to capture user input. The use of Bootstrap is creating two columns for text boxes and their labels, but the alignment goes off track with the last EditorFor element. I'm questioning if I am misusi ...

Transitioning from TSLint to ESLint in Angular across several projects leads to ng lint command freezing

After successfully migrating one of my Angular 10 apps using a single project from TSLint to ESLint, following the steps outlined by Angular ESLint, I encountered a problem when trying to migrate an Angular 10 app with multiple projects located in the &apo ...

Is your text appearing vertically in Internet Explorer?

I'm having an issue with my small single page website where the text in a form is displaying vertically instead of horizontally in Internet Explorer. The problematic part is related to "vehicle condition." Any suggestions on what I might be doing wron ...