What is the best way to center text within an input field?

I've been attempting to center the text in the span tag within the input field, but using "text-align": center in my css doesn't seem to be working as expected.

Interestingly, when I switched the span tag to a paragraph tag, it caused the input field to expand.

This is the code snippet:

<div class="nxui-form-group">
    <label for="external-realisation">
      <img src="assets/images/purchase_order.svg" class="nxui-icon-small nxui-icon-align-bottom">
      {{ 'i18n.all-damage-reports.label.external-realisation' | translate }}
    </label>
    <div *ngIf="!isExternal">
      {{ 'i18n.all-damage-reports.label.without-order' | translate }}
    </div>
    <div *ngIf="isExternal" class="nxui-label-plus-field">
      <span class="nxui-non-breakable-label">{{ 'i18n.all-damage-reports.label.with-order' | translate }}&nbsp;</span>
      <input [nxuiPlaceholder]="'i18n.all-damage-reports.label.external-realisation' | translate"
             [title]="'i18n.all-damage-reports.label.external-realisation' | translate"
             class="nxui-form-control"
             formControlName="company"
             id="external-realisation"
             pInputText
             >
    </div>
  </div>

Adding the paragraph tag shifts the layout like this: https://i.sstatic.net/2UPo6.png

With the span tag: https://i.sstatic.net/VPU7h.png

Solution to my query:

.nxui-label-plus-field {
  display: flex;
  align-items: center;
}

.nxui-non-breakable-label {
  white-space:nowrap ;
}

Any help or suggestions would be greatly appreciated.

Thank you.

Answer №1

CODE EXAMPLE:

<div *ngIf="isExternal" class="custom-label-field">
  <span class="non-breakable-label">{{ 'i18n.all-damage-reports.label.with-order' | translate }}&nbsp;</span>
  <input [nxuiPlaceholder]="'i18n.all-damage-reports.label.external-realisation' | translate"
         [title]="'i18n.all-damage-reports.label.external-realisation' | translate"
         class="custom-form-control"
         formControlName="company"
         id="external-realisation"
         pInputText
         >
</div>

CSS:

<style>
   .custom-label-field { 
      display: flex;
      align-items: center;
    }
</style>

Answer №2

Make sure to include the style attribute with "text-align:center" in your input element as shown below:

 <input [nxuiPlaceholder]="'i18n.all-damage-reports.label.external-realisation' | translate"
             [title]="'i18n.all-damage-reports.label.external-realisation' | translate"
             class="nxui-form-control"
             formControlName="company"
             id="external-realisation"
             pInputText
             style="text-align:center"
             >

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

Guide on downloading Node modules to an Angular 2 project

I am trying to install a Git project located in the directory D:/myapp. I currently have npm version 3.10.10 and Node.js version 6.11 installed on my Windows machine. However, when I attempt to install all dependent node modules using the 'install npm ...

Is there a way to left-align these items?

I currently have 10 divs arranged side by side. I am trying to align the last divs to the left instead of center, but the solutions I found so far are not working. I've checked Bootstrap's documentation and tried using <div class="d-flex ...

Personalizing HTML input fields

I am looking to create a contact form that will display an image depending on whether the input entered is correct or incorrect. My goal is to achieve this using only HTML and CSS, without needing any JavaScript. While there are similar posts on this topic ...

What is the proper location to insert CSS within Laravel?

What is the recommended location for storing CSS or JS files in Laravel? More specifically, what are the differences between placing CSS files in /public versus /resources/css? Which directory is preferable for storing my CSS files? ...

Center Items in Flexbox, Implementing React Material with React

In my React App, I am utilizing flexbox and React Material to align the searchbar and clear button on the same line with space between them. Here is the link for reference: CLICK HERE <div className={classes.searchBarSection}> <Paper com ...

The css values for component _nghost-c0 in an Angular application

Recently, I've been delving into Angular 5 and couldn't help but notice the peculiar html tags with ng generated attributes like _nghost-c0 and _nghost-c1... This got me wondering, what exactly do these attributes signify? [_nghost-c3] .employee ...

The issue with Angular 2's Ng style is that it does not properly remove the old style when there is a change

I am currently experiencing an issue within my project. When assigning an object to ngStyle in a div, the problem arises where ngStyle does not clear the style properties from the previous object when there is a change in the object. It should ideally rem ...

Visualizing Data with HTML and CSS Chart Designs

I'm seeking assistance with creating an organization tree in a specific layout. https://i.sstatic.net/z9LRc.jpg I am having trouble replicating the tree structure shown in the image. I need help generating a similar tree structure where multiple le ...

Getting the checkbox count value in a treeview upon successful completion of an AJAX request

After successful JSON Ajax response, a treeview structure with checkboxes is displayed. I need to capture the number of checkboxes checked when the save button is clicked. @model MedeilMVC_CLOUD.Models.UserView <script type="text/javascript"> ...

The default value of the input color in HTML/NextJS does not update when changed

Issue: The color input does not change when the default value (#ffffff) is declared. https://i.sstatic.net/BpqUj.png However, it works normally if I don't declare a default value. https://i.sstatic.net/0dBd6.png Note: Using NextJS framework. <i ...

Is a special *ngFor required when implementing Angular2 with Nativescript?

My current project involves creating a mobile app that requires listing a few labels. Here is the code snippet: @Component({ selector: 'code-m', template: ` <StackLayout dock="top" orientation="vertical" style="height: ...

Automated Validation Grouping in Angular Reactive Forms Based on Control Properties

After implementing the following code block, I am uncertain if this is the correct behavior: this.formGroup = this.fb.group({ name: this.fb.group({ firstName: ['', Validators.required], lastName: ['', Validators.required] ...

Achieve a centered content with a stretched background using Bootstrap 5

Having some trouble with aligning items in a div using align-items-stretch to stretch the background and align-items-center to vertically center the content. It seems that the alignment issue occurs when there's a very long title present. https://i.s ...

Create a dynamic HTML design with a resizable left panel and a static right panel. The left panel should not wrap text and display ellipsis if necessary

I am looking to display items in a list with a specific format: |Name | Some other val1| |Very very long no wrap line that...| Some other val2| The right side needs to have a fixed width. The left side should fill al ...

When attempting to retrieve the innerHTML of a <div> element within a <Script> tag, the value returned is undefined

Utilizing a masterpage to create a consistent header across all content pages, I encountered an issue on one page. I needed to retrieve the innerHTML of a div element and pass it to an input control on the same page in order to access the updated innerHTML ...

Why isn't it possible to send POST data to a JSON file using JQuery/AJAX?

I'm currently learning how to use JQuery/Ajax from a helpful tutorial on YouTube. To watch the video, simply click here. While I can successfully retrieve data from the order.json file, I encounter an error whenever trying to send POST requests. Bel ...

The functionality of item.classList.toggle() in HTML+CSS+JS fails to execute

I have been working on a program that aims to flip a card when it is clicked. The JavaScript code I have written for this functionality is as follows: /* Card flipping onclick */ import "./Stylesheets/FlipCardStyle.css" var cards = document.quer ...

What is the reason behind Facebook's use of margin-left: -9999px to conceal elements?

While experimenting with firebug on Facebook's stream, I stumbled upon an interesting discovery regarding the style of the "update options" menu. It appears that they are hiding this menu by using margin-left: -9999px, and displaying it by overriding ...

Displaying or concealing HTML elements using AngularJS while a modal is open

Looking for a way to display a loading spinner icon on my page when a user triggers a button that opens a modal, and then have the spinner disappear once the modal is open and its content has loaded. Currently, I've managed to make the spinner show up ...

Adjusting the dimensions of the canvas leads to a loss of sharpness

When I click to change the size of the graph for a better view of my data in the PDF, the canvas element becomes blurry and fuzzy. Even though I am using $('canvas').css("width","811"); to resize the canvas, it still results in a blurry graph. I ...