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>The Box Model</h1>
<p>The box model refers to how an element is spaced on the page. How much room is around it? Or around its content? This is what the box model is about.</p>
<p>There are a few different components in the box model, which are shown below. Interact with them to learn more about each one!</p>
<p>To fully interact with the site, you'll need to use Chrome (or Firefox if you enable the dialog element).</p>
<div class="container">
<div class="margin interactable-container">
<div class="border interactable-container">
<div class="padding interactable-container">
<div class="content interactable-container">
--- Content ---
</div>
</div>
</div>
</div>
</div>
@import url('https://fonts.googleapis.com/css?family=Lato');
$bg-margin: #e0892c;
$bg-border: #e0b92b;
$bg-padding: #9dd675;
$bg-content: #74bcd6;
@mixin centerElement() {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
}
* {
position: relative;
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Lato', sans-serif;
}
h1,
p {
margin: auto;
padding: 0 20px;
max-width: 600px;
width: 100%;
text-align: center;
}
p {
margin-top: 40px;
}
pre {
padding: 10px 0 10px 50px;
text-align: left;
}
.container {
margin-top: 50px;
display: flex;
}
.margin,
.border,
.padding,
.content {
margin: auto;
padding: 40px;
cursor: pointer;
transition: background-color .3s;
}
.margin {
background-color: $bg-margin;
&.hovered {
background-color: darken($bg-margin, 10%);
}
&::before {
content: '--- margin ---';
@include centerElement;
}
}
.border {
background-color: $bg-border;
&.hovered {
background-color: darken($bg-border, 10%);
}
&::before {
content: '--- border ---';
@include centerElement;
}
}
.padding {
background-color: $bg-padding;
&.hovered {
background-color: darken($bg-padding, 10%);
}
&::before {
content: '--- padding ---';
@include centerElement;
}
}
.content {
width: 40vw;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
background-color: $bg-content;
&.hovered {
background-color: darken($bg-content, 10%);
}
}
.dialog-content p {
margin-top: 15px;
text-align: left;
}
const marginContainer = document.querySelectorAll('.margin');
const borderContainer = document.querySelectorAll('.border');
const paddingContainer = document.querySelectorAll('.padding');
const contentContainer = document.querySelectorAll('.content');
const interactableContainers = document.querySelectorAll('.interactable-container');
// for all the interactable containers, add event listeners to:
// reset all interactive containers to not have a hover state:
// for the imediate hovered element, add a hover state.
interactableContainers.forEach((container, index) => {
container.addEventListener('mouseover', (event) => {
resetClasses();
addHoverClass(event, container);
});
});
// Listener for removing the hover class on the outermost container
marginContainer[0].addEventListener('mouseout', (event) => {
resetClasses();
});
// Remove hover class from all containers
function resetClasses() {
interactableContainers.forEach((container, index) => {
container.classList.remove('hovered');
});
}
// Add the hover class to all containers
function addHoverClass(event, container) {
event.stopPropagation();
container.classList.add('hovered');
}
/**
* Dialogr events
*/
dialogr(marginContainer, {
content: `<h2>Margin</h2>
<p>This is the space around the element, and outside of any defined borders. It can be set with any of the following properties:</p>
<pre>margin: "value";
margin-top: "value";
margin-right: "value";
margin-bottom: "value";
margin-left: "value";</pre>
<p>Here are the values that can be used with margins:</p>
<pre>'auto': let the browser take care of it!
'inherit': get a value from the parent element.
'number': this would be a number in px, inches, a percent, etc.
</pre>
<p>Couple notes. First, negative values work! Second, if you set the width of a container, the margin isn't included in that, so be careful.</p>
<p><a href="https://www.w3schools.com/css/css_margin.asp" target="_blank">Read More</a></p>`
});
dialogr(borderContainer, {
content: `<h2>Border</h2>
<p>Set a border around an element, with a specific color, size, and type. It can be set either for the entirety of the element, OR for an individual side. Here's how you can set the border (or just an individual aspect of the border:</p>
<pre>
border: 'size' 'type' 'color';
border-width: 'size';
border-style: 'type';
border-color: 'color';
</pre>
<p>It's also possible to set a border radius to give your elements rounded corners! This is done with the 'border-radius' property.</p>
<p><a href="https://www.w3schools.com/css/css_border.asp" target="_blank">Read More</a></p>`
});
dialogr(paddingContainer, {
content: `<h2>Padding</h2>
<p>This is spacing in between the element's border and its content. The values it accepts are the same as the margin property, accept for 'auto'. That doesn't really work with padding. Here's how to set padding:</p>
<pre>padding: "value";
padding-top: "value";
padding-right: "value";
padding-bottom: "value";
padding-left: "value";</pre>
<p>Quick tip: depending on how your element's <a href="https://www.w3schools.com/cssref/css3_pr_box-sizing.asp" target="_blank">box sizing</a> is set, your padding may or may not be included in the element's width. Also, an elements background extends over it's padding.</p>
<p><a href="https://www.w3schools.com/css/css_padding.asp" target="_blank">Read More</a></p>`
});
dialogr(contentContainer, {
content: `<h2>Content</h2><p>This is your actual content: any text or child elements. When you set width & height for an element, this is what your setting it for (unless you took a look at that 'box-sizing' property).</p>`
});
Also see: Tab Triggers