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.
<pre></pre><h1>Component size vs Visual size</h1>
<p>Some example buttons for focus appearance comparing what happens when you measure the underlying component vs visually.</p>
<p class="alert">OLD, do not reference!</p>
<p><button class="example1">Button 1</button> - default, passes both</p>
<p><button class="example2">Button 2</button> - drop shadow extends visual size, but not component size. Passes 'component'; fails 'visual'.</p>
<p><button class="example2b">Button 2b</button> - drop shadow used as focus indicator</p>
<p><button class="example2b">Button 2b</button> - drop shadow used as focus indicator</p>
<p><button class="example2b">Button 2b</button> - drop shadow used as focus indicator</p>
<p><button class="example3">Button 3</button> - outline offset (-3px) leads to fail measured both by component and visuals.</p>
<p><button class="example3 alt3">Button 3</button> - outline offset (+3px) leads to passes both by component and visuals.</p>
<p><span class="padding"><button class="example4">Button 4</button></span> - button wrapped in a span, the span is providing the background. Passes 'component'; fails 'visual'.</p>
<p><button class="example5"><span>Button 5</span></button> - background provided within the component, expanded hit area. Passes both.</p>
<p><button class="example5b"><span>Button 5b</span></button> - background provided within the component, and the outline goes around the inner span. (Grey dashed border shows the button size.) Passes visual, fails component.</p>
<p><button class="example6">Button 6</button> - large border radius.</p>
<p><button class="example7">Button 7</button> - small border radius.</p>
<p>Example grabbed from <a href="https://codepen.io/aardrian/pen/vYOGpXb">Adrian's block links article</a>. The link is the heading (which looks like a link), but the focus indicator goes around the whole card. Visually, what is the component? (Without a self-referencial "whatever the outline goes around!")</p>
<div>
<article class="block reorder">
<h3><a href="https://example.com/re-ordered" rel="nofollow">Art Visually Above Heading</a></h3>
<img src="https://placekitten.com/680/380" width="680" height="380" alt="Kitten resting against a scratching post.">
<p>
This looks perfect. Just Photoshop out the dog, add a baby, and make the curtains blue. There are more projects
lined up; charge extra the next time. Can you make the logo bigger? Yes, bigger. Bigger still. The logo is too
big.
</p>
</article>
<article class="blockalt reorder">
<h3><a href="https://example.com/re-ordered" rel="nofollow">Art Visually Above Heading</a></h3>
<img src="https://placekitten.com/680/380" width="680" height="380" alt="Kitten resting against a scratching post.">
<p>
This looks perfect. Just Photoshop out the dog, add a baby, and make the curtains blue. There are more projects
lined up; charge extra the next time. Can you make the logo bigger? Yes, bigger. Bigger still. The <a href="#">logo</a> is too
big.
</p>
</article>
</div>
<p>From a visual point of view, I find it impossible to evaluate these two 'card' examples without knowing what the interactive element is. The HTML for each is the same, but the first one uses CSS to increase the hit area and the second does not.</p>
<h2>Conclusion</h2>
<p>Most examples (in usage, not here) will match the component size and visuals. However, using the underlying component allows for the focus indicator to go around the text content, not the whole visual component (although that wouldn't be a problem to do so).</p>
<p>Also, the default focus indicator (outline) aligns with the component metric, therefore it is easier to test. You could outline everything with CSS, and then tab through to check for differences. e.g.</p>
<pre>a:not(:focus), button:not(:focus), input:not(:focus) {
outline: 2px red solid !important;
}</pre>
<img class="screenshot" src="https://alastairc.uk/tmp/highlight-outlines.png" alt="Screenshot of this page, with red outlines of every interactive component.">
body {
font-family: sans-serif;
max-width: 50rem;
margin: 0 auto;
}
.alert {
font-size: 2rem;
color: #900;
font-weight: bold;
}
button {
border-radius: 0;
border-style: none;
padding: 4px;
background: #DFDFDF;
cursor: hand;
}
.example2 {
box-shadow: 5px 5px 5px #bbb, -5px -5px 5px #bbb, -5px 5px 5px #bbb, 5px -5px 5px #bbb;
margin-bottom: 5px;
margin-right: 5px;
}
.example2:focus {
box-shadow: none;
}
.example2b {
margin-bottom: 5px;
margin-right: 5px;
}
.example2b:focus {
box-shadow: 5px 5px 5px #bbb, -5px -5px 5px #bbb, -5px 5px 5px #bbb, 5px -5px 5px #bbb;
outline: transparent 2px solid;
}
.example3 {
padding: 8px;
}
.example3:focus {
outline-offset: -5px;
}
.example3.alt3:focus {
outline-offset: 5px;
}
.padding {
padding: 10px;
background: #DFDFDF;
display: inline-block;
}
.example4 {
padding: 0px;
background: none;
}
.example5 {
background: none;
}
.example5 span {
margin: 5px;
padding: 3px;
background: #DFDFDF;
display: inline-block;
}
.example5b {
background: none;
padding: 5px;
border: 1px grey dashed;
}
.example5b span {
background: #DFDFDF;
padding: 3px;
margin: 3px;
display: inline-block;
}
.example5b:focus {
outline: none;
}
.example5b:focus span {
outline: 1px blue solid;
}
.example6 {
padding: 0.5rem;
height: 5rem;
width: 5rem;
border-radius: 5rem;
display: inline-block;
}
.example7 {
padding: 0.5rem;
width: 5rem;
border-radius: 1rem;
display: inline-block;
}
/* Adrian's example */
div {
display: flex;
justify-content: center;
flex-wrap: wrap;
}
div > * {
padding: .5em 1em;
margin: 0 0.5em 1em 0.5em;
flex: 0 1 20em;
border: .2em solid rgba(0,0,0,.15);
border-radius: 1em;
background-color: #fff;
}
img {
max-width: 100%;
height: auto;
}
h3 {
margin: .2em 0;
line-height: 1.2;
}
div p {
margin: .2em 0;
}
div a:focus, div a:hover, div a {
}
.default a:link {
color: #333;
}
.default a:focus h3, .default a:hover h3 {
color: #00f;
text-decoration: underline;
}
/* Move the image above the heading */
.reorder {
display: flex;
flex-direction: column;
padding-top: .5em;
}
.reorder img {
/* see comment on post */
order: -1;
}
/* Block links */
.block {
position: relative;
}
.block a[href]::after, .block button::after {
content: "";
display: block;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.block:hover, .block:focus-within, .default:hover, .default:focus-within {
border-color: rgba(0,0,255,.5);
box-shadow: 0 0 .5em rgba(0,0,0,.25);
}
.blockalt a:focus {
outline: 3px solid blue;
}
/* Help out IE */
.block a[href]:hover, .block a[href]:focus {
outline: .4em solid rgba(0,0,255,.25);
outline-offset: .25em;
}
/* Undo the IE */
.block:hover a[href]:hover, .block:hover a[href]:focus, .block:hover button:focus, .block:hover button:hover, .block:focus-within a[href]:hover, .block:focus-within a[href]:focus, .block:focus-within button:focus, .block:focus-within button:hover {
outline: none;
}
.screenshot {
border: 2px grey solid;
}
Also see: Tab Triggers