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.
<header class="site-header">
<div class="site-title">
A Serverless Blog
</div>
<div class="site-subtitle">
All the code that powers this blog is right here.
</div>
<a id="sign-in-button" class="button sign-in-button">
<svg width="24" height="24" viewBox="0 0 24 24">
<path d="M12,4A4,4 0 0,1 16,8A4,4 0 0,1 12,12A4,4 0 0,1 8,8A4,4 0 0,1 12,4M12,14C16.42,14 20,15.79 20,18V20H4V18C4,15.79 7.58,14 12,14Z"></path>
</svg> Sign In
</a>
<a id="new-post-button" class="button sign-in-button" style="display:none">
<svg width="24" height="24" viewBox="0 0 24 24">
<path d="M20.71,4.04C21.1,3.65 21.1,3 20.71,2.63L18.37,0.29C18,-0.1 17.35,-0.1 16.96,0.29L15,2.25L18.75,6M17.75,7L14,3.25L4,13.25V17H7.75L17.75,7Z"></path>
</svg> New Post
</a>
</header>
<main class="site-wrap">
<section id="new-post" class="article-form" style="display: none;">
<h2>New Post</h2>
<form id="message-form" action="#">
<h4><label for="new-post-title">Title</label></h4>
<input type="text" id="new-post-title">
<br />
<br />
<h4><label for="new-post-message">Message</label></h6>
<textarea rows="3" id="new-post-content"></textarea>
<br />
<br />
<button type="submit">Add post</button>
</form>
</section>
<div id="articles-list">
<div class="articles-container" />
</div>
<article id="article-whole" class="article-whole start-hidden" >
<div></div>
</article>
</main>
<script src="https://www.gstatic.com/firebasejs/4.8.0/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "AIzaSyDEpThYG_vYML09Q5HEY0RtkqzqGtq1XZM",
authDomain: "duncan-131.firebaseapp.com",
databaseURL: "https://duncan-131.firebaseio.com",
projectId: "duncan-131",
storageBucket: "duncan-131.appspot.com",
messagingSenderId: "631406093601"
};
firebase.initializeApp(config);
</script>
* {
box-sizing: border-box;
}
.site-header {
border-top: 1rem solid #556CF6;
background: #26232d;
padding: 2rem;
position: relative;
}
.site-title {
font-size: 2rem;
letter-spacing: 3px;
}
.site-subtitle {
font-style: italic;
opacity: 0.5;
}
.sign-in-button {
position: absolute;
top: 1rem;
right: 1rem;
}
.site-wrap {
padding: 2rem;
}
.article-form,
.article-block {
max-width: 650px;
margin: 0 auto 2rem;
background: #403E4F;
padding: 2rem;
box-shadow: 0.25rem 0.25rem 1.3rem rgba(0, 0, 0, 0.33);
}
time {
display: block;
padding: 0.25rem 0.75rem;
text-transform: uppercase;
letter-spacing: 0.25rem;
background: linear-gradient(
to right,
#4a4c5a,
#403E4D
);
font-size: 0.75rem;
}
.article-body {
color: #9e9fa8;
}
.article-whole {
max-width: 850px;
margin: 0 auto;
}
body {
margin: 0;
background: #323041;
color: white;
font-family: 'PT Serif', serif;
line-height: 1.4;
}
h1, h2, h3, h4, h5, h6,
.site-title {
font-family: 'Barlow Semi Condensed', sans-serif;
text-transform: uppercase;
}
h1, h2, h3, h4, h5, h6 {
margin: 0 0 1rem 0;
letter-spacing: 0.1rem;
line-height: 1;
}
h1 {
font-size: 3.0rem;
letter-spacing: 0.1rem;
}
a {
color: #8b9bff;
font-weight: bold;
border-bottom: 1px solid;
text-decoration: none;
padding: 0.25rem 0.5rem;
}
a svg {
fill: currentcolor;
vertical-align: bottom;
}
a:hover,
a:focus {
background: rgba(0, 0, 0, 0.33);
}
.start-hidden {
display: none;
}
input, textarea {
width: 100%;
}
var blog_api_url = 'https://us-central1-duncan-131.cloudfunctions.net/posts';
var posts_list = document.getElementById('articles-list');
var post_full = document.getElementById('article-whole');
var posts_container = posts_list.querySelector('.articles-container');
// track authenticated user to avoid triggering on refresh
var currentUID;
var loadJsonFromFirebase = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function () {
callback(JSON.parse(this.response));
});
xhr.open("GET", url);
xhr.send();
};
var getQueryParam = function(param) {
let params = window.location.search.substr(1);
params = params.split("&");
let paramList = {};
for (let i = 0; i < params.length; i++) {
let tmp = params[i].split("=");
paramList[tmp[0]] = decodeURI(tmp[1]);
}
return paramList[param];
}
// hide the individual post and show the list of posts
const showListClick = (e) => {
// hide the single post
post_full.classList.add('start-hidden');
// show the full list
posts_container.classList.remove('start-hidden');
}
// handle selecting the links in the list
const showPostClick = (e) => {
let post_id = e.target.dataset.post;
if (post_id) {
// load the post from ajax call
loadJsonFromFirebase(blog_api_url + '/' + post_id, function(data) {
let div = document.createElement('div');
let ts = new Date(data.created);
div.innerHTML = `<article class="article-block">
<h2>${data.title}</h2>
<time>${ts.toDateString()}</time>
<div class="excerpt">
<p>${data.content}</p>
</div>
</article>`;
post_full.replaceChild(div, post_full.firstChild);
// hide the full list
posts_container.classList.add('start-hidden');
// show the single post
post_full.classList.remove('start-hidden');
});
}
};
// toggle buttons on sign in/out auth changes
const onLogInOutChange = function(user) {
// Ignore token refresh events
if (user && currentUID === user.uid) {
return;
}
// If logged in, show the new post button
if (user) {
currentUID = user.uid;
document.getElementById('sign-in-button').style.display = 'none';
document.getElementById('new-post-button').style.display = 'block';
} else {
currentUID = null;
document.getElementById('sign-in-button').style.display = 'block';
document.getElementById('new-post-button').style.display = 'none';
}
}
//document.getElementById('post-view-all').addEventListener('click', showListClick);
posts_list.addEventListener('click', showPostClick);
loadJsonFromFirebase(blog_api_url, function(data) {
let list = document.createElement('div');
Object.keys(data).forEach(function(key) {
let ts = new Date(data[key].created);
list.innerHTML += `<article class="article-block">
<h2>${data[key].title}</h2>
<time>${ts.toDateString()}</time>
<div class="excerpt">
<p>${data[key].content.substr(0, 150)}...</p>
</div>
<a data-post="${key}">Read Post</a>
</article>`;
});
posts_container.insertBefore(list, posts_container.firstChild);
});
// Bindings on load.
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('sign-in-button').addEventListener('click', function() {
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider);
});
// Listen for auth state changes
firebase.auth().onAuthStateChanged(onLogInOutChange);
// show the new post form
document.getElementById('new-post-button').addEventListener('click', function() {
document.getElementById('new-post').style.display = '';
});
// Saves message on form submit.
let messageForm = document.getElementById('message-form');
messageForm.onsubmit = function(e) {
e.preventDefault();
let postTitle = document.getElementById('new-post-title');
let postContent = document.getElementById('new-post-content');
let title = postTitle.value;
let content = postContent.value;
if (content) {
firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function (idToken) {
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function () {
// on success, display the post at the top of the list & hide the form
postTitle.value = '';
postContent.value = '';
document.getElementById('new-post').style.display = 'none';
let postDetails = JSON.parse(this.response);
let div = document.createElement('div');
let ts = new Date(postDetails.created);
div.innerHTML += `<article class="article-block">
<h2>${postDetails.title}</h2>
<time>${ts.toDateString()}</time>
<div class="excerpt">
<p>${postDetails.content}</p>
</div>
</article>`;
posts_container.insertBefore(div, posts_container.firstChild);
});
xhr.open("POST", blog_api_url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify({"title": title, "content": content, "token": idToken}));
}).catch(function (error) {
// TODO: handle error, not authorized
});
} else {
// TODO: display box around the missing content field
}
}
});
Also see: Tab Triggers