Uncategorized

How to create a Modern Login Form using HTML CSS and JavaScript

Description

In this Web Development tutorial we are going to create a modern login from completely from scratch in form using HTML5 CSS3 and JavaScript. In order to access the form we will use a animated logo using CSS @keyframes and animations

Content of this tutorial

HTML Code:

<div class="logIn-container">
      <h1 class="logIn-title">Welcome to Norbert <span>BM</span> Academy</h1>
      <h2 class="sub-title">Pleas login</h2>
      <a href="#"><div id="LogInLogo"></div></a>
      <form id="MainLogIn-form">
        <div class="input-group">
          <label class="main-text-c">Username</label>
          <i class="far fa-user"></i>
          <input
            type="text"
            id="LogInUserName"
            class="main-text-c"
            placeholder="Type your name"
          />
        </div>
        <div class="input-group">
          <label class="main-text-c">Password</label>
          <i class="fas fa-key"></i>
          <input
            type="password"
            id="UserPassword"
            class="main-text-c"
            placeholder="Type your password"
          />
          <i class="far fa-eye"></i>
        </div>
        <button class="btn btn-danger-c" type="submit" id="submitLogInForm">
          Login
        </button>
      </form>
    </div>

CSS Code:

@import url("https://fonts.googleapis.com/css2?family=Raleway:wght@100;200;300;400;500;600;700;800;900&display=swap");

body {
  font-family: "Raleway", sans-serif;
  text-shadow: 1px 2px 2px #000;
}
/** Variables */
:root {
  --transition-speed: 0.3s;
  /* * Colors */
  --prim-dark-color: #24273b;
  --prim-light-color: #f8f9f7;
  --danger-color: #df1e26;
  --success-color: #22aa44;
  --info-color: #11a2b2;
}

#LogInLogo {
  height: 150px;
  width: 150px;
  background: url(/img/menyhart-Media.jpg) center/cover;
  border-radius: 50%;
  margin-top: 3rem;
  box-shadow: 0 15px 10px rgba(248, 249, 247, 0.5);
  border: 1px solid #ddd;
  animation: logoAnimation 4s infinite;
  transition: var(--transition-speed) ease;
}
#LogInLogo:hover {
  animation: none;
  opacity: 1;
  box-shadow: 0 10px 5px rgba(248, 249, 247, 0.1);
  transition: var(--transition-speed) ease;
}

@keyframes logoAnimation {
  0% {
    opacity: 0.4;
    box-shadow: 0 10px 5px rgba(10, 0, 14, 0.1);
  }
  50% {
    opacity: 1;
    box-shadow: 0 15px 15px rgba(10, 0, 14, 0.3);
  }
  100% {
    opacity: 0.4;
    box-shadow: 0 10px 5px rgba(10, 0, 14, 0.1);
  }
}

#LogInLogo:active {
  transform: scale(0.96);
  transition: var(--transition-speed) ease;
}
/* Todo: Main Login Container */

.logIn-container,
.home-container {
  position: relative;
  width: 500px;
  margin: 15vh auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.logIn-title {
  text-align: center;
  color: var(--prim-dark-color);
  font-size: 4rem;
}

.logIn-title span {
  color: var(--info-color);
}

.sub-title {
  font-weight: 400;
  text-shadow: none;
  margin-top: 2rem;
  letter-spacing: 2px;
}

/* * Log in form */

#MainLogIn-form {
  position: relative;
  margin-top: 4rem;
  width: 300px;
  animation: display-MainLogIn-form var(--transition-speed) ease;
  display: none;
}

@keyframes display-MainLogIn-form {
  form {
    top: -150px;
    opacity: 0;
  }
  to {
    top: 0;
    opacity: 1;
  }
}
#MainLogIn-form label {
  font-size: 0.8rem;
  margin-bottom: 0.5rem;
  color: var(--prim-dark-color);
  letter-spacing: 1.1px;
  text-shadow: none;
}
#MainLogIn-form input {
  background-color: var(--prim-dark-color);
  color: var(--prim-light-color);
  outline: none;
  border: none;
  border-radius: 5px;
  transition: var(--transition-speed) ease;
  padding: 0.5rem;
  padding-left: 1.5rem;
  letter-spacing: 1.1px;
}
#MainLogIn-form input:focus {
  border-bottom: 3px solid springgreen;
}
#MainLogIn-form input:placeholder {
  color: lightgray;
  font-weight: 100;
}
#MainLogIn-form .input-group {
  display: flex;
  flex-direction: column;
  margin-top: 1rem;
  position: relative;
}
#MainLogIn-form i {
  color: var(--prim-light-color);
}

#MainLogIn-form i.fa-key,
#MainLogIn-form i.fa-user {
  position: absolute;
  bottom: 0.5rem;
  left: 0.3rem;
}
#MainLogIn-form i.fa-eye {
  position: absolute;
  bottom: 0.5rem;
  right: 0.3rem;
  cursor: pointer;
}

.btn,
.alert {
  margin: 0.25rem 0;
  display: inline-block;
  font-weight: 500;
  color: var(--prim-light-color);
  font-family: inherit;
  text-align: center;
  font-size: 1rem;
  line-height: 1.5;
  vertical-align: middle;
  cursor: pointer;
  user-select: none;
  background-color: transparent;
  border: 1px solid transparent;
  padding: 0.375rem 0.75rem;
  border-radius: 0.25rem;
  text-decoration: none;
  box-shadow: 0 1px 2px #333;
  transition: all var(--transition-speed) ease;
  width: 100%;
}
.btn:hover {
  opacity: 0.8;
  transition: all var(--transition-speed) ease;
}
.btn-success-c {
  background-color: var(--success-color);
}
.btn-danger-c {
  background-color: var(--danger-color);
}

.btn-info-c {
  background-color: var(--info-color);
}

/* todo: Create Utility Classes */

.animate {
  animation: logoAnimation 4s infinite;
}
#MainLogIn-form.active {
  display: block;
}
#LogInLogo.no-animation {
  animation: none;
}

.alert {
  margin-top: 1rem;
  text-shadow: none;
}

.alert-error {
  background-color: var(--danger-color);
}
.alert-success {
  background-color: var(--success-color);
}
/* * Message Box  */

.message-box {
  width: 150px;
  min-height: 50px;
  position: absolute;
  right: 0;
  background-color: var(--prim-dark-color);
  display: flex;
  justify-content: center;
  text-align: center;
  align-items: center;
  border: 1px solid var(--info-color);
  border-bottom: 5px solid var(--info-color);
  border-radius: 3px;
  color: var(--prim-light-color);
  font-family: inherit;
}

JavaScript Code:

const mainLogInForm = document.getElementById("MainLogIn-form");
const mainLogInLogo = document.getElementById("LogInLogo");

const logInUserPassword = document.getElementById("UserPassword");
const showHideUserPasswordIcon = document.querySelector(".fa-eye");

console.log(mainLogInForm, mainLogInLogo);

// todo: Show/Hide Login

mainLogInLogo.onclick = () => {
  mainLogInForm.classList.toggle("active");
  mainLogInLogo.classList.toggle("no-animation");
};

// todo: Show/Hide Password
showHideUserPasswordIcon.onclick = () => {
  if (logInUserPassword.type === "password") {
    logInUserPassword.type = "text";
    showHideUserPasswordIcon.className = "fas fa-eye";
  } else {
    logInUserPassword.type = "password";
    showHideUserPasswordIcon.className = "far fa-eye";
  }
};

// todo: Create Message Box

// todo: info message for logo
// mainLogInLogo.onmouseover = () => {
//   //   console.log("log-in");
//   mainLogInLogo.appendChild(createMessageBox("Clock for login!"));
// };
// mainLogInLogo.onmouseout = () => {
//   //   console.log("log-out");
//   mainLogInLogo.querySelector(".message-box").remove();
// };

onMouseOverAndOut(mainLogInLogo, "Click for login!");

// todo: info message for password
onMouseOverAndOut(showHideUserPasswordIcon, "Click to show/hide password!");

//TODO: Form Validation

const form = document.querySelector("#MainLogIn-form");
const logInUserName = document.querySelector("#LogInUserName");
const userPassword = document.querySelector("#UserPassword");
const submitLogInForm = document.querySelector("#submitLogInForm");

// todo: validate input

form.onkeyup = () => {
  if (
    logInUserName.value !== "" &&
    logInUserName.value.length >= 3 &&
    userPassword.value !== "" &&
    userPassword.value.length > 3
  ) {
    // submitLogInForm.classList.remove("btn-danger-c");
    // submitLogInForm.classList.add("btn-success-c");
    classSwitcher(submitLogInForm, "btn-danger-c", "btn-success-c");
  } else {
    // submitLogInForm.classList.remove("btn-success-c");
    // submitLogInForm.classList.add("btn-danger-c");
    classSwitcher(submitLogInForm, "btn-success-c", "btn-danger-c");
  }
};

// todo: Alert messages

submitLogInForm.onclick = (event) => {
  event.preventDefault();
  validateLogin();
};

const validateLogin = () => {
  if (logInUserName.value === "") {
    showAlert("Username is empty of les then 3!", "error");
  } else if (logInUserName !== "" && userPassword.value === "") {
    showAlert("Username correct!", "success");
    showAlert("Password is empty of les then 4!", "error");
  } else if (submitLogInForm.classList.contains("btn-success-c")) {
    document.querySelector("body").innerHTML = `
    <div class="home-container">
    <h1 class="logIn-title">Hi <span id="UserName">${logInUserName.value}!</span></h1>
    <a href="./index.html"><div id="LogInLogo"></div></a>
  </div>
    
    `;
  }
};

// ! Utility Function

const createMessageBox = (stringMessage) => {
  let messageBox = document.createElement("div");
  messageBox.className = "message-box";
  messageBox.innerText = stringMessage;
  messageBox.classList.toggle("animate");
  return messageBox;
};

function onMouseOverAndOut(element, stringMessage) {
  element.onmouseover = () => {
    element.appendChild(createMessageBox(stringMessage));
  };
  element.onmouseout = () => {
    element.querySelector(".message-box").remove();
  };
}

// todo: create class switcher function

const classSwitcher = (element, currentClass, nextClass) => {
  element.classList.remove(currentClass);
  element.classList.add(nextClass);
};

// todo: Create Show Alert Function

const showAlert = (stringMessage, className) => {
  const div = document.createElement("div");
  div.className = `alert alert-${className}`;
  div.appendChild(document.createTextNode(stringMessage));
  const container = document.querySelector(".logIn-container");
  container.insertBefore(div, form);

  // todo: time condition display

  setTimeout(() => document.querySelector(".alert").remove(), 3000);
};

For more information about JavaScript get the Complete Modern JavaScript Course NOW and become a javascript developer

https://volere.life/web-development/web-dev-courses/

Modern JavaScript and NodeJS from Beginner to Advanced

Popular course:

https://www.udemy.com/course/30-html-css-javascript-projects-in-30-days-for-beginners/?couponCode=APR2021

30 HTML, CSS & JavaScript projects in 30 Days for Beginners

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: