Troubleshooting Bootstrap's Horizontal Alignment Problem

https://i.sstatic.net/3OTLd.pngWhile working on my Spring Boot Web App, I encountered a slight alignment issue on the login page. The problem, as shown in the attached image, is that the div is not perfectly centered on the page. I am utilizing Bootstrap for my project.

Although the element is almost centered, it seems to be slightly off to the left without a clear reason. I have included my HTML and relevant CSS below for reference. It appears that there might be some conflicting styles causing the misalignment, but pinpointing the exact issue is proving to be a challenge. https://i.sstatic.net/ccOZP.png

<div id="parentLogin">

    <div class="row" style="width: 40%;">
        
        <div class="col-md-12 col-md-offset-2">
        
            <div>
                <c:if test="${param.error != null}">
                    <div class="login-error" style="margin: 0 auto;">Incorrect username or password</div>
                </c:if>
            </div>
    
            <div class="panel panel-default">
    
                <div class="panel-heading">
                    <div class="panel-title" style="font-weight: bold; font-size: x-large; margin: 1%;">User Login</div>
                </div>
                
                <div class="panel-body">
    
                    <form method="post" action="${loginUrl}" class="login-form">
                    
                        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
    
                        <div class="input-group">
                            <input type="text" name="username" placeholder="Username"
                                class="form-control" />
                        </div>
    
    
                        <div class="input-group">
                            <input type="password" name="password" placeholder="Password"
                                class="form-control" />
                        </div>
    
                        
                        <button type="submit" class="suit_and_tie">Sign In</button>
                        
                    </form>
    
                </div>
    
            </div>
    
        </div>
        
    </div>
    
</div>
#parentLogin {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 75vh;
}

Answer №1

Your parent div with the id "parentLogin" is centered with a fixed width of 40% of the available width. However, it seems that the bootstrap panels inside this div are not centered within it. To fix this, you should add classes to all the child elements in the chain. Each class should utilize display: flex; and specify flex-direction, align-items, and justify-content properties. You can use Bootstrap classes for this, but make sure each element in the hierarchy specifies the alignment of its contents.

#parentLogin {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 75vh;
  width: 100%;
}

.parentRow {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  height: 75vh;
}

.parentCol {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 75vh;
}
<div id="parentLogin">
  <div class="row parentRow" style="width: 40%;">
    <div class="col-md-12 col-md-offset-2 parentCol">
      <div>
        <c:if test="${param.error != null}">
          <div class="login-error" style="margin: 0 auto;">
            Incorrect username or password
          </div>
        </c:if>
      </div>
      <div class="panel panel-default">
        <div class="panel-heading">
          <div class="panel-title" style="font-weight: bold; font-size: x-large; margin: 1%;">
            User Login
          </div>
        </div>
        <div class="panel-body">
          <form method="post" action="${loginUrl}" class="login-form">
            <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
            <div class="input-group">
              <input type="text" name="username" placeholder="Username" class="form-control" />
            </div>
            <div class="input-group">
              <input type="password" name="password" placeholder="Password" class="form-control" />
            </div>
            <button type="submit" class="suit_and_tie">
              Sign In
            </button>
          </form>
        </div>
      </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

Angular 10 Reactive Form - Controlling character limit in user input field

I'm currently developing an Angular 10 reactive form and I am looking for a way to restrict the maximum number of characters that a user can input into a specific field. Using the maxLength Validator doesn't prevent users from entering more chara ...

Creating layered images with CSS Grid

Visit this link for more details I've been attempting to create an overlap effect with 3 photos using CSS Grid. My desired outcome looks like this: Click here to see the desired result I followed tutorials that demonstrate the same method, but unfo ...

Explore the search bar with functional filtering for JSON items

I'm currently working on creating a dynamic filter bar integrated with a search functionality. My data is stored in JSON format, including details such as artist name, title, source, and more. Despite successfully retrieving results through console.lo ...

Design that is specifically tailored for mobile devices and excludes desktop compatibility

I am faced with the challenge of designing a website that is responsive on mobile devices while not being responsive on desktop computers. To address this issue, I have implemented a series of media queries based on specific breakpoints: 320px, 321-480px, ...

Ensuring various conditions with ngIf

I've created a toggle function that updates the text of a link, but I'm struggling to handle multiple conditions. For example, *ngIf="condition1 or condition2". Below is an excerpt from my code: <a routerLink="/ads" class="tip" (click)="togg ...

Setting the CSS property `overflow-y: scroll;` does not make the element scrollable and causes it to exceed 100% of the block

I am seeking a solution to create a y-axis scrollable block within the left-navbar. The height of this block should occupy all available space within the left-navbar, ensuring that the left-navbar itself remains at 100% of the page height. The issue arise ...

JavaScript Magic: Hide Div when Clicking Away

I need a solution where clicking outside of the My DIV with the ID Container_ID will hide all elements within the Container by setting their style to display: none;. Currently, the JavaScript code I am using partially achieves this functionality, but it al ...

Clicking on AngularJS ng-click to navigate to a different page within an Ionic framework application

My goal is to navigate to another page when clicking on a button in the Ionic navbar at the top. However, I am encountering an issue where upon clicking, all nav bar buttons disappear. Interestingly, using dummy codes triggers an alert successfully. But w ...

Which is better for creating a gradual moving background: Javascript or CSS?

I'm attempting to discover how to create a background image that scrolls at a slower pace than the page contents. I'm currently unsure of how to achieve this effect. A great example of what I'm aiming for can be seen here Would this require ...

Why does the container overflow when the child is floated in CSS?

Can you explain why a parent div element stops adjusting its height when its child is floated? Here is the HTML snippet: <!DOCTYPE html> <html> <head> <title>test</title> <link rel="stylesheet" type="text/css" href="in ...

Styling triangles within a CSS triangle

I'm attempting to design a webpage with a fixed triangle navigation element. The issue I am encountering is that I am unable to position smaller triangles inside the larger one, as shown in the image below. https://i.stack.imgur.com/1bTj8.png As th ...

jQuery class does not apply correctly in IE9

Currently, I'm designing a menu for a WordPress theme and facing a styling issue. I aim to differentiate between menu items with submenus and those without by applying different styles on hover. Here's the jQuery code snippet I am using: $(docum ...

Steps for generating a dropdown menu using API JSON information

I'm new to using Webapi with AngularJS. I have a URL that returns data in JSON format, and I want to extract only the employee names from the data and display them in a dropdown list using AngularJS. Any assistance would be greatly appreciated. ...

Activating list anchor upon click

I have a list that looks like this: <!-- List --> <ul class="nav nav-sm nav-tabs nav-vertical mb-4 steps-sampling"> <li class="nav-item"> <a class="nav-link active" id="link1" href="{{ ...

What is the best way to vertically align Angular Material tabs?

In my current project, I decided to use angular material and encountered a problem with the angular material tab. I wanted to arrange the tabs vertically, but it doesn't seem to be working properly. The alignment seems off and it's not functionin ...

image in the background following the header

Live Example: http://jsbin.com/opokev/54 I am currently trying to set this image as the background image while also including a header, but as you can see in the demo, the header is overlapping the image. Is there a way I can rearrange this so that the h ...

A guide on retrieving all the table data and displaying it in the user interface using templates in Django

I am working on a Django project where I have created a model named "Files" with fields such as Id (auto-generated) and file_name. My goal is to retrieve all file names from the database and display them as a list on the user interface. To achieve this, I ...

Uploading multiple files with unique IDs into a table

Currently, I am attempting to modify the code found at: This code functions perfectly with a single file input (and has been utilized in various projects without any issues). My specific requirement is to generate a table with a file input within each ro ...

Is it possible to globally modify the component reference <dropdown-component> name in Angular during runtime in a dynamic manner?

I am currently working on an application that utilizes a component called "dropdown-component" throughout its pages. However, due to specific business requirements, I have been tasked with replacing "dropdown-component" with "custom-dropdown-component". Un ...

Try utilizing the 'id name ends with' parameter within the on() function for creating dynamic bindings

I need help with dynamically binding the change event to multiple select input fields that are generated within my form. The item field is generated with IDs like: id="id_form-0-item" id="id_form-1-item" id="id_form-2-item" ... Although I have attempte ...