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.
body {
margin: 0;
padding: 0;
font-family: 'Open Sans', sans-serif;
}
.container {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
}
header {
width: 100%;
height: 10rem;
display: flex;
justify-content: center;
align-items: center;
}
.header-content {
width: 80%;
height: 100%;
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
font-size: 3rem;
user-select: none;
width: 5rem;
height: 5rem;
display: flex;
justify-content: center;
align-items: center;
border-radius: 50%;
transform: rotate(-25deg);
}
.drop-btn {
padding: 1rem;
font-size: 1rem;
background: none;
outline: none;
}
.dropdown {
position: relative;
display: inline-block;
}
.drop-content {
display: none;
position: absolute;
top: 3.3rem;
min-width: 160px;
z-index: 1;
}
.drop-content a {
padding: 12px 16px;
text-decoration: none;
display: block;
}
.drop-content a:hover {
text-decoration: underline;
}
.dropdown:hover .drop-content {
display: block;
}
main {
width: 85%;
height: 100%;
padding: 0 2rem 3rem;
box-sizing: border-box;
}
document.addEventListener('DOMContentLoaded', () => {
// ============== COLORS AND THEMES =============== //
const colors = {
white: '#FFFFFF',
lightGray: '#F5F3F4',
gray: '#D3D3D3',
warmGray: '#B1A7A6',
tomatoRed: '#E5383B',
lightRed: '#BA181B',
red: '#A4161A',
darkRed: '#660708',
darkGray: '#161A1D',
almostBlack: '#0B090A'
}
let isLight = true;
let isDark = false;
let isRed = false;
// =============== EXAMPLE ARTICLE INFO =============== //
let headline = 'Hello World!';
let subheadline = 'You can update your display preferences.';
let author = 'This Guy';
let publishDate = '6:45pm Wed March 17, 2021';
let copy1 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Aliquet risus feugiat in ante metus dictum at. Velit egestas dui id ornare arcu odio. Bibendum neque egestas congue quisque egestas. Gravida arcu ac tortor dignissim convallis aenean. Risus at ultrices mi tempus imperdiet nulla malesuada. Amet consectetur adipiscing elit duis. Quam pellentesque nec nam aliquam sem et. Pulvinar pellentesque habitant morbi tristique senectus et netus. Aliquam purus sit amet luctus venenatis. Malesuada pellentesque elit eget gravida cum sociis.';
let copy2 = 'Mattis rhoncus urna neque viverra justo nec ultrices dui. At consectetur lorem donec massa sapien faucibus et molestie ac. Vitae purus faucibus ornare suspendisse sed nisi lacus sed viverra. Lectus magna fringilla urna porttitor. Tristique nulla aliquet enim tortor. Massa massa ultricies mi quis hendrerit dolor magna eget est. Commodo nulla facilisi nullam vehicula. Scelerisque viverra mauris in aliquam sem fringilla ut morbi. Tortor vitae purus faucibus ornare suspendisse sed nisi lacus. Ut porttitor leo a diam sollicitudin tempor id eu. Nulla facilisi nullam vehicula ipsum a arcu cursus vitae. Nulla malesuada pellentesque elit eget gravida cum sociis natoque penatibus. Curabitur gravida arcu ac tortor dignissim convallis. Scelerisque eu ultrices vitae auctor eu augue ut. Nunc mi ipsum faucibus vitae. Vivamus arcu felis bibendum ut. Ultrices vitae auctor eu augue ut lectus arcu.';
// =============== ELEMENTS =============== //
const body = document.body;
const container = document.createElement('div');
container.className = 'container';
const header = document.createElement('header');
const headerContent = document.createElement('div');
headerContent.className = 'header-content';
const logo = document.createElement('div');
logo.className = 'logo';
logo.textContent = '📚';
logo.style.color = colors.tomatoRed;
logo.style.border = `0.2rem solid ${colors.tomatoRed}`;
const dropdown = document.createElement('div');
dropdown.className = 'dropdown';
const dropBtn = document.createElement('button');
dropBtn.className = 'drop-btn';
dropBtn.textContent = 'Preferences';
const dropContent = document.createElement('div');
dropContent.className = 'drop-content';
const light = document.createElement('a');
light.textContent = 'Light Mode';
light.href = '#';
light.addEventListener('click', () => {
isLight = true;
[isDark, isRed] = [false, false];
renderTheme();
});
const dark = document.createElement('a');
dark.textContent = 'Dark Mode';
dark.href = '#';
dark.addEventListener('click', () => {
isDark = true;
[isLight, isRed] = [false, false];
renderTheme();
});
const red = document.createElement('a');
red.textContent = 'Red Mode';
red.href = '#';
red.addEventListener('click', () => {
isRed = true;
[isLight, isDark] = [false, false];
renderTheme();
});
const main = document.createElement('main');
const h1 = document.createElement('h1');
h1.textContent = headline;
h1.style.marginTop = '3rem';
const h3 = document.createElement('h3');
h3.textContent = subheadline;
const details = document.createElement('p');
details.textContent = `${publishDate} | Written by ${author}`;
details.style.fontSize = '0.9rem';
const p1 = document.createElement('p');
p1.textContent = copy1;
p1.style.lineHeight = '1.8rem';
const p2 = document.createElement('p');
p2.textContent = copy2;
p2.style.lineHeight = '1.8rem';
// =============== Functions =============== //
function applyLight() {
body.style.backgroundColor = colors.lightGray;
header.style.backgroundColor = colors.lightGray;
dropBtn.style.color = colors.darkGray;
dropBtn.style.border = `0.1rem solid ${colors.darkGray}`;
dropContent.style.backgroundColor = colors.lightGray;
dropContent.style.border = `0.1rem solid ${colors.darkGray}`;
light.style.color = colors.almostBlack;
dark.style.color = colors.almostBlack;
red.style.color = colors.almostBlack;
main.style.backgroundColor = colors.white;
h1.style.color = colors.darkGray;
h3.style.color = colors.darkGray;
details.style.color = colors.warmGray;
p1.style.color = colors.darkGray;
p2.style.color = colors.darkGray;
}
function applyDark() {
body.style.backgroundColor = colors.almostBlack;
header.style.backgroundColor = colors.almostBlack;
dropBtn.style.color = colors.lightGray;
dropBtn.style.border = `0.1rem solid ${colors.lightGray}`;
dropContent.style.backgroundColor = colors.almostBlack;
dropContent.style.border = `0.1rem solid ${colors.lightGray}`;
light.style.color = colors.white;
dark.style.color = colors.white;
red.style.color = colors.white;
main.style.backgroundColor = colors.darkGray;
h1.style.color = colors.gray;
h3.style.color = colors.gray;
details.style.color = colors.warmGray;
p1.style.color = colors.gray;
p2.style.color = colors.gray;
}
function applyRed() {
body.style.backgroundColor = colors.red;
header.style.backgroundColor = colors.red;
dropBtn.style.color = colors.tomatoRed;
dropBtn.style.border = `0.1rem solid ${colors.tomatoRed}`;
dropContent.style.backgroundColor = colors.red;
dropContent.style.border = `0.1rem solid ${colors.tomatoRed}`;
light.style.color = colors.tomatoRed;
dark.style.color = colors.tomatoRed;
red.style.color = colors.tomatoRed;
main.style.backgroundColor = colors.darkRed;
h1.style.color = colors.tomatoRed;
h3.style.color = colors.tomatoRed;
details.style.color = colors.lightRed;
p1.style.color = colors.tomatoRed;
p2.style.color = colors.tomatoRed;
}
function renderTheme() {
if (isLight === true) {
light.textContent = 'Light Mode ✓';
applyLight();
} else {
light.textContent = 'Light Mode';
}
if (isDark === true) {
dark.textContent = 'Dark Mode ✓';
applyDark();
} else {
dark.textContent = 'Dark Mode';
}
if (isRed === true) {
red.textContent = 'Red Mode ✓';
applyRed();
} else {
red.textContent = 'Red Mode';
}
}
function addContent() {
body.appendChild(container);
container.appendChild(header);
container.appendChild(main);
header.appendChild(headerContent);
headerContent.appendChild(logo);
headerContent.appendChild(dropdown);
dropdown.appendChild(dropBtn);
dropdown.appendChild(dropContent);
dropContent.appendChild(light);
dropContent.appendChild(dark);
dropContent.appendChild(red);
main.appendChild(h1);
main.appendChild(h3);
main.appendChild(details);
main.appendChild(p1);
main.appendChild(p2);
}
addContent();
renderTheme();
});
Also see: Tab Triggers