Tuesday, January 21, 2020

How to Build a Responsive Handmade SVG Form

How to Build a Responsive Handmade SVG Form

In previous tutorials, I’ve shown you how to build a responsive form with flexbox as well as how to style form elements. Today, we’ll continue the journey into the “forms” world and learn to create a responsive handmade SVG form from scratch.

Here’s the form that we’re going to build:

1. Begin With the Page Markup

We’ll start with an SVG and a form element:

SVG Sprites

For our form, we’re going to need a bunch of handmade illustrations. Happily, Ian Yates, Web Design editor here at Tuts+, created for us some beautiful illustrations. Thank you, Ian :)

Similar to what we’ve done in a previous tutorial, we’ll create an SVG sprite and embed all the drawings within it. Then, inside the form, we’ll render the target icon whenever we need it by calling the use element.

Here’s the markup for the SVG sprite:

Notice the preserveAspectRatio="none" attribute which we attached to most of the illustrations. We did this because, as we’ll see later, our icons will scale and loose their initial dimensions.

Form

The form will consist of a heading and an unordered list. Plus, we’ll use a .container for setting a maximum width to the form and horizontally center its contents: 

As you can see, each of the list items above contains one or more classes. Alternatively, for readability reasons, we could have created an extra child element and passed those classes to it, like this:

Inside the list, we’ll place the SVGs and form elements. 

The first list item will include two required input fields along with their SVGs:

In the same way, the second list item will also hold two input fields along with their SVGs:

Within the third list item, we’ll place a textarea and its associated illustration:

The fourth list item will include a fieldset element. Inside it, we’ll put a legend element, a list with two radio buttons along with their SVGs, and its related SVG:

Finally, the fifth list item will contain the submit and reset buttons along with their SVGs:


2. Define Some Basic Styles

Before having a closer look at the individual form elements, let’s first define some basic CSS styles. These will include a custom font taken from Envato Elements, a few custom variables, and some reset rules:

SUMMER - FONT PACK
SUMMER - FONT PACK

Note: for simplicity, I won’t walk through all the CSS rules in this tutorial. You can check the rest of them by clicking at the CSS tab of the demo project.

3. Build the Form Layout

On small screens all our form elements will be stacked:

The form layout on small screens
The form layout on small screens

However, on viewports 600 pixels wide and above, the form layout will change. More specifically:

  • We’ll arrange the inputs of the first two rows into two equal-width columns.
  • Each button will cover one-third of the parent row width.
The form layout on large screens
The form layout on large screens

Thanks to CSS Grid, we can easily build the desired multi-column layout. To begin with, we’ll set the .handmade-form .grid container as being a grid. Then, we’ll use the grid-2 and grid-3 helper classes to define the number of grid columns:

All of these rules are placed within a media query, so they will only take effect on viewports 600px wide and above.

4. Style the Form Elements

With our structure sorted out, our next step is to specify some initial aesthetic styles to all form elements:

Positioning the Illustrations

The next and most challenging part of this exercise is to position the illustrations relative to their associated form element. To do this, we’ll use the position property. An alternative implementation might be to set the drawings as background images. However, I am not a big fan of this implementation because it needs extra manipulation for making images adapt on various screens.

Positioning the SVGs

Let’s take note of our plan:

  • The SVGs will be absolutely positioned elements.
  • They will sit in the same position as their form control and have the same dimensions. That’s why earlier we set their preserveAspectRatio to none.
  • The form controls should be focusable, so we’ll give them a higher z-index.

Here are the required styles for this behavior:


5. Create Custom Radio Buttons

To create our custom radio buttons, once more, we’re going to take advantage of the “CSS checkbox hack technique”.

If you look again at the markup section, you’ll notice that the fieldset element contains the .checkbox-list list. Inside each label, there are two SVGs. The first one describes the unchecked state of the associated radio button, while the other one its checked state.

The radio buttons should fit on a single line, so we’ll turn the .checkbox-list into a flex container. Also, we’ll absolutely position them relative to their label and give them some fixed dimensions (20px x 20px).  

As a radio button changes its state, the SVG which describes its checked state will be animated. To achieve this effect, we’re going to work with the stroke-dasharray and stroke-dashoffset properties which we’ve extensively covered in a recent tutorial.

Here are the corresponding styles:

Note: Even though I’ve attached the stroke-* properties to the parent SVG instead of its path, the effect still works because these properties are inherited.

Conclusion

That’s it, folks! In this tutorial, we managed to build a responsive handmade SVG form from scratch. I hope that this exercise helped you learn something new and inspired you for using it in an upcoming project.

Here’s a reminder of what we built:

In an upcoming tutorial, we’ll go a step further and discuss how to make this form dynamic by incorporating it in a popular WordPress contact form plugin. Stay tuned!

As always, thanks a lot for reading!


No comments:

Post a Comment