How To Center The Login Page In Html

How To Articles

Have you ever come across a login page on a website that was not centered properly? It can be frustrating to have to scroll or resize the page just to find the login form. As a web developer, I understand the importance of creating a user-friendly experience, which includes having a centered login page.

In this article, I will walk you through the steps on how to center a login page in HTML. By the end of this tutorial, you’ll have the knowledge and skills to ensure that your login page is perfectly centered, providing a seamless experience for your users.

Step 1: HTML Structure

First, let’s set up the basic HTML structure for our login page. We’ll create a simple form with an input field for the username and password, as well as a submit button. Here’s the code:

<form class="login-form">
<input type="text" placeholder="Username"><br>
<input type="password" placeholder="Password"><br>
<button type="submit">Login</button>

Step 2: CSS Styling

Now that we have our HTML structure in place, let’s add some CSS to center the login form. We’ll use a combination of flexbox and CSS positioning to achieve the desired result. Here’s the CSS code:

.login-form {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: 300px;
height: 200px;

Let’s break down what each CSS property does:

  • display: flex; – This property sets the container to use flexbox layout.
  • flex-direction: column; – This property specifies that the flex items should be laid out vertically in a column.
  • align-items: center; – This property centers the flex items horizontally within the container.
  • justify-content: center; – This property centers the flex items vertically within the container.
  • position: absolute; – This property positions the login form absolutely within its parent container.
  • top: 0;, bottom: 0;, left: 0;, right: 0; – These properties position the login form at the center of the parent container.
  • margin: auto; – This property centers the login form by setting equal margins on all sides.
  • width: 300px; – This property sets the width of the login form.
  • height: 200px; – This property sets the height of the login form.

Step 3: Testing and Adjustments

Once you’ve added the CSS code to your project, save the file and open it in a web browser. You should now see the login form centered on the page. If it’s not centered properly, you can make adjustments to the CSS properties until you achieve the desired result.


Creating a centered login page in HTML is simple and can greatly enhance the user experience. By following the steps outlined in this article, you can ensure that your login form is perfectly centered, providing a seamless and professional look for your website. Happy coding!