Contact Form Markup

In this lesson, we will create a contact form using Flexbox.

We'll cover the following

Creating a form

Replace the contents of the form area with an HTML form. We will not make use of the action attribute of the form for now. If you would like to make the form work by collecting data, you can use a free service on formspree.io.

<form 
   action="#" 
   method="POST" 
   class="contact-form">
</form>

Inside the form, create four containers for the form flex-items:

<form 
   action="#" 
   method="POST" 
   class="contact-form">
  <div class="form-flex-item"></div>
  <div class="form-flex-item"></div>
  <div class="form-flex-item"></div>
  <div class="form-flex-item"></div>
</form>

Inside the divs, we will have a Name, an Email, and a Message field. Note the Email field has a type email, while the Message field is a textarea.

<form 
   action="#" 
   method="POST" 
   class="contact-form">
  <div class="form-flex-item">
    <label class="form-label">
      Name
      <input 
         type="text" 
         name="name" 
         class="form-input" />
    </label>
  </div>
  <div class="form-flex-item">
    <label class="form-label">
      Email
      <input 
         type="email" 
         name="email" 
         class="form-input" />
    </label>
  </div>
  <div class="form-flex-item">
    <label class="form-label">
      Message
      <textarea name="message" class="form-textarea"></textarea>
    </label>
  </div>
  <div class="form-flex-item">
    <button class="btn-send">
      Send
    </button>
  </div>   
</form>

Styling the form

Use Flexbox to place the three Flex items below each other and center them horizontally. While the border around the form area will also be centered, this should not bother us, because later we will remove these borders to see the end result.

.contact-form {
  display: flex;
  flex-direction: column;
  align-items: center;
}

The label and input are styled such that they are below each other. While the label and input field positioning uniformly works for both text fields and text areas, it makes sense to set the height of the text area individually.

.form-label {
    margin: 20px;
    display: block;
    font-size: 14px;
    line-height: 24px;
}

.form-input, .form-textarea {
    display: block;
    padding: 6px;
}

.form-textarea {
  height: 125px;
}

.btn-send {
  border: none;
  box-shadow: 3px 3px 8px rgba(0, 0, 0, .5);
  padding: 12px;
  margin: 12px 0;
  cursor: pointer;
}

The final result looks as follows:

Get hands-on with 1200+ tech skills courses.