Creating a visually appealing and inclusive gender selection form is essential for modern web applications, such as user registration systems, surveys, or profile setups. By incorporating icons, you can make the form intuitive and engaging. This comprehensive guide will walk you through building a gender selection form with HTML, CSS, and JavaScript, featuring icons for male, female, and non-binary options. The form will be responsive, accessible, and styled with modern design principles. We’ll also provide links to free AI tools, a complete code snippet, and a separate SVG image (available for download) to use as the article’s featured image.
Gender selection forms are a staple in user interfaces, but plain text options can feel outdated or uninspiring. Adding icons enhances:
This tutorial covers:
The form will include three radio button options (male, female, non-binary) with corresponding SVG icons. Radio buttons ensure single-selection functionality, and we’ll prioritize accessibility with proper labeling.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gender Selection Form</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="form-container">
<h1>Gender Selection</h1>
<form id="genderForm">
<div class="option">
<input type="radio" id="male" name="gender" value="male" aria-label="Male">
<label for="male">
<svg class="icon" viewBox="0 0 24 24"><path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/></svg>
Male
</label>
</div>
<div class="option">
<input type="radio" id="female" name="gender" value="female" aria-label="Female">
<label for="female">
<svg class="icon" viewBox="0 0 24 24"><path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/></svg>
Female
</label>
</div>
<div class="option">
<input type="radio" id="nonbinary" name="gender" value="nonbinary" aria-label="Non-Binary">
<label for="nonbinary">
<svg class="icon" viewBox="0 0 24 24"><path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/></svg>
Non-Binary
</label>
</div>
<button type="submit">Submit</button>
</form>
<p id="result"></p>
</div>
<script src="script.js"></script>
</body>
</html>
The CSS will make the form responsive, visually appealing, and interactive. We’ll hide the default radio buttons and style the labels to act as custom buttons, with icons that change color on selection.
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f4f7fa;
}
.form-container {
background: white;
padding: 25px;
border-radius: 12px;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
text-align: center;
max-width: 400px;
width: 100%;
}
h1 {
font-size: 26px;
margin-bottom: 25px;
color: #333;
}
.option {
margin: 15px 0;
display: flex;
align-items: center;
}
input[type="radio"] {
display: none;
}
label {
display: flex;
align-items: center;
padding: 12px;
cursor: pointer;
transition: background 0.3s ease, transform 0.2s;
width: 100%;
border-radius: 8px;
}
label:hover {
background: #e8ecef;
transform: scale(1.02);
}
.icon {
width: 28px;
height: 28px;
margin-right: 12px;
fill: #555;
}
input[type="radio"]:checked + label {
background: #007bff;
color: white;
font-weight: bold;
}
input[type="radio"]:checked + label .icon {
fill: white;
}
button {
margin-top: 25px;
padding: 12px 30px;
background: #007bff;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
transition: background 0.3s;
}
button:hover {
background: #0056b3;
}
#result {
margin-top: 20px;
font-size: 16px;
color: #333;
}
@media (max-width: 600px) {
.form-container {
padding: 15px;
max-width: 90%;
}
h1 {
font-size: 22px;
}
label {
padding: 10px;
}
}
Explanation
Flexbox: Centers the form and aligns options horizontally.
Custom Radio Buttons: Labels are styled to replace default radio buttons, with hover and checked states.
Responsive Design: Media queries adjust padding and widths for smaller screens.
Transitions: Smooth hover and selection effects enhance interactivity.
Step 3: Adding JavaScript for Form Validation
JavaScript will handle form submission, validate the selection, and display feedback to the user.
document.getElementById('genderForm').addEventListener('submit', function(event) {
event.preventDefault();
const selectedGender = document.querySelector('input[name="gender"]:checked');
const result = document.getElementById('result');
if (selectedGender) {
result.textContent = `You selected: ${selectedGender.value.charAt(0).toUpperCase() + selectedGender.value.slice(1)}`;
result.style.color = '#28a745';
} else {
result.textContent = 'Please select a gender.';
result.style.color = '#dc3545';
}
});
To streamline development or enhance the form, try these free AI-powered tools:
These tools are beginner-friendly and can elevate your design process without cost.
To make the form inclusive:
Test the form across browsers (Chrome, Firefox, Safari) and devices. For deployment, use:
Here’s the complete code for the gender selection form, combining HTML, CSS, and JavaScript. The SVG icons use placeholder paths from Material Icons; you can replace them with custom icons from Iconify.
<!doctypehtml><html lang=en><meta charset=UTF-8><meta content="width=device-width,initial-scale=1"name=viewport><title>Gender Selection Form</title><style>body{font-family:Arial,sans-serif;display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background-color:#f4f7fa}.form-container{background:#fff;padding:25px;border-radius:12px;box-shadow:0 6px 12px rgba(0,0,0,.1);text-align:center;max-width:400px;width:100%}h1{font-size:26px;margin-bottom:25px;color:#333}.option{margin:15px 0;display:flex;align-items:center}input[type=radio]{display:none}label{display:flex;align-items:center;padding:12px;cursor:pointer;transition:background .3s ease,transform .2s;width:100%;border-radius:8px}label:hover{background:#e8ecef;transform:scale(1.02)}.icon{width:28px;height:28px;margin-right:12px;fill:#555}input[type=radio]:checked+label{background:#007bff;color:#fff;font-weight:700}input[type=radio]:checked+label .icon{fill:#fff}button{margin-top:25px;padding:12px 30px;background:#007bff;color:#fff;border:none;border-radius:8px;cursor:pointer;font-size:16px;transition:background .3s}button:hover{background:#0056b3}#result{margin-top:20px;font-size:16px;color:#333}@media (max-width:600px){.form-container{padding:15px;max-width:90%}h1{font-size:22px}label{padding:10px}}</style><div class=form-container><h1>Gender Selection</h1><form id=genderForm><div class=option><input aria-label=Male id=male name=gender type=radio value=male> <label for=male><svg class=icon viewBox="0 0 24 24"><path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/></svg> Male</label></div><div class=option><input aria-label=Female id=female name=gender type=radio value=female> <label for=female><svg class=icon viewBox="0 0 24 24"><path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/></svg> Female</label></div><div class=option><input aria-label=Non-Binary id=nonbinary name=gender type=radio value=nonbinary> <label for=nonbinary><svg class=icon viewBox="0 0 24 24"><path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/></svg> Non-Binary</label></div><button type=submit>Submit</button></form><p id=result></div><script>document.getElementById('genderForm').addEventListener('submit', function(event) {
event.preventDefault();
const selectedGender = document.querySelector('input[name="gender"]:checked');
const result = document.getElementById('result');
if (selectedGender) {
result.textContent = `You selected: ${selectedGender.value.charAt(0).toUpperCase() + selectedGender.value.slice(1)}`;
result.style.color = '#28a745';
} else {
result.textContent = 'Please select a gender.';
result.style.color = '#dc3545';
}
});</script>