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.
<body>
<div class="banner">
<p>This site uses cookies to improve the user experience and to collect data. You can <button type="button" id="configure-button">configure</button> which cookies you want to give your consent to.</p>
<button type="button" id="accept-button" class="button">Accept all</button>
</div>
<div hidden class="modal">
<div class="modal-content" role="dialog">
<div class="modal-header">
<h2>Configure cookies</h2>
<button id="close-button" type="button">Close</button>
</div>
<div class="modal-body">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Exercitationem atque dolorem, ipsam temporibus veniam, rem, rerum tempora doloribus quasi earum recusandae quas praesentium optio quod sapiente consequatur labore incidunt voluptatem?</p>
<h3>User experience</h3>
<div class="modal-cookie">
<input checked type="checkbox" id="appcues" />
<label for="appcues">Appcues</label>
</div>
<div class="modal-cookie">
<input checked type="checkbox" id="ab-tasty" />
<label for="ab-tasty">AB Tasty</label>
</div>
<h3>Tracking</h3>
<div class="modal-cookie">
<input checked type="checkbox" id="google-analytics" />
<label for="google-analytics">Google Analytics</label>
</div>
</div>
<button id="confirm-button" class="modal-confirm button" type="button">Confirm</button>
</div>
</div>
<header>
<a href="#main">Skip to main content</a>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">Blog</a></li>
</ul>
</nav>
</header>
<main id="main">
<h1>Accessible cookie banner 🍪</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sed quam ac nulla finibus tincidunt. Sed id erat rhoncus, sodales risus a, ultricies dui. Praesent id diam malesuada, aliquet felis et, volutpat magna. Sed ut risus aliquam, mattis purus vitae, consequat nisl. Duis interdum consequat orci id finibus. Fusce venenatis elit ante, vel posuere purus suscipit nec. <a href="">Duis sit amet risus</a> eu odio feugiat venenatis. Aenean lobortis nisi nec nulla cursus, vitae elementum nulla euismod. Suspendisse potenti. Donec ex diam, placerat eu odio vel, ornare varius nisi. Duis tempus fermentum nunc, eget condimentum dui molestie sed. Aliquam erat volutpat. Fusce leo arcu, consequat vel odio ac, varius porttitor augue. Nam maximus enim semper lorem consequat, ut maximus ante efficitur. Ut aliquam mi vitae quam luctus, vel elementum dui hendrerit. Etiam laoreet enim commodo, laoreet felis at, gravida ex.</p>
<p>Suspendisse potenti. Nam aliquet convallis volutpat. Aliquam elementum leo odio, eu porta est porttitor et. Vivamus <a href="">egestas molestie diam</a>, eu ultrices eros molestie quis. Nullam id eleifend tortor, a tristique augue. Donec pretium felis a suscipit accumsan. Nulla a turpis sit amet elit interdum scelerisque. Vestibulum et risus elementum, facilisis lacus non, dictum felis. Aliquam interdum finibus nisl interdum convallis. Etiam quis porta justo. Maecenas egestas iaculis ipsum quis finibus. Duis quis ullamcorper mauris, non bibendum diam. Nunc aliquet ipsum ac dictum efficitur. Nulla non enim nec metus finibus placerat. Fusce at tempor justo.</p>
<p>Sed lobortis ante eu mauris cursus tristique. Praesent condimentum dui vel nisi consectetur, sed ullamcorper erat elementum. Nulla tincidunt tellus vel mauris ornare, eu hendrerit justo viverra. Suspendisse justo dui, mollis eu lorem eu, suscipit fringilla quam. Nunc fringilla tortor vitae dignissim convallis. Nam cursus libero id vehicula eleifend. Fusce porta erat nec dolor sollicitudin, et interdum lorem imperdiet. Sed vestibulum vitae velit fermentum sagittis. Fusce quis tincidunt risus. Cras efficitur dolor at massa pulvinar faucibus. Maecenas porttitor pulvinar risus quis vestibulum. Proin dapibus efficitur augue nec finibus. Vestibulum id placerat dui. Nam a hendrerit ipsum, non gravida ante. Suspendisse potenti.</p>
<p>Donec at sagittis libero, nec malesuada leo. Mauris dapibus purus in tellus hendrerit accumsan. Nulla suscipit, risus vel faucibus condimentum, sapien libero tincidunt ipsum, ac varius nisl diam id lectus. Donec eget pretium mauris. Duis finibus tortor at massa imperdiet feugiat. Nulla sit amet nisi vel lorem sodales porta non vitae mauris. Aliquam pellentesque lacinia est. Praesent nisl ligula, cursus a turpis in, laoreet consequat lectus. Nam id mi tempus, consequat magna quis, ultrices eros. Pellentesque hendrerit pretium fringilla.</p>
</main>
</body>
:root {
--color-main: #008694;
--color-black: black;
}
/* Banner */
.banner {
background: white;
box-shadow: 0 0 5px 1px rgba(black, 0.3);
padding: 1rem;
position: fixed;
bottom: 0;
left: 0;
right: 0;
p {
margin-bottom: 1rem;
& > button {
background: none;
border: none;
font: inherit;
padding: 0;
text-decoration: underline;
&:hover,
&:focus {
text-decoration: none;
}
}
}
}
/* Modal */
.modal {
background: rgba(black, 0.3);
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.modal-header {
align-items: center;
display: flex;
justify-content: space-between;
margin-bottom: 1rem;
h2 {
color: var(--color-main);
font-size: 2rem;
font-weight: 700;
}
button {
background: none;
border: none;
font: inherit;
}
}
.modal-body {
margin-bottom: 3rem;
h3 {
color: var(--color-main);
font-size: 1.2rem;
font-weight: 600;
margin: 0.5rem 0;
}
.modal-cookie {
& + .modal-cookie {
margin-top: 0.5rem;
}
}
}
.modal-confirm {
margin-top: auto;
}
.modal-content {
background: white;
overflow: auto;
padding: 1rem;
display: flex;
flex-direction: column;
position: fixed;
top: 1rem;
left: 1rem;
right: 1rem;
bottom: 1rem;
}
/* Button */
.button {
background: white;
border: 1px solid var(--color-main);
font: inherit;
padding: 0.5rem 1rem;
&:hover,
&:focus {
background: var(--color-main);
color: white;
}
}
/* Main layout */
body {
font-family: sans-serif;
line-height: 1.5;
margin: 0;
padding: 0;
}
*:focus {
outline: 2px dashed var(--color-black);
outline-offset: 0.25rem;
}
header {
background: var(--color-main);
padding: 1rem 2rem;
[href="#main"] {
background: white;
color: var(--color-main);
padding: 0.5rem;
position: absolute;
top: -100%;
left: 1rem;
&:focus {
text-decoration: none;
top: 1rem;
}
}
ul {
align-items: center;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
}
li {
&:first-child {
margin-right: auto;
}
& + li {
margin-left: 1rem;
}
a {
color: white;
&:hover,
&:focus {
text-decoration: none;
}
}
}
}
main {
margin: 0 auto;
max-width: 45rem;
padding: 4rem 1rem;
h1 {
color: var(--color-main);
font-size: 3rem;
font-weight: 700;
line-height: 1.2;
margin-bottom: 2rem;
}
p {
font-size: 1.2rem;
& + p {
margin-top: 1rem;
}
}
a {
color: var(--color-main);
&:hover,
&:focus {
text-decoration: none;
}
}
}
const acceptButton = document.querySelector('#accept-button')
const configureButton = document.querySelector('#configure-button')
const closeButton = document.querySelector('#close-button')
const confirmButton = document.querySelector('#confirm-button')
const banner = document.querySelector('.banner')
const modal = document.querySelector('.modal')
const skipLink = document.querySelector('[href="#main"]')
let focusables = []
/* Hide banner if user accepts cookies */
acceptButton.addEventListener('click', () => {
banner.hidden = true
})
/* Open modal if user wants to configure cookies */
configureButton.addEventListener('click', () => {
modal.hidden = false
closeButton.focus()
document.body.style.overflow = 'hidden'
focusables = Array.from(modal.querySelectorAll('button, a, input, textarea, select'))
document.addEventListener('keydown', handleModalKeyboard)
})
/* Close modal on close */
closeButton.addEventListener('click', () => {
modal.hidden = true
document.body.style.overflow = 'initial'
document.removeEventListener('keydown', handleModalKeyboard)
configureButton.focus()
})
/* Close modal on confirm */
confirmButton.addEventListener('click', () => {
banner.hidden = true
modal.hidden = true
document.body.style.overflow = 'initial'
document.removeEventListener('keydown', handleModalKeyboard)
skipLink.focus()
})
const handleModalKeyboard = e => {
if (e.key === 'Escape') {
modal.hidden = true
configureButton.focus()
}
if (e.key === 'Tab') {
e.preventDefault()
let index = focusables.findIndex(f => f === modal.querySelector(':focus'))
console.log(index)
if (e.shiftKey === true) {
index--
} else {
index++
}
if (index >= focusables.length) {
index = 0
}
if (index < 0) {
index = focusables.length - 1
}
focusables[index].focus()
}
}
Also see: Tab Triggers