How To Make Login Page With Skratch

How To Articles

Creating a login page from scratch might sound like a daunting task, but with the right guidance and a little bit of coding knowledge, it can be a rewarding experience. In this article, I will walk you through the process of creating a login page using HTML, CSS, and JavaScript.

Step 1: Setting Up the HTML Structure

The first step in creating a login page is to set up the HTML structure. Start by creating a new HTML file and opening it in your favorite text editor. We will begin by adding some basic HTML tags.

<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
</body>
</html>

Inside the body tag, we will add a form element that will contain our login form fields. We will also include a heading and some labels for the form fields.

<h2>Login to Your Account</h2>
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>

<label for="password">Password:</label>
<input type="password" id="password" name="password" required>

<input type="submit" value="Login">
</form>

Step 2: Styling the Login Page

Now that we have set up the basic HTML structure, let’s move on to styling our login page using CSS. We will create a separate CSS file and link it to our HTML file.

<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<h2>Login to Your Account</h2>
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>

<label for="password">Password:</label>
<input type="password" id="password" name="password" required>

<input type="submit" value="Login">
</form>
</body>
</html>

Next, open the “styles.css” file and add the following CSS code to give your login page a stylish look.

body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
}

h2 {
text-align: center;
color: #333;
}

form {
max-width: 300px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 5px;
}

label {
display: block;
margin-bottom: 10px;
font-weight: bold;
}

input[type="text"],
input[type="password"] {
width: 100%;
padding: 10px;
margin-bottom: 20px;
}

input[type="submit"] {
width: 100%;
padding: 10px;
background-color: #333;
color: #fff;
border: none;
border-radius: 5px;
}

Step 3: Adding Functionality with JavaScript

Now that our login page is styled, let’s add some functionality to it using JavaScript. We will validate the form fields and display a success message upon successful login.

const form = document.querySelector('form');
const username = document.querySelector('#username');
const password = document.querySelector('#password');

form.addEventListener('submit', function(event) {
event.preventDefault();

if(username.value === 'myusername' && password.value === 'mypassword') {
alert('Login successful!');
// Redirect to the desired page using window.location.href = 'URL';
} else {
alert('Invalid username or password. Please try again.');
}
});

Make sure to replace ‘myusername’ and ‘mypassword’ with your desired username and password. You can also uncomment the line to redirect the user to a specific page upon successful login by providing the desired URL.

Conclusion

Creating a login page from scratch might seem challenging at first, but by following the steps outlined in this article, you can create a functional and visually appealing login page. Remember to personalize it by adding your own touch and branding elements. Happy coding!