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.
///////////////////////////////// Writing Methods ////////////////////////////////
// As mentioned earlier, objects can have methods that we can invoke to perform some kind of function or action on the object
// Methods are stored as an object property, but are programmed by defining a new function to assign the property to
// To look at a few examples, we will use the employee object below:
// The employee object will represent an employee of our imaginary company, Katie Johnson.
// This object stores the information for employee Katie Johnson, with properties for her name, ID, phone number, address, hourly wage, and number of hours worked this week.
// The last property of the employee object will be the method called "printEmployeeInfo"
// The printEmployeeInfo method declares a new function that returns the employee ID and full name in a formatted order
// You'll notice that the method uses a new keyword we haven't worked with yet, "this"
// When used in an object method, the 'this' keyword refers to the object in context.
// On line 30, this.firstName is equivalent to saying employee.firstName:
let employee = {
firstName: "Katie",
lastName: "Johnson",
employeeID: 295025,
phone: 4560001234,
address: "42 Wallaby Way",
wage: 13.5,
hoursWorked: 25,
printEmployeeInfo: function() {
return (
"Employee #" +
this.employeeID +
" : " +
this.firstName +
" " +
this.lastName
);
}
};
console.log(employee.printEmployeeInfo());
// Next, lets add a method to the employee object that calculates their weekly pay
// This method will be called "paycheck" and will return the product of the employee object's wage property and the employee object's hoursWorked property:
employee.paycheck = function() {
return "$" + this.wage * this.hoursWorked;
};
// Now, we can invoke the paycheck method like so:
let income = employee.paycheck();
console.log(income);
// or:
console.log(
"Employee #" +
employee.employeeID +
" worked " +
employee.hoursWorked +
" hours this week and earned " +
employee.paycheck()
);
// Try updating the hoursWorked property to a number greater than 25, and invoking the paycheck method again:
employee.hoursWorked = 40;
console.log(
"Employee #" +
employee.employeeID +
" worked " +
employee.hoursWorked +
" hours this week and earned " +
employee.paycheck()
);
// Finally, lets write an employee contacy information method.
// The method will be named "contact" and will print the information to the console in this order:
// Employee #ID - Last Name, First Name , Phone: (###)-###-#### , Address: __________
employee.contact = function() {
return (
"Employee #" +
this.employeeID +
" - " +
this.lastName +
", " +
this.firstName +
" , Phone: (" +
this.phone.toString().slice(0, 3) +
")-" +
this.phone.toString().slice(3, 6) +
"-" +
this.phone.toString().slice(6, 10) +
" , Address: " +
this.address
);
};
// Now we can invoke this method and print its return statement to the console:
console.log(employee.contact());
///////////////////////// Exercises /////////////////////////
//1. Create a new object representing a pizza place's customer with the following properties: customerName(string), orderID(number), order(string), orderTotal(number), payed(boolean)
// assign appropriate values to each of the object's properties
//2. Declare a new method named printOrder that returns the object's properties in this format: "Order #(Customer ID goes here): (order goes here) = (order total goes here)"
//3. Invoke the method and print it to the console
//4. Declare a new method called "pay" that changes the value of the payed boolean property to True
//5. Declare a new method called "receipt" that uses a conditional to check if the customer has payed, and if so, and returns "(Customer Name) payed (order total) for order #(orderID)"
// If the customer has not payed, the method should return "Awaiting customer payment for order #(order ID)"
//6. Call the receipt method before calling the pay method to ensure the else statement is executed when necessary.
//////////Extra Exercises:
//1. Create an object called me that stores information about you: add properties for your name, age, favorite food, and favorite emoji (from here: https://www.w3schools.com/charsets/ref_emoji.asp).
// Add a method called printEmoji, which prints out the favorite emoji property.
// Add another method which creates a new date object for today and your birthday, and uses an if statement to calculate whether you are over 16 or not (if you are print out your name, and "is over 16."/ If not, print out your name and "is under 16.")
//2. Create an object called me2. Give it the properties name, birthday, and currentDate. Give the birthday property a date object with the date of your birthday, and the currentDate a date object with todays date (just new Date()).
//Create a method called birthdayMonth.
//In an if statement, test if the month of your birthday is less than the current month, and if it is, return "I have had my birthday already this year". (you can use me.birthday.getMonth() and me.currentDate.getMonth() to compare the months).
// If your birth month is the same as the current month, return "My birthday is this month." If it is more than the current month, return out "My birthday will be later this year."
//Invoke your method inside a console.log
// 3. Create an object called pet
// Give the pet the properties type, age, name, and fill them out with relevant data.
// Make a method within your object to create an alert with the information, "My pet is called_____. They are a _____. They are _____ years old."
// Invoke your method to make sure its working.
//4. Create an object which contains the parameters highTemp and lowTemp. Set them equal to the high and low temperatures today (google it!)
//Create a method which returns the following text on your webpage: "The high temperature for today is:____. The low temperature for today is_____."
//Outside of your object, use document.querySelector to select the body of your webpage, and set the innerHTML equal to your method, so that the text displays on your webpage.
Also see: Tab Triggers