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 class="headers" id= "header1"> This is my Header </h1>
<p id="para1" name="hello"> This is a paragraph, which includes some random text. </p>
<h1 class="headers" id="header2"> This is my second Header. It has the same class as my first, but a different id. </h1>
<p id=para2> This is another paragraph. Bla bla bla bla bla bla bla bla bla bla bla bla </p>
//Its very useful to be able to access HTML elements and CSS styling in JavaScript, so that, for example, we can program JavaScript to make changes to things like text and appearance based on conditional statements. In this section, we will learn how to access HTML and CSS in JavaScript, store elements in variables.
////////////1.1 document.querySelector()
//When we access HTML or CSS in JavaScript, we always start with the document object. We will learn two different methods that can be used to access HTML elements. The first one is querySelector().
//document.querySelector() can be used to access an element with no class or id, an element with an id, or the FIRST element of a specified class.
//If we want to access an element with an id, we use document.querySelector() and in the parentheses, we put a hash sign and the name of the id, all in quotes. We could access the first paragraph in our HTML like this:
//document.querySelector("#para1")
//If we want to change something about it, one of the easiest things to change is the text itself, and we change that with the property innerHTML, which we can set equal to a new text, like this:
//document.querySelector("#para1").innerHTML = "hello!";
//If I uncomment this code, it will change the text in the first paragraph!
///////////Practice////////////////
//1. In HTML, create two paragraphs, and give them both an id. No need to put any text.
//2. In JavaScript, access both paragraphs using document.querySelector(). Set the innerHTML of the first one equal to your name (as a string!). Set the second one to your favorite sport.
/////////////////////////////////
//If we want to access the first element of a class, we use the same procedure, but instead of using #, we use a period, to tell JavaScript that we are accessing a class, not an id.
//This is how we would access the first header, which has a class of "headers", and change its text:
//document.querySelector(".headers").innerHTML = "New Header Text";
//This would not affect the text of the other headers.
//To change the innerHTML of every element in the class, we would have to use the method querySelectorAll() in combination with a for loop, which we will learn later on.
///////////document.getElementById()//////////
//The second method we will learn is document.getElementById(), which is used only to access an element by its id name.
//For this method, we DO NOT use the hash sign (#), because JavaScript already knows that it is looking for an id. So if we want to change the text of the second header, using its id, we can do so like this:
//document.getElementById("header2").innerHTML = "New Text for the Second Header!";
//We can store elements in a variable, which allows use to use them in many different ways, for example:
//let header2 = document.getElementById("header2");
//Now that we have stored this in a variable, we can apply innerHTML to our variable, instead of typing out all of the code, like this:
//header2.innerHTML = "This is so easy!"
//No we can use the header2 variable storing our element in as many ways as we want!
//////////////Practice////////////////
//1. Create a paragraph in HTML and give it an id.
//2. In JavaScript, store your paragraph in a variable.
//3. Set the innerHTML to some random text using your variable.
///////////Accessing CSS in JavaScript
//In order to change CSS styling in JavaScript, we first need to access the HTML element that we want to style, and we do that in the ways we've just learned. Then, we add the style property, which specifies that we're accessing CSS, and then whatever specific CSS property we want to change, for example color. So, if we want to change the color of the text in our first paragraph, we could do it like this:
//document.getElementById("para1").style.color = "green"
//Notice that in CSS, we put the name of the color without quotes, but in JavaScript, we need to use quotes to make the color a string.
//We can also use variables which store elements, which is very useful, so we could change the style of our already specified header2 variable like this:
//header2.style.color = "orange"
//In CSS, we use dashes inbetween words for different properties, for example background-color, or font-size. JavaScript does not recognize dashes in properties, so in JavaScript, we use camelCase, so backgroundColor or fontSize.
//So if we wanted to change the background color of the body, we would do it like this:
//document.querySelector("body").style.backgroundColor = "violet"
//1.3 document.getElementsByTagName()
// The getElementsByTagName() method returns a collection of all elements with a specified tag name.
// This method returns an HTML collection, which is an array-like collection (list) of HTML elements.
//Let's look at an example:
let myTags = document.getElementsByTagName("p");
console.log(myTags);
//We can see that we get a list of the different paragraph elements in our document.
//We can now access each element by using square brackets, similar to accessing elements of an array, so if we want to change the text in the first paragraph element, we could uncomment the following code:
//myTags[0].innerHTML = "This is the first paragraph";
//Or to change the text in the second paragraph, uncomment the following code:
//myTags[1].innerHTML = "This is the second paragraph";
////////////////////////////////Exercises/////////////////////////////////////
//1. In HTML, create two headers and two paragraphs. Give them all their own id.
//2. In JavaScript, store each of your elements in a variable.
//3. Using innerHTML, set some text for each element.
//4. Using style.color, set a text color for each element.
//5. Access the body of the webpage in JavaScript, and set the background color.
////////////Extra Exercises:
//1. Create a function that changes the text of a header when the header is clicked on!
//First, make a header in HTML and give it an id and an onclick. Give it some text.
//Next, make a function in JavaScript with the same name as the onclick. Iside the function, store your header as a variable (using document.getElementbyId()) and set the innerHTML to something new.
//Now, click on your header! The text should change!
Also see: Tab Triggers