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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
<h1>Todo</h1>
<form id="todo-form">
<label for="todo-item">What do you need to do?</label>
<input type="text" name="todo-item" id="todo-item">
<button class="btn" id="add-todo">Add Todo</button>
</form>
<div id="app"></div>
/**
* Add box sizing to everything
* @link http://www.paulirish.com/2012/box-sizing-border-box-ftw/
*/
*,
*:before,
*:after {
box-sizing: border-box;
}
/**
* Layout
*/
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
font-size: 112.5%;
margin-left: auto;
margin-right: auto;
max-width: 40em;
width: 88%;
}
/**
* Forms
*/
label,
[type="text"] {
display: block;
width: 100%;
}
label {
margin-bottom: 0.25em;
}
[type="text"] {
font-size: inherit;
margin-bottom: 1em;
padding: 0.25em 1em;
}
[type="checkbox"] {
margin-right: 0.5em;
}
/**
* Buttons
*/
.btn {
background-color: #0088cc;
border: 1px solid #0088cc;
border-radius: 1px;
color: #ffffff;
display: inline-block;
font-size: 0.9375em;
font-weight: normal;
line-height: 1.2;
margin-right: 0.3125em;
margin-bottom: 0.3125em;
padding: 0.5em 0.6875em;
}
.btn:hover,
.btn:focus,
.btn:active {
background-color: #005580;
border-color: #005580;
color: #ffffff;
text-decoration: none;
}
/**
* Todos
*/
.todos {
list-style: none;
margin-left: 0;
padding-left: 0;
}
.todos .completed {
text-decoration: line-through;
}
.todo:before,
.todo:after {
display: table;
content: " ";
}
.todo:after {
clear: both;
}
.todo label {
width: auto;
}
.todo label,
.todo button {
float: left;
}
.todo button {
margin-left: 0.5em;
}
let {signal, component} = reef;
// Get the todo field
let todoField = document.querySelector('#todo-item');
// Create signal
let todos = signal(getTodos());
/**
* Save todo items to local storage
*/
function saveTodos () {
localStorage.setItem('todos', JSON.stringify(todos));
}
/**
* Get todo items from local storage
* @return {Array} The todo items (or an empty array if none exist)
*/
function getTodos () {
let todos = localStorage.getItem('todos');
if (todos) return JSON.parse(todos);
return [];
}
/**
* Add a todo to the list
*/
function addTodo (event) {
// Only run if there's an item to add
if (todoField.value.length < 1) return;
// Prevent default form submission
event.preventDefault();
// Update the state
todos.push({
item: todoField.value,
id: crypto.randomUUID(),
completed: false
});
// Clear the input field and return to focus
todoField.value = '';
todoField.focus();
}
/**
* Mark a todo as complete (or incomplete)
* @param {Node} item The todo item
*/
function completeTodo (item) {
// Get the todo item
let todoItem = todos[item.getAttribute('data-todo')];
if (!todoItem) return;
// If it's completed, uncomplete it
// Otherwise, mark is as complete
if (todoItem.completed) {
todoItem.completed = false;
} else {
todoItem.completed = true;
}
}
/**
* Delete a todo list item
* @param {Node} btn The delete button that was clicked
*/
function deleteTodo (btn) {
// Get the index for the todo list item
let index = btn.closest('.todo').querySelector('input').getAttribute('data-todo');
if (!index) return;
// Remove the item from state
todos.splice(index, 1);
}
/**
* Edit a todo list item
* @param {Node} btn The edit button that was clicked
*/
function editTodo (btn) {
// Get the todo list DOM element
let todoListItem = btn.closest('.todo');
if (!todoListItem) return;
// Get the index for the item
let index = todoListItem.querySelector('input').getAttribute('data-todo');
if (!index) return;
// Get the item from state
let todoItem = todos[index];
if (!todoItem) return;
// Update state
todoItem.edit = true;
}
function saveEditTodo (event) {
// Prevent default form submit
event.preventDefault();
// Get the todo list DOM node
let newTodo = event.target.querySelector('.todo-update');
if (!newTodo) return;
// Get the todo list item index
let index = newTodo.getAttribute('data-todo-edit');
if (!index) return;
// Get the item from state
let todoItem = todos[index];
if (!todoItem) return;
// If the item is empty, delete the todo item
// Otherwise, update it
if (newTodo.value.length < 1) {
todos.splice(index, 1);
} else {
todoItem.item = newTodo.value;
todoItem.edit = false;
}
}
/**
* Remove all todo items from state
*/
function clearTodos () {
todos.splice(0, todos.length);
}
/**
* Handle click events
*/
function clickHandler (event) {
// Complete todos
let todo = event.target.closest('[data-todo]');
if (todo) {
completeTodo(todo);
}
// Edit todo
let editBtn = event.target.closest('.todo-edit');
if (editBtn) {
editTodo(editBtn);
}
// Delete todo
let deleteBtn = event.target.closest('.todo-delete');
if (deleteBtn) {
deleteTodo(deleteBtn);
}
// Clear all todos
if (event.target.closest('.todo-clear')) {
clearTodos();
}
}
/**
* Handle form submit events
*/
function submitHandler (event) {
// If it's the "new todo" form, add the item
if (event.target.matches('#todo-form')) {
addTodo(event);
}
// If it's the "edit todo" form, save the edit
if (event.target.matches('.todo-edit-form')) {
saveEditTodo(event);
}
}
/**
* Create template
*/
function template () {
// Create each todo item
let html = todos.map(function (todo, index) {
// If it's being edited, show a form
// Otherwise, show the item with a checkbox
if (todo.edit) {
return `
<li class="todo" id="todo-${todo.id}">
<form class="todo-edit-form">
<input type="text" class="todo-update" value="${todo.item}" data-todo-edit="${index}">
<button>Save</button>
</form>
</li>`;
} else {
return `
<li class="todo" id="todo-${todo.id}">
<label ${todo.completed ? ' class="completed"' : ''}>
<input data-todo="${index}" type="checkbox" ${todo.completed ? ' checked="checked"' : ''}>
<span class="todo-item">${todo.item}</span>
</label>
<button class="todo-edit">Edit</button>
<button class="todo-delete">Delete</button>
</li>`;
}
}).join('');
// If there are todo items, wrap it in an unordered list
if (html.length > 0) {
return `<ul class="todos">${html}</ul><p><button class="todo-clear">Clear All Todos</button></p>`;
}
return '';
}
// Create reactive component
component('#app', template);
// Listen for events
document.addEventListener('submit', submitHandler);
document.addEventListener('click', clickHandler);
document.addEventListener('reef:signal', saveTodos);
Also see: Tab Triggers