I need to find a way to position my login in the center of the page, even if my HTML body doesn't take up the entire space. This is

I am having trouble centering my login Component in the red zone on the page. It seems that my html and body tags are not taking up all the available space on the page. If you want to view my project, you can find it on my GitHub at https://github.com/SIGX-SENPAI/Angular-VRChat

LoginComponent.html

<form class="dropdown p-5 justify-content-center bg-danger">
  <div class="form-group">
    <label for="exampleDropdownFormEmail2">Email address</label>
    <input type="email" class="form-control" id="exampleDropdownFormEmail2" placeholder="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="debbb3bfb7b29ebba6bfb3aeb2bbf0bdb1b3">[email protected]</a>">
  </div>
  <div class="form-group">
    <label for="exampleDropdownFormPassword2">Password</label>
    <input type="password" class="form-control" id="exampleDropdownFormPassword2" placeholder="Password">
  </div>
  <div class="form-group">
    <div class="form-check">
      <input type="checkbox" class="form-check-input" id="dropdownCheck2">
      <label class="form-check-label" for="dropdownCheck2">
        Remember me
      </label>
    </div>
  </div>
  <button type="submit" class="btn btn-primary">Sign in</button>
</form>

AppComponent.html


<app-navbar *ngIf="login" (showNav)="login = $event" (showOut)="local = $event"></app-navbar>

<div class="container" *ngIf="!local">
  <router-outlet></router-outlet>
</div>

<div *ngIf="local">

  <div class="pt-2 pb-2">
    <app-promo></app-promo>
  </div>

  <div class="container">
    <app-home></app-home>
  </div>
  <app-features></app-features>
  <app-footer></app-footer>
  </div>

enter image description here enter image description here

The blue shadow represents the html body.

enter image description here

Answer №1

Did you attempt to set both the body and html to a height of 100%?

html, body {
  height: 100%;
}

Answer №2

  • It seems like you're utilizing bootstrapp.css
  • You can skip using justify-content-center if the element is already displaying flex d-flex

Consider these options:

Enclose the button within another form-group and center it inside that wrapper

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<form class="dropdown p-5 bg-danger">
  <div class="form-group">
    <label for="exampleDropdownFormEmail2">Email address</label>
    <input type="email" class="form-control" id="exampleDropdownFormEmail2" placeholder="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f89d95999194b89d80999588994a979695">[email protected]</a>">
  </div>
  <div class="form-group">
    <label for="exampleDropdownFormPassword2">Password</label>
    <input type="password" class="form-control" id="exampleDropdownFormPassword2" placeholder="Password">
  </div>
  <div class="form-group">
    <div class="form-check">
      <input type="checkbox" class="form-check-input" id="dropdownCheck2">
      <label class="form-check-label" for="dropdownCheck2">
        Remember me
      </label>
    </div>
  </div>
  <div class="form-group text-center">
    <button type="submit" class="btn btn-primary m-auto d-inline-block">Sign in</button>
  </div>
</form>

You can align all text in form element to the center, but align labels to the left within form-groups

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<form class="dropdown text-center p-5 bg-danger">
  <div class="form-group text-left">
    <label for="exampleDropdownFormEmail2">Email address</label>
    <input type="email" class="form-control" id="exampleDropdownFormEmail2" placeholder="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a5f575b53567a5f425b574a565f14595557">[email protected]</a>">
  </div>
  <div class="form-group text-left">
    <label for="exampleDropdownFormPassword2">Password</label>
    <input type="password" class="form-control" id="exampleDropdownFormPassword2" placeholder="Password">
  </div>
  <div class="form-group text-left">
    <div class="form-check">
      <input type="checkbox" class="form-check-input" id="dropdownCheck2">
      <label class="form-check-label" for="dropdownCheck2">
        Remember me
      </label>
    </div>
  </div>
  <button type="submit" class="btn btn-primary m-auto d-inline-block">Sign in</button>
</form>

Answer №3

To center an element on the screen, consider using the padding-top attribute with a percentage value that aligns it to the middle.

.Box /*You can replace this with any class or id you prefer*/ {
padding-top: 20%; 
}

This method may not be the most conventional, but it has been effective for me in achieving the desired result.

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

button behavior based on the currently visible section

Hey there, I'm new to this! I usually come here to find solutions, but I'm stuck right now. I have a single-page website with different sections. I've created a function to switch between these sections, showing one at a time and hiding the ...

Encountering issues when attempting to activate npm start command in Ubuntu operating system

Recently delving into the world of Angular2, I've been working on setting up an AngularJS 2.0 project on Ubuntu. I made sure to install npm in the root directory of my Angular2 app. However, when I attempt to run npm start, I encounter the following e ...

Stop bullet points from overlapping when using a floating left image in Internet Explorer 9 and above

Having trouble with bullets displaying in IE9+? Check out this link for more information: I've attempted to fix the issue by creating an ie.css file using conditional comments, but it hasn't worked. Can anyone provide assistance? ...

Eliminate an item from an array of objects utilizing a specific key

Here is the JSON data provided: var data= { 'A' : { 'Total' : 123, 'Cricket' : 76, 'Football' : 12, 'Hockey' : 1, 'None' : 10 }, 'B&ap ...

The image has max-height within a max-height container inside a fixed-height container

Here's the scenario: Can an image's max-height property be made to respect the fixed height of an ancestor element that is not its direct parent? I am aware that max-height requires a fixed height for calculation, so this query is distinct from ...

How come "display:none" is still occupying space on the page?

Trying to figure out how to make a specific cell in a table disappear when viewing an HTML page on mobile devices, while having a width of 20% for desktop. However, the issue is that even though the table cell is invisible on mobile, it still occupies sp ...

Obtain the current date using Moment JS in JavaScript

Here is a scenario with code : let currentTime = moment(); console.log(currentTime.format()); // 2019-11-25T20:23:50+02:00 console.log(currentTime.toDate()); // 2019-11-25T18:23:50.916Z After applying the timezone change on Heroku using the command ...

Addressing component validation conflicts in Vuelidate on VUE 3

I am currently experiencing an issue with VUE 3 Vuelidate. In my project, I have 2 components that each use Vuelidate for validation (specifically a list with CRUD functionality implemented using modals). However, when I navigate from one component to anot ...

Identify all anchor elements that contain image elements

I am on the hunt. I am looking for a CSS-Selector, but if that's not an option, a jQuery selector would work too. ...

Tips on hiding the menu toggler in PrimeNg

Struggling with PrimeNg implementation in my Angular project, I am unable to find a simple solution to hide the menu toggler. Allow me to illustrate my current setup in the first image and what I desire in the second. Let's dive in: View First Image ...

Integrate service once the constructor has completed execution

I created a service to connect with the Spotify API. When initializing this service in the constructor, it needs to retrieve the access token by subscribing to an Observable. However, there's an issue: The service is injected into my main component ...

Is there a way to retrieve text from within a div using code behind in asp.net with c#?

I am currently developing a SQL QUERY editor where users can enter queries. The entered text is then passed to a SQL COMMAND for execution, and the results are displayed to the user in a GRIDVIEW. However, I am facing an issue in retrieving the text entere ...

What is the best approach to link an HTML document to a MySQL Workbench server utilizing JavaScript, Ajax, and PHP?

On my HTML page, users can enter the name of a movie and if it is found in the database, the name will be displayed. I am attempting to establish a connection to the MySQL Workbench Server using JavaScript, Ajax, and PHP. This is what I have implemented s ...

What is the best way to show only a specific v-for element in Vue.js?

Is there a way to select a specific item from a v-for loop in vue js? I am using v-for to retrieve Youtube data API items. Each item contains an iframe that should only be displayed when a user clicks on a play button. Currently, all the iframes are shown ...

The hover effect in CSS animations is turned up too high

Check out my Reddit news feed design: https://codepen.io/Teeke/pen/YxXXaE When hovering over articles, the headlines get displayed. But if the article text is too long, part of the headline may go off-screen. Instead of changing the line-height, I' ...

What is the best way to display information from a Django model using a React frontend?

Currently, I am in the process of developing a personal portfolio website using Django for the backend and React for the frontend components. Within this project, I have set up Django tables to store my education history, work experiences, skills, and port ...

The variable (form.onsubmit) remains unset even after assigning a value

function setFormOnSubmit(formName){ if(!formName) return; var form = document[formName]; form.onsubmit = function(){ console.log('This is the onsubmit function'); }; console.log('onsubmit successfully set ...

Having trouble getting the toggleClass function to work properly?

I'm facing an issue with some straightforward code that I've written. $(function() { $("#tren").click(function() { $("#trens").toggleClass("show"); }); }); .show { color: "red"; } <ul> <li id="tren">Some text</li> < ...

Preview multiple images while uploading in a React JS application

In order to achieve multiple image upload with preview using React JS, I attempted the code below. However, I encountered an error in the console: Uncaught DOMException: Failed to set the 'value' property on 'HTMLInputElement': This ...

Set the scroll position to the top left corner (0,0) when a link to the current route is clicked or pressed

In my React application, I am faced with a challenge. When the user is on the route /gallery/images and clicks on a link (such as <a> or <Link> from react-router-dom) that leads to the same route currently being displayed, how can I detect thes ...