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.
///////////////////////////////// Invoking Functions ////////////////////////////////
// The code inside of a function will not be executed until the function is told to do so.
// When we tell a function to run, we are 'invoking' the function.
// Functions can also be invoked by an event. When a specific event occurs, the function is ordered to execute.
// The event that invokes a function can be anything. Usually, you will see events such as button clicks triggering functions. For example, when a user fills out a form online and clicks the "submit" button, the function that sends the form's data is invoked and the data is sent to the correct location.
// Functions can also be invoked by themselves, this is called self invocation.
// We will visit each of these methods of invocation, beginning with user invocation:
// To invoke a function we have written, or a function in the JavaScript library, we simply type the function's name, followed by a pair of parenthesis.
// If the function requires parameters, we will provide the arguments for these parameters within the parenthesis after the function's name. Otherwise, we invoke functions without parameters by including the pair of empty parenthesis.
// Examples:
// 1. A Simple Adding Function
function adder(value1, value2, value3){
return value1 + value2 + value3;
}
// the function, named adder, takes three parameters.
// the function returns the sum of all three parameters.
// the keyword "return" is the indicator of the last line of a function. If any code is placed beyond the return statement, the code will not be executed, as the function exits after executing its return statement.
// to invoke the function and retrieve the value it returns to us, we can either save the return value to a variable or print the value to the console, like so:
let sumOfNumbers = adder(45,67,23);
console.log(sumOfNumbers);
// Orrrr:
console.log(adder(45,67,23));
// Both methods will achieve the same result, but you can see that the second option is more efficient
// As you can see on line 42, we can pass arguments directly into a function without having to store them as variables first.
// 2. A Simple Current-Date Printing Function:
function printDate(){
let today = new Date();
return today.getMonth() + " / " + today.getDate() + " / " + today.getFullYear();
}
// the function, named printDate, uses the JavaScript Date object methods getMonth(), getDate(), and getYear() to print the current date in this format: MM / DD / YYYY
// to use the methods for the JavaScript Date object, we created a new variable named today and declared that it was a date object. You should remember objects from the lesson on data types. Objects can have methods that act like functions, which we will visit in a few lessons, so don't worry if you are confused about this function.
// The purpose of this function(for us) is to see an example of a parameter-less function.
// To invoke the printDate function, we simply save the return value to a variable or print the invocation to the console, like so:
let todaysDate = printDate();
console.log(todaysDate);
// orrrr:
console.log(printDate());
// Note that we did not pass in arguments in either method because the function does not have parameters.
///////////////////////// Exercises /////////////////////////
// 1. Write a function named "multiply" that takes two parameters: x and y, and logs the product of these two parameters to the console.
// 2. Invoke the multiply function with three different sets of arguments (the function will be invoked 3 separate times)
// 3. Declare a new function named 'printer' that takes one parameter named 'printContent'
// 4. Within the printer function, console.log the value of printContent printed out to the console in this format:
// "New print job: ___(value of printContent will go here)____."
// 5. Invoke the printer function 3 times with 3 separate arguments passed in for the printContent parameter
/////////Extra Exercises:
//1. This function is going to create a prompt that asks for a name, and then print out your name, with a message.
//First, use the function keyword. Then, name your function, something like namePrompt.
//Give your function the parameter "message" in parentheses.
//Add some curly brackets. Our code will go inside.
//Inside your curly brackets, create a variable called namePrompt, which stores a prompt that contains the text “What is your name”
//Below that, log the variable myPrompt, plus message into the console.
//Now, invoke your function! Type out the name of your function, and in the parentheses, write a message (as a single string!) that you would like to give to yourself.
//2. Create an object called me. Within it, store your name, age, and favorite song.
// Create a function called personalAlerts. Give it the parameters name, age, and favoriteSong.
// In the curly brackets, create three alerts. The first one should have the text "My name is", and then add the name parameter.
// The second one should have the text "My age is" and then add the age parameter.
// The third one should have the text "My favorite song is" and then add the favoriteSong parameter.
// Invoke the function, and pass the data from your object into it!
//3. We are going to create a function to covert fahrenheit to celsius! The formula for this is: ((fahrenheit - 32) * 5) / 9
// First, use the function keyword, and name your function something like fahrenheitConverter. Give the function the parameter fahrenheit.
// In the curly brackets, create a variable called celsius that stores the formula (you can copy it from above!)
// Next, print out the fahrenheit parameter, and add "F is equal to ", then add the celsius variable, and add "C".
// Finally, invoke your function by writing it out, and adding a number in the parentheses, which will be a fahrenheit variable! Make sure that 32 degrees fahrenheit is equal to 0 degrees celsius!
//4. We’re going to make a function that prints out the date as a string.
//First, type the function keyword.
//Next, name your function, something like printDate, and give it the parameter date.
//In your curly brackets, log the today parameter with the toString() method into the console.
//Now, invoke your function, and create a new date object in place of the today parameter!
//You can also add a different date by putting one into your date object!
//5. Create a function that rounds a number to two decimal places! Use number as the parameter. If you don't remember how to round to two decimal places, google it!
Also see: Tab Triggers