May
10

Build a Gender Selection Form with Icons

05/10/2025 12:00 AM by Admin in Html


gender selection

 

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.

Why Create a Gender Selection Form with Icons?

Gender selection forms are a staple in user interfaces, but plain text options can feel outdated or uninspiring. Adding icons enhances:

  • Visual Appeal: Icons make the form more engaging.
  • Accessibility: Visual cues help users with different abilities or language barriers.
  • Inclusivity: Representing diverse gender identities fosters a welcoming experience.

This tutorial covers:

  • Structuring the form with HTML.
  • Styling it with CSS for responsiveness and aesthetics.
  • Adding interactivity with JavaScript for validation.
  • Using free AI tools to enhance the design process.
  • Providing a complete, ready-to-use code snippet.
  • Offering a custom SVG image (350x200, 500 quality) as the article’s featured image.

Step 1: Building the HTML Structure

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>

Explanation

  • Radio Buttons: The name="gender" attribute groups the options, allowing only one selection.
  • SVG Icons: We use placeholder paths from Material Icons (you can replace them with custom icons from Iconify).
  • Accessibility: The aria-label attribute and for attribute in labels ensure screen-reader compatibility.

Step 2: Styling with CSS

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';
  }
});

 

Explanation

  • Event Listener: Prevents default form submission and checks for a selected radio button.
  • Dynamic Feedback: Displays the selected gender with proper capitalization or an error message.
  • Color Coding: Green for success, red for errors, improving user experience.

Step 4: Using Free AI Tools

To streamline development or enhance the form, try these free AI-powered tools:

  • Canva: Design custom SVG icons or edit the featured image.
  • Figma: Prototype the form layout or collaborate on design.
  • ColorSpace: Generate accessible color palettes for the form.
  • WAVE Accessibility Checker: Test the form for WCAG compliance.
  • Iconify: Access thousands of free SVG icons for the form.

These tools are beginner-friendly and can elevate your design process without cost.

Step 5: Ensuring Accessibility

To make the form inclusive:

  • Use aria-label for screen readers.
  • Ensure a contrast ratio of at least 4.5:1 (e.g., white text on #007bff background).
  • Test keyboard navigation (use Tab and Enter keys).
  • Validate with WAVE to meet WCAG 2.1 standards.

Step 6: Testing and Deployment

Test the form across browsers (Chrome, Firefox, Safari) and devices. For deployment, use:

  • GitHub Pages: Host the form for free by pushing to a GitHub repository.
  • Netlify: Deploy with a drag-and-drop interface or Git integration.

Complete Code

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>