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 URL's added here will be added as <link>
s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
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.
If the stylesheet 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 CSS 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.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<h2>Document and Button Clicking</h2>
<p>Click this document to activate the handler.</p>
<div id="h1"></div>
<div id="h2"></div>
<button>Click me</button>
<button>Click me too</button>
<button>One more click</button>
<h2>Playing with Style Sheets</h2>
<p>This page turns violet when you hold the V key.</p>
<h2>MouseDown and MouseMove</h2>
<p>Drag the bar to change its width:</p>
<div id="bar">
</div>
<h2>Form Focus Events</h2>
<form>
<p>Name: <input type="text" data-help="Your full name"></p>
<p>Age: <input type="text" data-help="Your age in years"></p>
<p id="help"></p>
</form>
body {
font-size: 1.5em;
font-family: Arial;
}
#bar {
background: red;
width: 60px;
height: 20px;
}
// Mouse click event that will record when anyone clicks on anything within the document - Remember to open the Console to see the responses
// TO DO: Add a heading (h1) and another heading (h2) to the HTML section and see if the click event works for those
var flag = false;
window.addEventListener("click", () => {
console.log("You clicked?");
if(flag == false) {
var div = document.getElementById("h1");
div.innerHTML += "<h1>H1 Added!</h1>";
div = document.getElementById("h2");
div.innerHTML += "<h2>H2 Added!</h2>";
flag = true;
}
});
// This next example is a mouse click event that will trigger when someone clicks on the FIRST button on the page. What tells us that it will only work on the FIRST button?
let button = document.querySelector("button");
button.addEventListener("click", () => {
console.log("First Button clicked.");
});
// How can we modify this so that it will occur when the 2nd button is clicked?
// We need to use querySelectorAll which will produce a nodelist/array of all the buttons. Then we can reference which button we want to apply the click event using [] with the array element value - in this case 1
let second_button = document.querySelectorAll("button");
second_button[1].addEventListener("click", () => {
console.log("Second Button clicked.");
});
// TO DO - How can we modify this so that the events will occur when ANY button is clicked? Add a third button and create some code that will assign click events with the message "Any button clicked" in response to any button clicks. Think about it.
let any_button = document.querySelectorAll("button");
for(var i = 0; i<any_button.length; i++) {
any_button[i].addEventListener("click", () => {
console.log("Any button clicked");
});
}
// Let's explore a keypress event that triggers a change in the DOM. Think of this like applying a "theme" to our webpage design. In this first example, when someone presses the p button, the style changes to a purple background and white text.
window.addEventListener("keydown", event => {
if (event.key == "p") {
document.body.style.background = "purple";
document.body.style.color = "white";
}
});
// What if we only wanted the keypress to happen while the key is pressed, and to go back to normal once the key is no longer being pressed? We can remove the event listener like this:
window.addEventListener("keydown", event => {
if (event.key == "v") {
document.body.style.background = "violet";
}
});
window.addEventListener("keyup", event => {
if (event.key == "v") {
document.body.style.background = "";
document.body.style.color = "";
}
});
// This is a really fun example of how usingmousedown/mousemove events can allow a user to interact with an object on your webpage. One thing to note however is that the mouse events might not work on touchscreens.
let lastX; // Tracks the last observed mouse X position
let bar = document.getElementById("bar");
bar.addEventListener("mousedown", event => {
if (event.button == 0) {
lastX = event.clientX;
window.addEventListener("mousemove", moved);
event.preventDefault(); // Prevent selection
}
});
function moved(event) {
if (event.buttons == 0) {
window.removeEventListener("mousemove", moved);
}
else {
let dist = event.clientX - lastX;
let newWidth = Math.max(10, bar.offsetWidth + dist);
bar.style.width = newWidth + "px";
lastX = event.clientX;
}
}
// This is an example using a form. Form validation is a huge topic in web development and will need to be fully understood by students. Take a look at this example using the focus event on particular form elements. The data-help text is being pulled by the html attribute of the form element and that is what is outputting to the document
let help = document.querySelector("#help");
let fields = document.querySelectorAll("input");
for (let field of Array.from(fields)) {
field.addEventListener("focus", event => {
let text = event.target.getAttribute("data-help");
help.textContent = text;
});
field.addEventListener("blur", event => {
help.textContent = "";
});
}
Also see: Tab Triggers