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 it 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;
  kleur: wit;
  invulling: 16px 20px;
  rand: none;
  cursor: pointer;
  透明度: 0.8;
  positie: fixed;
  bottom: 23px;
  right: 28px;
  width: 280px;
}
/* Pop-up chat window - default hidden */
.chat-popup {
  weergave: none;
  positie: fixed;
  onderkant: 0;
  rechts: 15px;
  rand: 3px vast #f1f1f1;
  z-indices: 9;
}
/* Voeg stijlen toe aan de formuliercontainer */
.form-container {
  maximalebreedte: 300px;
  invulling: 10px;
  achtergrondkleur: wit;
}
/* Volledige breedte tekstgebied */
.form-container textarea {
  breedte: 100%;
  invulling: 15px;
  marge: 5px 0 22px 0;
  rand: none;
  achtergrond: #f1f1f1;
  grootteaanpassen: none;
  minimalehoogte: 200px;
}
/* Voer enkele acties uit wanneer het tekstgebied gefocust is */
.form-container textarea:focus {
  achtergrondkleur: #ddd;
  omlijsting: none;
}
/* Stel de stijl van de indienen/inlogknoppen in */
.form-container .btn {
  achtergrondkleur: #04AA6D;
  kleur: wit;
  invulling: 16px 20px;
  rand: none;
  cursor: pointer;
  breedte: 100%;
  onderkantmarge:10px;
  透明度: 0.8;
}
/* Voeg een rode achtergrondkleur toe aan de annuleerknop */
.form-container .cancel {
  achtergrondkleur: rood;
}
/* Voeg enkele hover-effecten toe aan de knoppen */
.form-container .btn:hover, .open-button:hover {
  透明度: 1;
}

Derde stap - Voeg JavaScript toe:

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