HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!-- Your HTML here -->
<button class="toggle">
<div class="name">John Wick</div>
</button>
<div class="chat" aria-hidden="true">
<div class="window">
<div class="bubble guest">
Hi man! Lorem ipsum dolor sit amet consectetur adipisicing elit.
Aspernatur esse commodi.
</div>
<div class="bubble own">
Sounds great!
</div>
</div>
<div class="input">
<input type="text" class="field" />
<button class="send">Send</button>
</div>
</div>
:root {
--chat-width: 250px;
}
.chat {
position: fixed;
right: var(--space-4);
bottom: var(--space-12);
width: var(--chat-width);
border: var(--border);
border-radius: var(--radius);
opacity: 0;
overflow: hidden;
transform: translateY(var(--space-8)) scale(0.9);
transform-origin: right;
pointer-events: none;
transition: var(--transition);
background-color: var(--color-white);
}
.chat.visible {
opacity: 1;
pointer-events: all;
transform: translateY(0) scale(1);
}
.window {
display: flex;
flex-direction: column;
height: var(--chat-width);
padding: var(--space-3);
overflow-y: scroll;
background-color: var(--color-white);
}
.bubble {
max-width: 80%;
font-size: var(--text-sm);
padding: var(--space-1) var(--space-2);
margin-bottom: var(--space-1);
align-self: flex-start;
word-break: break-word;
background-color: var(--color-gray-100);
border-radius: var(--radius);
}
.bubble:last-child {
margin-bottom: 0;
}
.own {
align-self: flex-end;
background-color: var(--color-blue-500);
color: var(--color-white);
}
.own + .guest {
margin-top: var(--space-1);
}
.guest + .own {
margin-top: var(--space-1);
}
.input {
display: flex;
padding: var(--space-2);
border-top: var(--border);
}
.field {
font-size: var(--text-md);
padding: var(--space-1);
border: 0;
outline: none;
}
.send {
padding: var(--space-2) var(--space-3);
/* Align properly on FireFox */
margin-left: auto;
border: 0;
font-weight: bold;
border-radius: var(--radius);
background-color: var(--color-gray-100);
color: var(--color-gray-700);
transition: var(--transition);
outline: none;
}
.send:focus {
box-shadow: var(--shadow-focus);
}
.send.active {
cursor: pointer;
color: var(--color-white);
background-color: var(--color-blue-500);
}
/* Toggle */
.toggle {
font-size: var(--text-sm);
position: fixed;
bottom: var(--space-2);
right: var(--space-4);
min-width: var(--space-16);
padding: var(--space-2) var(--space-3);
border: var(--border);
border-radius: var(--radius);
outline: none;
user-select: none;
background-color: var(--color-white);
color: var(--color-gray-900);
transition: min-width var(--transition-speed) var(--transition-curve);
}
.toggle:hover,
.toggle:focus {
cursor: pointer;
border-color: var(--color-gray-300);
}
.toggle:active {
border-color: var(--color-gray-400);
}
.name {
display: flex;
align-items: center;
}
.name:before {
display: block;
content: "";
width: var(--space-2);
height: var(--space-2);
margin-right: var(--space-2);
border: 1px solid var(--color-green-600);
background-color: var(--color-green-400);
border-radius: var(--round);
}
// Selectors
const chatContainer = document.querySelector(".chat");
const chatField = document.querySelector(".field");
const sendButton = document.querySelector(".send");
const chatWindow = document.querySelector(".window");
const chatToggle = document.querySelector(".toggle");
const keyword = "coffee";
let menuOpen = false;
// Get chat toggle width
const originalWidth = chatToggle.offsetWidth;
// Set that width to be the minimum width of the toggle
chatToggle.style.minWidth = `${originalWidth}px`;
const addMsg = () => {
// Check if field has content (and not only spaces)
if (chatField.value.length > 0 && chatField.value.trim()) {
// Create a new chat bubble from the user (hence "own")
createBubble(chatWindow, chatField.value, "own");
// Check if message has certain keyword...
if (chatField.value.includes(keyword)) {
// ...and respond to it.
addResponse();
}
// Reset text field value
chatField.value = "";
// Disable send button
sendButton.classList.remove("active");
// Focus text field again
chatField.focus();
}
};
const createBubble = (destination, message, owner = "own") => {
const chatBubble = document.createElement("div");
chatBubble.className = "bubble";
// Add separate classes for own and friend's messages
if (owner == "own") {
chatBubble.classList.add("own");
} else {
chatBubble.classList.add("guest");
}
// Insert message within bubble (note SECURITY)
chatBubble.innerText = message;
// Append child to destination
destination.appendChild(chatBubble);
// Scroll to the bottom of the destination container (chat window)
destination.scrollTop = destination.scrollHeight;
};
const resizeToggle = (sourceWidth) => {
// If menu is not open, set the width of the toggle equal to the chat window
if (!menuOpen) {
chatToggle.style.minWidth = `${sourceWidth}px`;
} else {
chatToggle.style.minWidth = `${originalWidth}px`;
}
};
const toggleChat = (event) => {
// Get chat window width
chatWidth = chatContainer.offsetWidth;
// Resize chat toggle
resizeToggle(chatWidth);
// Show chat
chatContainer.classList.toggle("visible");
// Expand toggle
event.currentTarget.classList.toggle("open");
// Tell screen readers that the content is not hidden anymore
chatContainer.setAttribute("aria-hidden", false);
// Toggle menu open variable
menuOpen = !menuOpen;
// Prevent event from bubbling
event.stopPropagation();
// Focus on the text field
chatField.focus();
};
const closeChat = () => {
// Close chat
chatContainer.classList.remove("visible");
// Let screenreaders know that content is hidden
chatContainer.setAttribute("aria-hidden", true);
// Reset toggle to close state
chatToggle.classList.remove("open");
resizeToggle();
menuOpen = false;
// Take focus out of chat text field...
chatField.blur();
// ...and focus on the toggle instead
chatToggle.focus();
};
// Add event listeners to chat toggle and send button
chatToggle.addEventListener("click", toggleChat);
sendButton.addEventListener("click", addMsg);
// Add keyboard event listeners
window.addEventListener("keydown", function (e) {
if (e.key == "Enter") {
addMsg();
}
});
window.addEventListener("keydown", (e) => {
if (e.key == "Escape" && menuOpen) {
closeChat();
}
});
// Detect click outside the chat container to close chat
window.addEventListener("click", (e) => {
console.log("container:", chatContainer);
console.log("event target:", e.target);
if (menuOpen && !chatContainer.contains(e.target)) {
closeChat();
}
});
// Toggle send button state based on chatfield content
chatField.addEventListener("input", () => {
if (chatField.value.length > 0 && chatField.value.trim()) {
sendButton.classList.add("active");
} else {
sendButton.classList.remove("active");
}
});
// Add response to a message
const addResponse = () => {
setTimeout(() => {
createBubble(chatWindow, "Coffee sounds great!", "");
}, 1000);
};
Also see: Tab Triggers