How To Make An Interactive Envelope?

Ankit Kumar
Web For You
Published in
9 min readJun 19, 2020
Partially Open Letter

Today, I am going to demonstrate how to make the envelope with a contact letter in it as demonstrated.

Prerequisites:-

i. Basic knowledge of general things like paragraphs, background colors, etc.

ii. Basic Knowledge CSS Pseudo Classes

iii. CSS animation using @keyframes

iv. CSS Transforms

v. Know-how of shapes using CSS (I demonstrated this below)

vi. Basic JavaScript which involves Immediately Invoked Functions Expression (IIFE), Event-Handling, and Nested Functions.

CSS Shapes

CSS Triangle CodePen Illustration

Now, I will illustrate how to make this envelope. I am using Visual Studio Code to do the coding part and use Mozilla Firefox for debugging as this browser has great tools to debug CSS.

Let’s Make Something Cool

Step 1 — I will create a div with class attribute value “envelope” to group all the components.

HTML:-

HTML Code Snippet illustrating “envelope” div

Step 2 — Now, I will add 3 div’s as child elements as illustrated below.

HTML Code Snippet illustrating various envelope child div

Notice how I am giving multiple class names to separate properties specific to a given div element.

The purpose of the class-names are as follows:-

1. “cover” — To specify positioning in CSS and to set the width and height of the box.

2. “top”, “right”, “bottom” and “left” — This specifies a part of the envelope. This will become clear later on.

Step 3 (Optional) — I will set the background color of the body to black color to make the envelope color more prominent and specify margin and padding to 0 value.

CSS:-

CSS Styling for Body

Step 4 — Now, I will style the “cover”, “top”, “left” and “bottom right” as follows.

CSS Styling for various envelope parts

Explanation:-

i. I have set the value of position property of “cover” to absolute so that all the elements stack above each other.

ii. I have set the height and width of the cover to 0 value, so I can turn the div into a triangle (Refer to the top CSS Shapes section to get more clarity).

iii. The rest of the properties make the shape of the div a triangle.

iv. I have set the transform-origin property of the “top” class to make it rotate about the x-axis along the top of that element.

Step 5 — Now, I will style the “envelope” div as follows.

CSS Styling for Envelope div

Explanation:-

i. I have set the value of position property to relative so that the “cover” div position themselves according to the “envelope” div.

ii. I have set the height and width of this div such that it matches the envelope “top”, “left” and “bottom right” dimensions.

iii. I have set the background-color as shown to match it to the top part so that when it opens the color matches with the body.

iv. I have set the top, left, margin-left and margin-right arbitrarily so that it appears in the middle of the browser window.

Step 6 — Now, I will insert a “letter” div inside the “envelope” div and style it as shown below.

HTML:-

HTML Code Snippet illustrating letter div

CSS:-

CSS Styling for letter div

Explanation:-

i. I have set the background-color as shown to give the feel of a paper.

ii. I have set the value of width to 95% to make it appear as a separate body inside the envelope and it can be as large as possible.

iii. The properties margin-left, margin-right is used to position the paper in the middle of the envelope.

iv. I have set the box-sizing property to border-box and given some padding for later when we will add content inside it.

Note — 1. You can verify “letter” div by setting the visibility of “cover” div to hidden.

2. The used font is a Google Font.

Now, I will add JavaScript Event-Listeners to trigger animations.

Step 7 — I will write all functions and variables inside an Immediately Invoked Function Expression (IIFE) to avoid polluting the global namespace and I will use strict mode.

JavaScript IIFE and strict mode

Step 8 — Now, I will add event-listeners and ids to the required div which is self-explanatory.

JavaScript:-

JavaScript Variable Declarations

HTML:-

HTML Code Snippet illustrating id’s for div’s

Step 9 — I will add z-Index to the “top”, “letter” and “bottom right” class so that the letter appears below all the elements. In my case, I have used odd-numbers.

Step 10 — I will add some more classes in CSS which corresponds to the events.

.open Class

CSS Styling for “open” class

This class corresponds to the opening of the envelope. This will be used on .top class.

.close Class

CSS Styling for “close” class

This class corresponds to the closing of the envelope. This will be used on .top class.

.in Class

CSS Styling for “in” class

This class corresponds to the state when the “letter” div is inside the envelope.

.out-partial Class

CSS Styling for “out-partial” class

This class corresponds to the state when the “letter” div is partially outside the envelope.

Step 11 — I will make the default state of envelope “top” div closed.

JavaScript Code Snippet illustrating the addition of Event Listener

Now, I will add event-listeners.

JavaScript Code Snippet illustrating the addition of Event Listener

I have defined four functions as follows:-

1. openEnvelopeOnHover(): This function will remove the class “close” and add the class “open” and will then call the function pullOutPartial() which will pull the letter out partially.

2. pullOutPartial(): This function will pull out the letter partially by removing the class “in” and adding “out-partial” to the “letter” div.

3. closeEnvelopeOnHover(): This function will first put the letter inside the envelope by calling putIn() and then will remove the class “open” and add the class “close”.

4. putIn(): This function will put the letter inside the envelope by removing the class “out-partial” and adding the class “in”.

Note — The transition property in all classes will make the change of state silky smooth.

Important Note: –

1. I have used transition property on all the classes to make animations smoother and used animation-delay such that the “closing of the envelope” and “letter going inside” animation do not overlap.

2. I have used the variable flag whose purpose will be explained later.

The function body is as follows:-

JavaScript Code Snippet illustrating function bodies

(Important) Step 12 — I will add perspective: 1000px property and transform-style:preserve-3d so that the “envelope” parent and its child are rendered properly in 3D-space.

Output:-

Main Browser Window — Letter with Envelope

So, We have made the basic “envelope” and “letter” and will now move to the part where we will add a “click” event listener to pull the letter out completely so that the user can write the message.

Note — I have used a “flag” variable as an indicator that a user has clicked on the envelope and for scalability so I can add more interactions later on. The working is as follow:-

When the “flag” variable is equal to 0

This is used as an indicator to tell the computer that I am going to pull the letter out completely and to remove the “mouseout” event in which it will put the letter inside on hovering out.

When the “flag” variable is equal to 1

This is used as an indicator to tell the computer that the user has not clicked on the letter yet and continue to function as normal with hover-in and hover-out events.

Tip — You should include an extra button that tells the user to click on it to open the letter. You can add this later by yourself after this tutorial so I am leaving that part in this tutorial.

Step 13 — Now, I will add a “click” event listener with an event handler function named “openEnvelope()” and add three CSS classes named “pull”, “put” and “final” and two CSS animations using @keyframes named “pullLetter” and “putLetter”.

JavaScript:-

JavaScript Code Snippet illustrating the addition of Event Listener

CSS:-

  1. @keyframes animation
CSS Animations using @keyframes

The “pullLetter” animation will pull the letter out using translateY transform and will then “putLetter” will put the letter in the original position simultaneously increasing the size using the scale transform.

2. CSS classes

CSS Styling for “pull”, ”put” and “final” class

The “pull” and “put” classes are used to trigger the animations whereas the “final” class is used to fix the position of the letter at the ending of the animation “put”.

Step 14 — Now I will write the “openEnvelope()” event handler function.

JavaScript:-

JavaScript Code Snippet consisting of the function body

Step-by-Step Explanation:-

  1. The first two lines will set the “flag” variable to value 0 and will then call the function “closeEnvelopeHover()” to remove the “mouseout” event listener to stop the incorrect triggering of hover animations.
  2. The second line will add the class “pull” dynamically which in turn will trigger the animation of opening the letter and pulling the letter out.
  3. The third line is an event listener which will trigger the anonymous function written as the second argument when the “pull” animation end using an event named “animationend”.
  4. The function in the previous step will change the z-index of the “bottom right” and “left” class from 5 to 0 dynamically so that they don’t appear above the letter and set the z-index of the letter to 20(any arbitrary value than the maximum of all envelope elements will do) dynamically.
  5. The function will then add the class “put” dynamically which will trigger the put the letter at its original position and over the envelope.
  6. The end of the “put” animation will then trigger another event handler function as written in the above code snippet which will set the transition property to none value so that the final state can be immediately set using “final” class without any animation.
  7. The function will then remove the class “pull” and “put” as they are no longer useful and will then set the cursor to default to indicate that no interactive events are remaining.
  8. After the completion of the above steps, the function will remove the “click” event listener to stop the further triggering of the above steps.

Final Output

Main Browser Window — Letter Final State

The “letter” will appear like this in its final state.

Now, I will add the remaining things so that the user can provide inputs and will style them so that it appears like a letter.

Step 15 — Now I will add the form element so that I can input the message from the user.

HTML:-

HTML Code Snippet illustrating form elements

Specify the above attributes according to your needs.

Step 16 — Now, I will add CSS styling to the form.

CSS:-

CSS Styling for letter form

The previous two-step purely depends upon you. Customize and share the improved version via CodePen with me and I will be happy to see that people have learned from this article.

Output:-

Main Browser Window-Final Letter Output

So, this is how you can create the demonstrated envelope using CSS and JavaScript easily.

Thank You for reading this article.

You can contact me through my E-Mail.

--

--

Ankit Kumar
Web For You

I am a student pursuing my B. Tech at Indian Institute of Information Technology Guwahati.