How to create: Pop-up chat window

Learn how to create a pop-up chat window using CSS and JavaScript.

Try It Yourself

How to create a pop-up chat window

Step 1 - Add HTML:

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

<div class="chat-popup" id="myForm">
  <form action="/action_page.php" class="form-container">
    <h1>Chat</h1>
    <label for="msg"><b>Message</b></label>
    <textarea placeholder="Type message.." name="msg" required></textarea>
    <button type="submit" class="btn">Send</button>
    <button type="button" class="btn cancel" onclick="closeForm()">Close</button>
  </form>
</div>

Step 2 - Add CSS:

{box-sizing: border-box;}
/* Button used to open chat form - Fixed at the bottom of the page */
.open-button {
  background-color: #555;
  color: white;
  padding: 16px 20px;
  border: none;
  cursor: pointer;
  opacity: 0.8;
  position: fixed;
  bottom: 23px;
  right: 28px;
  width: 280px;
}
/* Pop up chat window - Default hidden */
.chat-popup {}}
  display: none;
  position: fixed;
  bottom: 0;
  right: 15px;
  border: 3px solid #f1f1f1;
  z-index: 9;
}
/* Add styles to the form container */
.form-container {
  max-width: 300px;
  padding: 10px;
  background-color: white;
}
/* Full-width text area */
.form-container textarea {
  width: 100%;
  padding: 15px;
  margin: 5px 0 22px 0;
  border: none;
  background: #f1f1f1;
  resize: none;
  min-height: 200px;
}
/* Perform some actions when the text area gains focus */
.form-container textarea:focus {
  background-color: #ddd;
  outline: none;
}
/* Set the style for submit/login buttons */
.form-container .btn {
  background-color: #04AA6D;
  color: white;
  padding: 16px 20px;
  border: none;
  cursor: pointer;
  width: 100%;
  margin-bottom:10px;
  opacity: 0.8;
}
/* Add red background color to the cancel button */
.form-container .cancel {
  background-color: red;
}
/* Add some hover effects to the buttons */
.form-container .btn:hover, .open-button:hover {
  opacity: 1;
}

Step 3 - Add JavaScript:

function openForm() {
  document.getElementById("myForm").style.display = "block";
}
function closeForm() {
  document.getElementById("myForm").style.display = "none";
}

Try It Yourself

Related Pages

Tutorial:HTML Form

Tutorial:CSS Form