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.
article
h1 Accessible Medium Style Dividers
p
| After I saw Rafal Chmiel's
a(href="https://codepen.io/rafalchmiel/details/Cjacq/")
| pen with Medium style dividers
| on the CodePen frontpage I thought to myself <q>This should be possible with simpler markup that is accessible</q>.
hr
h2 Semantic HTML
p Rafal states that the code for his pen is taken directly from Medium; so we shall not fault him for any errors in the markup. Medium uses a simple <code>div</code> with a <code>class</code> of <code>divider</code>. This works very well for the visual effect they are after. A sighted user will clearly see that this is the start of a new section.
p However, this information is not communicated to assistive technologies (AT) because it has no meaning; classes don't convey any information other than <q>Hey, you can select me with CSS!</q>
p
| Further more, they use an anchor to markup what clearly should be a header. AT users tend to
a(href="http://webaim.org/projects/screenreadersurvey4/#finding") scan a page by headings
| ; not anchors.
hr
h2 Effortless Style
p
| Semantic HTML means that we don't need to depend on meaningless classes to style our content.
a(href="https://twitter.com/heydonworks") Heydon Pickering
| coined the term Effortless Style in 2014
a(href="https://vimeo.com/101718785") at CSS Day
| . In essense it is the separation of concerns; a best practice according to the W3C.
p
| You could think of it as smart CSS. In this pen we know the title of our sections are right after an <code>hr</code>—or horizontal rule. With this knowledge we can utilize the
a(href="http://www.w3.org/TR/css3-selectors/#adjacent-sibling-combinators") adjacent sibling combinator
| to select our heading: <code>hr + h2</code>.
hr
h2 Alternative technique
p
| After a comment from
a(href="https://codepen.io/ZCKVNS") ZCKVNS
| , I decided to try
a(href="https://codepen.io/Moiety/details/yyyOep/") an alternative technique
| . The alternative uses a span to center text, instead of a <code>transform: translateX()</code>.
p If you really want to keep markup to a minimum, there is an alternative border method included in this—the one you're reading—pen. You can find it in the CSS, near the end.
// Some vars
$link-color: #f52e62;
$text-color: #3f517e;
$hr-color: rgba(0,0,0,0.35);
$hr-text-color: #453986;
$letter-spacing: .32em;
$background-color: #fff;
hr {
display: block;
margin: 50px 0 -15px;
width: 100%;
height: 1px;
border: 0;
background-color: $hr-color;
+ h2 {
display: inline-block;
position: relative;
left: 50%;
margin: 0;
padding: 5px 10px;
border: 1px solid $hr-text-color;
//box-shadow: inset 0 0 0 1px $hr-text-color;
transform: translateX(-50%);
color: $hr-text-color;
font-size: 12px;
font-weight: 500;
letter-spacing: $letter-spacing;
text-align: center;
text-transform: uppercase;
background-color: $background-color;
// Cancel out offset created by letterspacing
&::first-letter {
margin-left: $letter-spacing;
}
}
}
/* Alternative transform: translate */
hr + h2 {
border-width: 1px 0;
&::before,
&::after {
display: block;
position: absolute;
top: 0;
bottom: 0;
width: 1px;
background: $hr-text-color;
content: '';
}
&::before {
left: 0;
}
&::after {
right: 0;
}
}
/**/
// Unimportant bits
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
color: $text-color;
font-family: Helvetica Neue, Helvetica, Arial sans-serif;
background-color: $background-color;
}
article {
margin: 20px 10px;
@media (min-width: 30em) {
margin: 40px 0;
padding: 0 10px;
width: 30em;
}
}
a {
color: $link-color;
text-decoration: none;
&:focus,
&:hover {
text-decoration: underline;
}
}
code {
font-size: 1.1em;
}
h1 {
margin: 0 0 1em;
color: #44388b;
}
p {
padding: 0 10px;
// This is just to get rid of the needles p's CodePen drops in.
&:empty {
display: none;
}
}
Also see: Tab Triggers