Is there a way to position the input directly above the div in the center?

In my HTML code, I have a div container with an input and another nested div element that contains various other elements. My goal is to center the input element in relation to the parent div, where error messages are displayed.

Currently, both elements start at the same point on the left side of the screen.



.con {
    display:flex;
    justify-content: space-between;
}

<div class="con">
            
            <div>
              <input placeholder="Home team score" class="home-team-input" type="number"
                  [(ngModel)]="predictionService.predictionFormModel.home_team_predicted_points"
                  name="homeTeamPrediction" id="home-team" #homeTeamPredictionVal="ngModel" min="0" max="1000" required
                  pattern="^[1-1]?[0-9]$" />
                <div class="pr"
                  *ngIf="homeTeamPredictionVal.invalid && (homeTeamPredictionVal.dirty || homeTeamPredictionVal.touched)"
                  class="alert alert-danger absolute-home-team">
                  <div *ngIf="homeTeamPredictionVal.errors.required">This field is required.</div>
                  <div *ngIf="homeTeamPredictionVal.errors.pattern">The score prediction should be between 0-1000.</div>
                </div>
            </div>

            <div>
              <input placeholder="Away team score" class="away-team-input" type="number"
                [(ngModel)]="predictionService.predictionFormModel.away_team_predicted_points"
                name="awayTeamPrediction" id="away-team" #awayTeamPredictionVal="ngModel" min="0" max="1000" required
                pattern="^[1-1]?[0-9]$" />
              <div
                *ngIf="awayTeamPredictionVal.invalid && (awayTeamPredictionVal.dirty || awayTeamPredictionVal.touched)"
                class="alert alert-danger absolute-away-team">
                <div *ngIf="awayTeamPredictionVal.errors.required">This field is required.</div>
                <div *ngIf="awayTeamPredictionVal.errors.pattern">The score prediction should be from 0 until 1000.
                </div>
              </div>
            </div>

          </div>

Answer №1

To center the input in the div, simply add text-align: center;. If you want the input to take up all available space, add width: 100%. Here is the solution for your issue: https://stackblitz.com/edit/angular-ct3fzx

CSS :

.input-container{
  text-align: center;  // center the input in the div
}
.home-team-input{
  width: 100%; // set input's width to 100%
  margin-top:5px; // add some space
}
.away-team-input{
  width: 100%  // set input's width to 100%
  margin-top:5px; // add some space
}

HTML :

<div class="con">
            <div class="input-container">
              <input placeholder="Home team score" class="home-team-input" type="number"
                  [(ngModel)]="predictionService.predictionFormModel.home_team_predicted_points"
                  name="homeTeamPrediction" id="home-team" #homeTeamPredictionVal="ngModel" min="0" max="1000" required
                  pattern="^[1-1]?[0-9]$" />
                <div *ngIf="homeTeamPredictionVal.invalid && (homeTeamPredictionVal.dirty || homeTeamPredictionVal.touched)"
                  class="alert alert-danger absolute-home-team">
                  <div *ngIf="homeTeamPredictionVal.errors.required">This field is required.</div>
                  <div *ngIf="homeTeamPredictionVal.errors.pattern">The score prediction should be between 0-1000.</div>
                </div>
            </div >
               <div class="input-container">
                <input placeholder="Away team score" class="away-team-input" type="number"
                [(ngModel)]="predictionService.predictionFormModel.away_team_predicted_points"
                name="awayTeamPrediction" id="away-team" #awayTeamPredictionVal="ngModel" min="0" max="1000" required
                pattern="^[1-1]?[0-9]$" />
              <div
                *ngIf="awayTeamPredictionVal.invalid && (awayTeamPredictionVal.dirty || awayTeamPredictionVal.touched)"
                class="alert alert-danger absolute-away-team">
                <div *ngIf="awayTeamPredictionVal.errors.required">This field is required.</div>
                <div *ngIf="awayTeamPredictionVal.errors.pattern">The score prediction should be from 0 until 1000.
                </div>
              </div>
               </div>
          </div>

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

Looking for an image that spans the entire height of the container

I need help with positioning an img inside a div container that has a background color. I want the image to overlay on top of the div, extending beyond its height without causing any scroll bars. Here is a fiddle related to this issue: http://jsfiddle.net ...

Utilize ASP to limit the application of a CSS file exclusively to the child page

My website consists of a master page and several child pages. I have created specific CSS classes for styling. However, I only want certain child pages to utilize these styles while the master page should always use them. How can I limit the access to th ...

Angular's '@angular/core/core' module does not have the exported member 'ɵɵFactoryDeclaration'

My Angular application was successfully compiled after running npm install. However, upon executing npm start, I encountered the following error: ERROR in node_modules/ng-apexcharts/lib/chart/chart.component.d.ts:57:21 - error TS2694: Namespace '&quo ...

How can I fix the issue with Parallax scrolling not functioning properly? (Using Next.js Image + Tailwind)

What am I missing to get parallax scrolling to work? <div className="h-screen"> <div className="relative h-full w-full bg-cover bg-fixed bg-center bg-no-repeat"> <Image src="https://images.unsplash.com/photo-1454496522488-7a8e488e86 ...

javascript include new attribute adjustment

I am working with this JavaScript code snippet: <script> $('.tile').on('click', function () { $(".tile").addClass("flipOutX"); setTimeout(function(){ $(".tile-group.main").css({ marginLeft:"-40px", widt ...

Achieve hover overflow effect in HTML and CSS without altering element positions

Hey there, I could really use some help with a CSS and HTML issue I'm having. Take a look at the code snippet below: Here's what I'm trying to achieve: 1. Keep the boxes (.box) with images fixed in place 2. Make the hidden description (. ...

Update a variety of CSS properties simultaneously

Is it possible to change multiple CSS attributes of an element with just one line of code? Currently, if I want to alter various CSS attributes of an element, I have to write separate lines of code for each attribute. For instance: $("div#start").cli ...

CSS Accordion Collapse Widget

I currently have a CSS collapse accordion created using pure CSS, and it is functioning perfectly. However, I am facing one issue: When the user clicks on any label - Label One, Label Two, or Label Three, they are unable to close it by clicking on the sam ...

When attempting to access http://localhost:3000/traceur in Angular 2 with the angular-in-memory-web-api, a 404 (Not Found) error

Hello, I'm encountering an issue with angular-in-memory-web-api. I have attempted to use angular2-in-memory-web-api in SystemJS and other solutions, but without success. I am currently using the official quickstart template. Thank you for any assistan ...

Ways to make a button blink using AngularJS

Currently working on an AngularJS SPA application where a button is present in the front-end HTML page. When this button is clicked, it triggers an operation which takes some time to complete. I want to inform the user that the operation is still in prog ...

Legend alignment issue in Firefox disrupting Flexbox functionality

My fieldset has a legend containing text and a button. I am attempting to use flexbox to right-align the button within the legend. This setup works correctly in Chrome, but encounters issues in Firefox. I have managed to replicate the problem with a simpl ...

Incorporating Angular with a python PDF generator library

How can an external Python PDF generator library be integrated with Angular? Please provide a detailed explanation with example. ...

Automatically dismiss modal upon submission

In the process of implementing a CRUD operation, I am trying to automatically close the modal upon submission. Using data-dismiss on the submit button only closes the modal without executing the functionality. What I need is for the functionality to execut ...

Unable to utilize material tabs in this situation

Discovering the material tabs feature at https://material.angular.io/components/tabs/api#MatTab got me excited to implement it in my project. After adding the suggested import, I encountered an issue where I couldn't find the module "@angular/materia ...

C++ engine for embedding HTML, CSS, and scripts

I am working on a C++ Windows native application and I'm looking for an HTML engine to help with displaying data in the GUI. After some searching, I came across Sciter, which seems like a good fit. However, I've noticed that it lacks complete CS ...

Is this an effective and appropriate method for establishing media queries?

<link id ="style" rel="stylesheet" type="text/css" title="other" href="regularStyles.css" /> <link rel="stylesheet" media= "all and (mid-width: 767px) and (max-width:2049px) and (min-height:767px) and (max-height: 1538px)" href="notePads.css" /& ...

When text exceeds multiple lines, display an ellipsis to indicate overflow and wrap only at word boundaries

Here is an example snippet of my code: <div class="container"> <div class="item n1">Proe Schugaienz</div> <div class="item n2">Proe Schugaienz</div> </div> and I am using the following jQuery code: $(&apos ...

List layout enhancement provided by Bootstrap

I'm facing an issue with the layout of a feature list content box. While it looks good on desktop, there are problems in smaller versions like tablet and mobile devices. The four words with icons are breaking and I believe this might not be the best s ...

Angular problem arises when attempting to map an array and selectively push objects into another array based on a specific condition

Setting up a cashier screen and needing an addToCart function seems pretty simple, right? However, I am encountering a strange logical error. When I click on an item to add it to the cart, my function checks if the item already exists in the array. If it d ...

Despite passing the same dependency to other services, the dependencies in the constructor of an Angular2 Service are still undefined

To successfully integrate the "org-agents-service" into the "org-agents-component," I need to resolve some dependencies related to the required api-service. Other components and services in the hierarchy are also utilizing this api-service, which acts as a ...