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.
<div id="page-wrapper" class="clearfix">
<h1>HTML5 Text Editor</h1>
<p>Powered by the FileSystem APIs.</p>
<form action="#" method="POST" id="file-form">
<div class="field">
<input type="text" name="filename" id="filename" placeholder="Filename (e.g. treehouse.txt)">
</div>
<div class="field">
<textarea name="content" id="content" placeholder="Type your content here..."></textarea>
</div>
<div class="field">
<button type="submit">Save File</button>
<div id="messages"></div>
</div>
</form>
<div id="files">
<h2>File Browser</h2>
<ul id="file-list"></ul>
</div>
</div>
*, *:before, *:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
html {
font-family: Helvetica, Arial, sans-serif;
font-size: 100%;
background: #333;
color: #33383D;
-webkit-font-smoothing: antialiased;
}
#page-wrapper {
width: 960px;
background: #FFF;
padding: 1.25rem;
margin: 1rem auto;
min-height: 300px;
border-top: 5px solid #69c773;
box-shadow: 0 2px 10px rgba(0,0,0,0.8);
}
h1 {
margin: 0;
}
h2 {
margin-top: 0;
font-size: 0.9rem;
text-transform: uppercase;
letter-spacing: 1px;
color: #999;
}
p {
font-size: 0.9rem;
margin: 0.5rem 0 1.5rem 0;
}
a,
a:visited {
color: #08C;
text-decoration: none;
}
a:hover,
a:focus {
color: #69c773;
cursor: pointer;
}
a.delete-file,
a.delete-file:visited {
color: #CC0000;
margin-left: 0.5rem;
}
#file-form {
width: 650px;
float: left;
}
.field {
margin-bottom: 1rem;
}
input,
textarea {
width: 100%;
padding: 0.5rem;
font-size: 1rem;
border: 1px solid #D9D9D9;
border-radius: 3px;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.1);
}
textarea {
min-height: 300px;
}
button {
display: inline-block;
border-radius: 3px;
border: none;
font-size: 0.9rem;
padding: 0.6rem 1em;
background: #86b32d;
border-bottom: 1px solid #5d7d1f;
color: white;
margin: 0 0.25rem;
text-align: center;
}
button:hover {
opacity: 0.75;
cursor: pointer;
}
#files {
width: 230px;
float: right;
}
#files ul {
margin: 0;
padding: 0.5rem 1rem;
height: 330px;
overflow-y: auto;
list-style: none;
background: #F7F7F7;
border: 1px solid #D9D9D9;
border-radius: 3px;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.1);
}
#files li {
padding: 0.5rem 0;
}
#messages {
display: inline-block;
font-weight: bold;
color: #69c773;
margin-left: 1rem;
}
/* Clearfix Utils */
.clearfix {
*zoom: 1;
}
.clearfix:before,
.clearfix:after {
display: table;
line-height: 0;
content: "";
}
.clearfix:after {
clear: both;
}
// Allow for vendor prefixes.
window.requestFileSystem = window.requestFileSystem ||
window.webkitRequestFileSystem;
// Create a variable that will store a reference to the FileSystem.
var filesystem = null;
// Get references to the page elements.
var form = document.getElementById('file-form');
var filenameInput = document.getElementById('filename');
var contentTextArea = document.getElementById('content');
var fileList = document.getElementById('file-list');
var messageBox = document.getElementById('messages');
// A simple error handler to be used throughout this demo.
function errorHandler(error) {
var message = '';
switch (error.code) {
case FileError.SECURITY_ERR:
message = 'Security Error';
break;
case FileError.NOT_FOUND_ERR:
message = 'Not Found Error';
break;
case FileError.QUOTA_EXCEEDED_ERR:
message = 'Quota Exceeded Error';
break;
case FileError.INVALID_MODIFICATION_ERR:
message = 'Invalid Modification Error';
break;
case FileError.INVALID_STATE_ERR:
message = 'Invalid State Error';
break;
default:
message = 'Unknown Error';
break;
}
console.log(message);
}
// Request a FileSystem and set the filesystem variable.
function initFileSystem() {
navigator.webkitPersistentStorage.requestQuota(1024 * 1024 * 5,
function(grantedSize) {
// Request a file system with the new size.
window.requestFileSystem(window.PERSISTENT, grantedSize, function(fs) {
// Set the filesystem variable.
filesystem = fs;
// Setup event listeners on the form.
setupFormEventListener();
// Update the file browser.
listFiles();
}, errorHandler);
}, errorHandler);
}
function loadFile(filename) {
filesystem.root.getFile(filename, {}, function(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onload = function(e) {
// Update the form fields.
filenameInput.value = filename;
contentTextArea.value = this.result;
};
reader.readAsText(file);
}, errorHandler);
}, errorHandler);
}
function displayEntries(entries) {
// Clear out the current file browser entries.
fileList.innerHTML = '';
entries.forEach(function(entry, i) {
var li = document.createElement('li');
var link = document.createElement('a');
link.innerHTML = entry.name;
link.className = 'edit-file';
li.appendChild(link);
var delLink = document.createElement('a');
delLink.innerHTML = '[x]';
delLink.className = 'delete-file';
li.appendChild(delLink);
fileList.appendChild(li);
// Setup an event listener that will load the file when the link
// is clicked.
link.addEventListener('click', function(e) {
e.preventDefault();
loadFile(entry.name);
});
// Setup an event listener that will delete the file when the delete link
// is clicked.
delLink.addEventListener('click', function(e) {
e.preventDefault();
deleteFile(entry.name);
});
});
}
function listFiles() {
var dirReader = filesystem.root.createReader();
var entries = [];
var fetchEntries = function() {
dirReader.readEntries(function(results) {
if (!results.length) {
displayEntries(entries.sort().reverse());
} else {
entries = entries.concat(results);
fetchEntries();
}
}, errorHandler);
};
fetchEntries();
}
// Save a file in the FileSystem.
function saveFile(filename, content) {
filesystem.root.getFile(filename, {create: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
// Update the file browser.
listFiles();
// Clean out the form field.
filenameInput.value = '';
contentTextArea.value = '';
// Show a saved message.
messageBox.innerHTML = 'File saved!';
};
fileWriter.onerror = function(e) {
console.log('Write error: ' + e.toString());
alert('An error occurred and your file could not be saved!');
};
var contentBlob = new Blob([content], {type: 'text/plain'});
fileWriter.write(contentBlob);
}, errorHandler);
}, errorHandler);
}
function deleteFile(filename) {
filesystem.root.getFile(filename, {create: false}, function(fileEntry) {
fileEntry.remove(function(e) {
// Update the file browser.
listFiles();
// Show a deleted message.
messageBox.innerHTML = 'File deleted!';
}, errorHandler);
}, errorHandler);
}
// Add event listeners on the form.
function setupFormEventListener() {
form.addEventListener('submit', function(e) {
e.preventDefault();
// Get the form data.
var filename = filenameInput.value;
var content = contentTextArea.value;
// Save the file.
saveFile(filename, content);
});
}
// Start the app by requesting a FileSystem (if the browser supports the API)
if (window.requestFileSystem) {
initFileSystem();
} else {
alert('Sorry! Your browser doesn\'t support the FileSystem API :(');
}
Also see: Tab Triggers