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>Group Mad Libs</h1>
<div id="display"></div>
body {
text-align: center;
display: grid;
grid-template-columns: 100%;
font-family: sans-serif;
}
#display {
width: 50%;
margin: 0 auto;
line-height: 2em;
max-width: 30em;
}
.inputarea {
display: grid;
grid-template-columns: 30% 70%;
grid-column-gap: 4em;
}
.inputarea label {
text-align: right;
text-decoration: underline;
}
.answer {
font-weight: bold;
text-decoration: underline;
}
//LOOK HERE, edit this story and use NOUN or VERB or any of the parts of speech in the wordFinder line below
let yourMadLibStoryA =
`My name is PROPERNAME and I am the ADJECTIVE captain of the spaceship PROPERNAME. After NUMBER days, the ship's NOUN started to malfunction. My crew is ING-VERB everyday to keep morale ADJECTIVE. A repair NOUN is on the way with new PLURALNOUN. This has been a/an ADJECTIVE adventure!`;
let yourMadLibStory = 'Hello, I am absolutely ADJECTIVE to welcome to the ADJECTIVE NOUN hall of fame!!!! Just so you know, we no longer allow guests to bring any sort of NOUN, after PROPERNAME had a very ADJECTIVE incident... Anyway, our newest candidate is PROPERNAME, who is renowned for ING-VERB NUMBER PLURALNOUN!!!! Can you believe it??? '
let yourMadLibTitle = "Wacky Hall of Fame!!!";
//If you want to add your own parts of speech you could add them here
//By the way, this is called a REGULAR EXPRESSION
let wordFinder = /NOUN|VERB|ADJECTIVE|ADVERB|PLURALNOUN|NUMBER|PROPERNAME|LOCATION|ANIMAL|ING-VERB/g;
//this will find every part of speech word from wordFinder
let partsOfSpeech = yourMadLibStory.match(wordFinder); //e.g. ["ADJECTIVE","NOUN","VERB","NOUN"]
//this will make an array of everything else in your story that isn't a partOfSpeech
let chunksOfStory = yourMadLibStory.split(wordFinder); //e.g. ["part of story","another part"]
// Initialize Firebase our database, you don't have to touch this part
var firebaseConfig = {
apiKey: "AIzaSyBHcN0HVMwxxW-xBkXHYAm2X-kGWjxPzNk",
authDomain: "basiccrud-ffe19.firebaseapp.com",
databaseURL: "https://basiccrud-ffe19.firebaseio.com",
projectId: "basiccrud-ffe19",
storageBucket: "basiccrud-ffe19.appspot.com",
messagingSenderId: "47974982638",
appId: "1:47974982638:web:9a034202ca787ee47764ff"
};
firebase.initializeApp(firebaseConfig);
function getUniqueId() { //a silly function just to let you fork this codepen and have your own database
var CODEPEN_ID = /[codepen|cdpn]\.io\/[^/]+\/(?:pen|debug|fullpage|fullembedgrid)\/([^?#]+)/;
var id;
if(CODEPEN_ID.test(window.location.href)) {
id = CODEPEN_ID.exec(window.location.href)[1];
} else if (CODEPEN_ID.test(document.location.href)){
id = CODEPEN_ID.exec(window.location.href)[1];
} else {
var metas = document.getElementsByTagName('link');
for(i=0;i<metas.length;i++) {
if(metas[i].getAttribute('rel') == 'canonical') {
if(CODEPEN_ID.test(metas[i].getAttribute('href')))
id = CODEPEN_ID.exec(metas[i].getAttribute('href'))[1];
}
}
}
return id || `randoDB${Math.floor(Math.random()*10000000)}`;
}
//this is the "handler" for YOUR database where we can store and read values from anyone at your site!
let yourDatabase = firebase.database().ref("madlibs").child(getUniqueId());
let showEmptyQuestions = function(){
$("#display").html(`<h2 class="title">${yourMadLibTitle}</h2>`);
for(let i=0; i < partsOfSpeech.length; i++){
$("#display").append(
`
<div class="inputarea">
<label>${partsOfSpeech[i]}:</label>
<input class="speechparts" placeholder="${partsOfSpeech[i]}" data-index="${i}"/>
</div>
`);
}
$("#display").append(`<button id="finish" disabled=true>All Done</button>`);
$(".speechparts").on("keyup", function(evt){
let answeredIndex = $(evt.currentTarget).attr("data-index");
let newAnswer = $(evt.currentTarget).val();
yourDatabase.child("currentAnswers").child("answers").child(answeredIndex).set(newAnswer);
});
$("#finish").click(function(){
yourDatabase.child("currentAnswers").child("hideStory").set(false);
});
}
showEmptyQuestions();
let showQuestions = function(answerArray){
if (answerArray.length == 0){
showEmptyQuestions();
}
let numberOfAnswers = 0;
for(let i=0; i < partsOfSpeech.length; i++){
let answer = ""; //for each question, check for answer, display an input box
if (answerArray.hasOwnProperty(i)){
answer = answerArray[i];
$(`.speechparts[data-index=${i}]`).val(answer);
}
if (answer.length > 0){
numberOfAnswers += 1;
}
}
if (numberOfAnswers == partsOfSpeech.length){
//We have an answer for every word, let's show the story
$("#finish").attr("disabled", false);
}
};
let showStory = function(answerArray){
$("#display").html(`<h2 class="title">${yourMadLibTitle}</h2>`);
for(let i=0; i < partsOfSpeech.length; i++){
let answer = answerArray[i] || ""; //for each question, check for answer, display an input box
$("#display").append(`${chunksOfStory[i]}<span class="answer">${answerArray[i]}</span>`);
}
$("#display").append(`${chunksOfStory[partsOfSpeech.length] || ""}`);
$("#display").append(`<div><button id="reset">Play Again?</button></div>`);
$("#reset").on("click", function(){
yourDatabase.child("currentAnswers").set({hideStory: true});
});
};
//This is where the action lives
yourDatabase.child("currentAnswers").on("value", function(dataSnapshot){
//this function gets called every time the page is loaded AND when someone updates an answer
//it's job is to draw the database values onto our screen
//we use the hideStory key to decide if we're still gathering answers
//or showing the finished product
let everyOneElsesAnswers = dataSnapshot.val();
if (everyOneElsesAnswers){
//there was at least one answer
if (!everyOneElsesAnswers.hasOwnProperty("hideStory")){
//if this is the FIRST time connecting to the DB (i.e. you just forked)
yourDatabase.child("currentAnswers").child("hideStory").set(true);
everyOneElsesAnswers.hideStory = true;
}
if (everyOneElsesAnswers.hideStory){
showQuestions(everyOneElsesAnswers.answers || []); //show questions
} else {
showStory(everyOneElsesAnswers.answers); //show results
}
} else {
showQuestions([]); //an empty array
}
});
Also see: Tab Triggers