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.
<!-- Starter code: heading and empty flag container -->
<h1 id='gradient'>Happy Independence Day!</h1>
<div id='flag'></div>
/* Starter Code: all CSS properties needed for exercise */
/* Read the comments and play with the values to see how they work! */
#gradient {
/* Fallback bg color: */
background-color: red;
/* Center text on page */
text-align: center;
/* Create gradient */
background: radial-gradient(circle, blue 0%, white 10%, red 20%);
/* Mask the background so only the text is seen */
-webkit-background-clip: text;
/* Set text to transparent so gradient shows through */
-webkit-text-fill-color: transparent;
/* Add blue text border (width and color) */
-webkit-text-stroke: 1px blue;
}
#flag {
/* Manually set flag dimensions */
height: 300px;
width: 500px;
/* Add border */
border: 1px solid black;
/* Center flag on page */
margin: 0 auto;
position: relative;
}
#union {
/* Set color and dimensions */
background-color: blue;
height: 53.85%;
width: 40%;
/* Position union on top of other divs */
z-index: 1;
/* Place union in upper left corner of flag div */
position: absolute;
top: 0;
left: 0;
text-align: center;
display: flex;
/* Children displayed top to bottom */
flex-direction: column;
justify-content: space-between;
}
.star-row {
/* Use flexbox */
/* Children displayed left to right by default */
display: flex;
justify-content: space-between;
/* Add small margin around rows */
margin: 3px;
}
.five {
/* Add spacing so stars are between top and bottom stars */
padding: 0px 9%;
}
.fa-star {
/* Set color and size of star icons */
color: white;
font-size: 12px;
}
#stripes {
/* Set stripes to fill available space */
height: 100%;
width: 100%;
/* Use Flexbox */
display: flex;
/* Children displayed top to bottom */
flex-direction: column;
}
.red {
/* Set color */
background-color: red;
/* Set stripe to fill available space */
width: 100%;
height: 100%;
}
.white {
/* Set color */
background-color: white;
/* Set stripe to fill available space */
width: 100%;
height: 100%;
}
// function to add stripes to flag
const stripes = () => {
// find flag in html
let flag = document.getElementById("flag");
// create new div for stripes section
let stripes = document.createElement("div");
// set CSS id for stripes section
stripes.id = "stripes";
// enter for loop to create 13 stripes
for (let i = 0; i < 13; i++) {
// create individual stripe
let stripe = document.createElement("div");
// if the remainder of i / 2 is 0 (even number)
if (i % 2 === 0) {
// set class to red
stripe.className = "red";
// if the remainder of i / 2 is not 0 (odd number)
} else {
// set class to white
stripe.className = "white";
// close if statment
}
// add stripe to stripes container
stripes.appendChild(stripe);
// close for loop
}
// add stripes container to flag
flag.appendChild(stripes);
// close function
};
// function to add stars to flag
const stars = () => {
// find flag in HTML
let flag = document.getElementById("flag");
// create new div for union (blue star section)
let union = document.createElement("div");
// set union's id for CSS
union.id = "union";
// for loop to add the 9 rows of stars
for (let i = 0; i < 9; i++) {
// create div for row of stars
let row = document.createElement("div");
// create empty variable for number of stars in row
let num;
// if the remainder of i / 2 is 0 (even number)
if (i % 2 === 0) {
// set class names
row.className = "star-row six";
// set number of stars to 6
num = 6;
// if the remainder of i / 2 is not 0 (odd number)
} else {
// set class names
row.className = "star-row five";
// set number of stars to 5
num = 5;
// close if statement
}
// enter for loop to add stars to row
for (let j = 0; j < num; j++) {
// add star icon's HTML (from fontAwesome) to row div
row.innerHTML += '<i class="fas fa-star"></i>';
// close j for loop
}
// add row to union
union.appendChild(row);
// close i for loop
}
// add union to flag
flag.appendChild(union);
// close function
};
// Call functions to add flag to the DOM
stripes();
stars();
Also see: Tab Triggers