How to create: Contact Form

Learn how to create a contact form using CSS.

Contact Form

Try It Yourself

How to create a contact form

Step 1 - Add HTML:

Use the <form> element to handle input. You can learn more about it in our PHP tutorial.

Then add input controls for each field (with matching labels):

<div class="container">
  <form action="action_page.php">
    <label for="fname">First Name</label>
    <input type="text" id="fname" name="firstname" placeholder="Your name..">
    <label for="lname">Last Name</label>
    <input type="text" id="lname" name="lastname" placeholder="Your last name..">
    <label for="country">Country</label>
    <select id="country" name="country">
      <option value="australia">Australia</option>
      <option value="canada">Canada</option>
      <option value="usa">USA</option>
    </select>
    <label for="subject">Subject</label>
    <textarea id="subject" name="subject" placeholder="Write something.." style="height:200px"></textarea>
    <input type="submit" value="Submit">
  </form>
</div>

Step 2 - Add CSS:

/* Set styles for input fields, selection elements, and text fields of type "text" */
input[type=text], select, textarea {
  width: 100%; /* Full width */
  padding: 12px; /* Some padding */ 
  border: 1px solid #ccc; /* Gray border */
  border-radius: 4px; /* Rounded border */
  box-sizing: border-box; /* Ensures that padding and width remain unchanged */
  margin-top: 6px; /* Bottom external margin */
  margin-bottom: 16px; /* Bottom margin */
  resize: vertical /* Allows users to resize the text field vertically (not horizontally) */
}
/* Set specific background color and other styles for the submit button */
input[type=submit] {
  background-color: #04AA6D;
  color: white;
  padding: 12px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
/* Add a deeper green background when the mouse hovers over the submit button */
input[type=submit]:hover {
  background-color: #45a049;
}
/* Add background color and some padding to the form container */
.container {
  border-radius: 5px;
  background-color: #f2f2f2;
  padding: 20px;
}

Try It Yourself

Related Pages

Tutorial:HTML Form

Tutorial:CSS Form