To create an awesome combined input type password box with a toggle feature using Bootstrap icons, you'll need to use a mix of HTML, CSS, and JavaScript. Below is an example implementation that fulfills these requirements:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Password Toggle</title>
<!-- Bootstrap CSS -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap Icons -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-icons/1.10.0/font/bootstrap-icons.min.css" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container my-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="form-group position-relative">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Enter password">
<span class="position-absolute end-0 top-50 translate-middle-y me-3" id="toggle-password" style="cursor:pointer;">
<i class="bi bi-eye-slash" id="toggle-icon"></i>
</span>
</div>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS (styles.css)
body {
background-color: #f0f2f5;
font-family: 'Arial', sans-serif;
}
.form-group {
margin-top: 2rem;
}
.form-control {
padding-right: 40px; /* Add padding to the right to prevent the icon from overlapping the text */
border-radius: 25px; /* Rounded corners */
border: 1px solid #ccc;
transition: border-color 0.2s;
}
.form-control:focus {
border-color: #007bff;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}
#toggle-password:hover {
color: #007bff; /* Change eye icon color on hover */
}
.container {
max-width: 400px;
background-color: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
JavaScript (script.js)
document.addEventListener("DOMContentLoaded", function() {
const passwordInput = document.getElementById('password');
const togglePassword = document.getElementById('toggle-password');
const toggleIcon = document.getElementById('toggle-icon');
togglePassword.addEventListener('click', function() {
// Toggle the type attribute
const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
passwordInput.setAttribute('type', type);
// Toggle the icon
toggleIcon.classList.toggle('bi-eye');
toggleIcon.classList.toggle('bi-eye-slash');
});
});
Explanation
- HTML: We create a simple form with a password input and an eye icon button to toggle the password's visibility.
- CSS: Styles enhance the appearance with subtle transitions and rounded corners.
- JavaScript: The script toggles the input type between
'password' and 'text', changing the icon accordingly to provide feedback to the user.
Make sure you include the CSS and JS in the correct files and paths if you're using separate files. You can adjust styling and lay-outing further to suit your preferences.