Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a responsive login form with CSS?
A responsive login form adapts to different screen sizes and provides an optimal user experience across all devices. This form typically includes fields for username and password, a login button, "Remember Me" checkbox, and a forgot password link. Let's create a responsive login form using CSS media queries and flexible layouts.
Syntax
/* Basic form styling */
form {
max-width: value;
margin: auto;
padding: value;
}
/* Responsive breakpoints */
@media screen and (max-width: value) {
/* Mobile styles */
}
Form Structure
The login form consists of several key components −
<form>
<div class="profilePic">
<img src="avatar.png" alt="Avatar" class="avatar" />
</div>
<div class="formFields">
<label for="username">Username</label>
<input type="text" name="username" required />
<label for="password">Password</label>
<input type="password" name="password" required />
<button type="submit">Login</button>
<label><input type="checkbox" /> Remember me</label>
</div>
</form>
Avatar Image Styling
The avatar image is centered and styled with rounded corners −
.profilePic {
text-align: center;
margin: 24px 0 12px 0;
}
img.avatar {
width: 150px;
height: 150px;
border-radius: 50%;
}
Input Fields and Button Styling
Style the input fields and button for better usability −
input[type="text"],
input[type="password"] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
font-size: 16px;
}
button {
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
Media Queries for Responsiveness
Use media queries to adjust the layout for smaller screens −
@media screen and (max-width: 768px) {
form {
margin: 10px;
}
img.avatar {
width: 100px;
height: 100px;
}
input[type="text"],
input[type="password"] {
font-size: 14px;
}
}
@media screen and (max-width: 300px) {
span.pass {
display: block;
float: none;
text-align: center;
}
}
Example
Here's a complete responsive login form example −
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
margin: 0;
padding: 20px;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
form {
background: white;
border-radius: 10px;
box-shadow: 0 15px 35px rgba(0,0,0,0.1);
max-width: 400px;
width: 100%;
overflow: hidden;
}
.profilePic {
text-align: center;
padding: 30px 0 20px 0;
background: #f8f9fa;
}
img.avatar {
width: 120px;
height: 120px;
border-radius: 50%;
border: 4px solid white;
box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}
.formFields {
padding: 30px;
}
label {
display: block;
margin-bottom: 5px;
color: #333;
font-weight: bold;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 12px;
margin-bottom: 20px;
border: 2px solid #e1e1e1;
border-radius: 5px;
box-sizing: border-box;
font-size: 16px;
transition: border-color 0.3s ease;
}
input[type="text"]:focus,
input[type="password"]:focus {
outline: none;
border-color: #667eea;
}
button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 12px;
border: none;
border-radius: 5px;
cursor: pointer;
width: 100%;
font-size: 16px;
font-weight: bold;
transition: transform 0.2s ease;
}
button:hover {
transform: translateY(-2px);
}
.checkbox-container {
margin: 20px 0;
}
.forgot-password {
text-align: center;
margin-top: 20px;
}
.forgot-password a {
color: #667eea;
text-decoration: none;
}
@media screen and (max-width: 480px) {
body {
padding: 10px;
}
.formFields {
padding: 20px;
}
img.avatar {
width: 80px;
height: 80px;
}
}
</style>
</head>
<body>
<form>
<div class="profilePic">
<img src="https://cdn.pixabay.com/photo/2016/08/08/09/17/avatar-1577909_960_720.png" alt="Avatar" class="avatar" />
</div>
<div class="formFields">
<label for="username">Username</label>
<input type="text" placeholder="Enter your username" name="username" required />
<label for="password">Password</label>
<input type="password" placeholder="Enter your password" name="password" required />
<button type="submit">Login</button>
<div class="checkbox-container">
<label>
<input type="checkbox" name="remember" /> Remember me
</label>
</div>
<div class="forgot-password">
<a href="#">Forgot your password?</a>
</div>
</div>
</form>
</body>
</html>
A centered login form with a gradient background appears. The form has a white background with rounded corners and shadow. It includes an avatar image, username and password fields with focus effects, a gradient login button, remember me checkbox, and forgot password link. The form adapts to smaller screens by reducing padding and avatar size.
Conclusion
Creating a responsive login form requires proper use of CSS media queries, flexible layouts, and touch-friendly input sizes. The key is ensuring the form remains functional and visually appealing across all device sizes while maintaining good usability standards.
