Shifting the placement of a material tag widget

I came across a problem while trying to position a block of text using an HTML Label element. The label is supposed to be at the center, but it aligns at the bottom of the input control instead. Here's a screenshot for better understanding: https://i.sstatic.net/YL04v.png

The "Trader Reference [07]" text seems too low and I have attempted various methods to adjust it by a few pixels without success.

It's worth mentioning that I'm utilizing Angular Material controls in this scenario.

Here is a snippet of the code:

<div class = "wrapper">
  <div>
  <mat-accordion>
  <mat-expansion-panel (opened)="panelOpenState = true"
                       (closed)="panelOpenState = false">
    <mat-expansion-panel-header [collapsedHeight]="customCollapsedHeight" [expandedHeight]="customExpandedHeight">
      <mat-panel-title>
     <h4> Declaration Type</h4>
      </mat-panel-title>
    </mat-expansion-panel-header>
    <div class="field">
        <label class= "field-label">Decln Type [01]:
            <mat-form-field>
                <mat-select class="declaration-type-combobox">
                  <mat-option *ngFor="let declarationType of declarationTypes" [value]="declarationType.value">
                    {{declarationType.value}}
                  </mat-option>
                </mat-select>
              </mat-form-field>
        </label>
        <label class= "field-label">Badge Codes:
          <mat-form-field>
              <mat-select class="badge-codes-combobox">
                <mat-option *ngFor="let badge of badges" [value]="badge.code">
                  {{badge.code}} -  {{badge.name}}
                </mat-option>
              </mat-select>
            </mat-form-field>
      </label>
    </div>
    <div class="field">
        <div>
            <label>Trader Referecne [07]:
            <mat-form-field>
                <input matInput>
              </mat-form-field>
            </label>
          </div>
  </div>
    </mat-expansion-panel>
</mat-accordion>
</div> <!--End wrapper-->
</div>

Also, here's the associated CSS:

 .wrapper {
  margin-top: 30px;
  margin-left: 30px;
}

/* contains a label and a value */
.field {
  display: flex;
  margin: 0 0 0 0;
}

.field-label {
  width: 35%;
}

.field-value {
  width: 60%;
  float: right;
}

.field label + label /*label after another label */
 {
  margin-left: 30px;
}

.declaration-type-combobox {
  width: 400px;
  max-width: 400px;
  font-size: 12px;
  border: #673ab7 2px solid;
  border-radius: 5px;
  height: 10px;
  padding: 9px;
}

.badge-codes-combobox {
  width: 200px;
  max-width: 200px;
  font-size: 12px;
  border: #673ab7 2px solid;
  border-radius: 5px;
  height: 10px;
  padding: 9px;
}

.form-field {
  width: 380px;
  max-width: 380px;
  border: #673ab7 2px solid;
  border-radius: 5px;
  font-size: 12px;
  height: 20px;
  max-height: 20px;
}

::ng-deep .mat-select-panel mat-option.mat-option {
  height: unset;
}

::ng-deep .mat-option-text.mat-option-text {
  word-wrap: break-word;
  white-space: pre-line;
  padding: 5px;
  margin: 2px;
  line-height: 15px;
}

::ng-deep .mat-form-field-underline {
  display: none;
}

.mat-expansion-panel-header.mat-expanded {
  background: #673ab7;
}

input {
  border: none;
  background: none;
  padding: 0;
  text-align: left;
  font-size: 12px;
  border: #673ab7 2px solid;
  border-radius: 5px;
  height: 9px;
  padding: 9px;
  width: 200px;
}

Answer №1

The issue with the alignment is most likely caused by the matInput directive assigned to the input element. Removing this directive from the input should fix the alignment problem. It seems that the mat-input-element class, which is applied to the input, is responsible for the issue. By removing this class in the developer tools, the problem goes away.

  • Despite trying to override every style within the mat-input-element class, it's unclear what specific aspect of the class is causing the problem. Without delving into the source code of the library, it's challenging to pinpoint the exact root cause of the issue.

To potentially address the problem, you could consider disconnecting your label from the matInput directive as an alternative approach.

<div class="field">
        <pre style="padding-top:8px; font: 400 14px/20px Roboto,Helvetica Neue,sans-serif;">Trader Referecne [07]: </pre>
        <div>
            <mat-form-field>
                <input matInput>
              </mat-form-field>
          </div>
  </div>

Answer №2

Although this issue may be considered old, I recently discovered in Angular 16 that the positioning of the label is influenced by the width of

<div class="mdc-notched-outline__leading"></div>
before the label. You can adjust the width by overriding the class with the use of !important. As an example, I implemented the following:

.mdc-notched-outline__leading {
    width: calc(40% - 8px) !important;
}

This resulted in: https://i.sstatic.net/RRLbB.png

The issue arises when the input field is empty, as the placeholder occupies the same space: https://i.sstatic.net/epUU9.png

https://i.sstatic.net/BrlUR.png

In my opinion, utilizing JS/TS instead of solely relying on CSS/SCSS could offer easier management even with empty input fields.

I hope this information proves to be helpful!

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

Is there a way to execute a javascript function that is located outside of my Angular application without having to import it?

I need to be able to trigger a JavaScript function that is located outside of my Angular app when a button is clicked. Unfortunately, it seems that importing the JavaScript directly into my Angular app isn't feasible for this task. The platform I am ...

Is it possible to manipulate a modal within a controller by utilizing a certain attribute in HTML, based on specific conditions (without relying on any bootstrap services), using AngularJS?

Within my application, I have a modal that is triggered by clicking on a button (ng-click) based on certain conditions. Here is the HTML code: <button type="button" class="btn btn-outlined" ng-click="vm.change()" data-modal-target="#add-save-all-alert ...

Inject a dynamic URL parameter into an iframe without the need for server-side scripting

I'm really stuck and could use some assistance with the following issue, as I am unable to solve it on my own :( When a user is redirected to a form (provided via an iframe), there is a dynamic URL involved: website.com/form?id=123 The code resp ...

Add the directive prior to rendering the HTML

I attempted to comprehend the problem below: My html string is rendered using ng-bind-html I successfully replaced a specific placeholder in the html string with a directive (or multiple). For example, I changed [placeholder]test[/placeholder] to <my- ...

Is there a way to submit the value of a textbox that has been generated dynamically using jQuery?

I am currently using the CodeIgniter framework and am attempting to incorporate textboxes in an HTML form. When the user clicks a button, a new textbox should be generated automatically using jQuery. However, when I submit the form, the values of the dynam ...

Unable to retrieve HTML code with PHP's file_get_contents function

I have been trying to retrieve the HTML content of a webpage using the code below: $url = "http://mysmallwebpage.com/"; $html = file_get_contents($url); Unfortunately, it seems that the file_get_contents function is unable to open URLs in certain formats ...

What is the best way to activate a function once two HTML elements have been completed?

In my HTML code, I have integrated a datepicker and a select element. Currently, there is a button linked to the method checkAvailability() <div class ="d-flex flex-row bd-highlight mb-3"> <div class = "form-froup row"> ...

Pager.js utilizing Deferred Bindings

I am currently working on using Pager.js to develop a single page application. The structure I have set up is as follows: #word/definition #word/example #word/synonym This means that definition, example, and other elements are divs with page bindings: & ...

NgRx Action Payload fails to trigger Effect, but no error messages are generated

I've exhausted all resources on Stack Overflow and still can't seem to figure this out. The issue lies in passing a payload into the 'GetUser' action. My intention is for this payload to go through the effect, and eventually be sent v ...

Issues with CSS arising from conflicts in MUI alert component styling

My experience with Material UI 5.10.11 has been mostly positive, but I have encountered some issues with the MuiAlert component. When setting severity to 'error', it should look like this https://i.sstatic.net/A1KIE.png However, on certain pages ...

Looking for a way to create a scrollable section in a designated spot?

I've been struggling with this issue for a few days and finally decided to seek some assistance. My goal is to have a scrollable box at a specific location on my website containing an image. Below is the code I currently have: <html> < ...

Modifying the HTML code to include extra spacing can completely alter the design and

I'm struggling to find the right words for this question title. I created a website using Webflow and after exporting the code, I encountered a peculiar issue. I can't seem to identify what the problem might be. When the HTML code is compressed ...

Error: Trying to access 'MaterialModule' before it has been initialized results in an uncaught ReferenceError

I have been working on a form for a personal project and attempted to implement a phone number input using this example: . However, after trying to integrate it into my project, I encountered an error. Even after removing the phone number input code, the e ...

What's the best way to target an element that is outside a certain parent container, but based on the parent's class?

Looking to target specific divs with a data-role of content that are not nested within a data-role="page" div with the class "modal". Advanced CSS selectors are not my forte. <div> <div data-role="page"> <div data-role="content" ...

"Access denied: Resteasy and ajax do not support this method (error

Encountering a login problem, although registration is functioning smoothly. Tech stack - HTML/AJAX/JQuery->Resteasy (Wildfly 10.1.0 Final) The URL - https://www.test.com/login.html - (actual URL name altered) Upon submitting the form with method type ...

What is the best approach to using two controllers simultaneously?

In my webpage, there is a left sidebar with its own controller running in the background. On the right side, I have a view of states that are dependent on the left side (a classic ui-view scenario). I am looking to update variables in the sidebar's co ...

Is it possible to instruct Vue to prioritize the inherited class over others?

I have a situation in my component where I need Vue to disregard the existing class and instead use the inherited class, but only if it is of the same type as the inherited class. Let me illustrate with an example: Consider a button component like this M ...

The JavaScript function document.getElementById.onclick is not functioning as expected

One issue I am facing involves counting all downloads on my website. My current approach is to increment a value in my database each time a download button is clicked, and then display this value. Despite trying multiple methods, the download count always ...

Angular2 is designed to break down complex applications into smaller, more manageable parts

Need for a Solution Recently, I was given responsibility of overseeing a large, legacy web application at work that involves multiple scrum teams and development teams. One major issue we face with this application is that whenever one team makes updates ...

What are the steps to save an XML file into a MySQL database and later access it?

An XML file has been automatically generated from the code snippet provided below. if (isset($_POST["song"]) && $_POST['song'] != "") { $song = $_POST["song"]; } else { $song = array(); } $dom = new DOMDocument("1.0"); $root = $dom-> ...