Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- Main container -->
    <div class="container">
      <div class="messages">
        <!-- <div class="received">Hey</div>
        <div class="sent">Hey , I'm fine</div>
        <div class="received">Hey how are you?</div>
        <div class="received">Hey how are you?</div>
        <div class="sent">Hey , I'm fine</div>
        <div class="sent">Hey , I'm fine</div>
        <div class="sent">Hey , I'm fine</div>
        <div class="received">Hey how are you?</div>
        <div class="sent">Hey , I'm fine</div>
        <div class="sent">Hey , I'm fine</div>
        <div class="received">Hey how are you?</div> -->
      </div>
      <form id="send-message">
        <input
          type="text"
          id="message"
          placeholder="Enter your message..."
          autocomplete="off"
        />
        <button type="submit"><i class="fas fa-paper-plane"></i></button>
      </form>
    </div>

    <div class="toast">Hey, I'm here at the Top-right</div>
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css?family=Source+Sans+Pro&display=swap");

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  font-family: "Source Sans Pro", sans-serif;
  background-color: #ecf0f1;
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23bdc3c7' fill-opacity='0.35'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

.container {
  width: 100%;
  height: 100%;

  .status {
    position: fixed;
    top: 0;
    width: 70%;
    margin-left: 50%;
    transform: translateX(-50%);
    background-color: #2ecc71;
    color: #fff;
    padding: 8px;
    border-radius: 3px;
    font-weight: 600;
  }

  .messages {
    background-color: #ecf0f1;
    background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23bdc3c7' fill-opacity='0.35'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
    max-width: 70%;
    margin: 1rem auto;
    display: flex;
    flex-direction: column;

    & > div {
      padding: 8px 12px;
      border: 1px solid #c4c4c4;
      width: auto;
      margin: 8px;
      // border-radius: 5px;
      background-color: #fff;

      .username {
        font-size: 12px;
        color: #3498db;
        margin-bottom: 4px;
        font-weight: 600;
      }

      &:last-child {
        margin-bottom: 3rem;
      }
    }
    .received {
      align-self: flex-start;
      border-radius: 5px 8px 6px 0;
    }
    .sent {
      align-self: flex-end;
      border-radius: 5px 0 6px 8px;
    }
  }

  form {
    position: fixed;
    bottom: 2px;
    width: 70%;
    display: flex;
    margin-left: 50%;
    transform: translateX(-50%);

    input {
      width: calc(100% - 100px);
      outline: none;
      padding: 12px;
      border: 1px solid #747474;
      border-radius: 4px;
      margin-right: 2px;
      font-size: 16px;
    }

    button {
      width: 100px;
      padding: 12px 18px;
      border: none;
      background-color: #2ecc71;
      color: #fff;
      font-size: 22px;
      outline: none;
      border-radius: 0 5px 5px 0;
      cursor: pointer;
      transition: 0.2s all;

      &:active {
        transform: scale(0.97);
      }
    }
  }
}

.toast {
  display: none;
  position: absolute;
  padding: 12px 30px;
  background-color: #57606f;
  color: #fff;
  border-radius: 3px;
  font-size: 14px;
  cursor: pointer;
  z-index: 99;
  top: 22px;
  right: 12px;
}
.show-top {
  display: block;
  animation: animate-top 0.1s forwards;
}
@keyframes animate-top {
  to {
    transform: translateY(8px);
  }
}

// Mobile
@media screen and (max-width: 700px) {
  .container {
    width: 100%;

    .messages {
      max-width: 100%;
    }
    form {
      width: 100%;
      margin-left: 0;
      transform: translateX(0);
    }

    .status {
      width: 100%;
    }
  }
}

              
            
!

JS

              
                const name = prompt("Enter your nick name");

// Init socket.io
const socket = io("https://warm-ravine-44332.herokuapp.com");


// Elements
const messages = document.querySelector(".messages"),
  msg = document.getElementById("message"),
  toast = document.querySelector(".toast");

// Connected
socket.emit("connected", name);

// Message received
socket.on("message", msg => {
  appendMessage(msg.text, msg.name, "received");
  // console.log(msg);
});

// Another user joined
socket.on("user-connected", name => {
  showToast(`${name} has joined`);
});

// User leaved
socket.on("user-disconnected", function(name) {
  // console.log(name);
  showToast(`${name} leaved`);
});

// Form submit/ message sent
document.getElementById("send-message").addEventListener("submit", e => {
  e.preventDefault();
  // Broadcast message

  if (msg.value !== "") {
    socket.emit("message", msg.value);
    appendMessage(msg.value, "You", "sent");
    msg.value = "";
  } else {
    return false;
  }
});

// Append message
function appendMessage(msg, user_name, class_name) {
  let message = document.createElement("div");
  message.classList.add(class_name);
  message.innerHTML = `
    <span class="username">${user_name}</span>
    <br/>
    ${msg}
  `;
  messages.appendChild(message);
  window.scrollTo(0, messages.scrollHeight);
}

// Notification
function showToast(msg) {
  toast.innerHTML = msg;
  toast.classList.add("show-top");
  setTimeout(() => {
    toast.classList.remove("show-top");
  }, 3000);
}

window.onload = msg.focus();

              
            
!
999px

Console