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.
<section search="container">
<h1>Search and highlight</h1>
<form class="input__container">
<input type="text" search="input" placeholder="Search Miguel or Charles">
<button type="reset" title="Clear input" search="clear" hidden>
<svg viewBox="0 0 20 20">
<path
d="M11.4142136,10 L14.2426407,7.17157288 L12.8284271,5.75735931 L10,8.58578644 L7.17157288,5.75735931 L5.75735931,7.17157288 L8.58578644,10 L5.75735931,12.8284271 L7.17157288,14.2426407 L10,11.4142136 L12.8284271,14.2426407 L14.2426407,12.8284271 L11.4142136,10 L11.4142136,10 Z M2.92893219,17.0710678 C6.83417511,20.9763107 13.1658249,20.9763107 17.0710678,17.0710678 C20.9763107,13.1658249 20.9763107,6.83417511 17.0710678,2.92893219 C13.1658249,-0.976310729 6.83417511,-0.976310729 2.92893219,2.92893219 C-0.976310729,6.83417511 -0.976310729,13.1658249 2.92893219,17.0710678 L2.92893219,17.0710678 Z M4.34314575,15.6568542 C7.46734008,18.7810486 12.5326599,18.7810486 15.6568542,15.6568542 C18.7810486,12.5326599 18.7810486,7.46734008 15.6568542,4.34314575 C12.5326599,1.21895142 7.46734008,1.21895142 4.34314575,4.34314575 C1.21895142,7.46734008 1.21895142,12.5326599 4.34314575,15.6568542 L4.34314575,15.6568542 Z"
stroke="none" stroke-width="1" fill="#38383a" fill-rule="evenodd"></path>
</svg>
</button>
<div search="counter"></div>
</form>
<details search="item">
<summary>Don Quixote</summary>
The Ingenious Gentleman Don Quixote of La Mancha, or just Don Quixote, is a Spanish novel by Miguel de Cervantes.
</details>
<details search="item">
<summary>A Tale of Two Cities</summary>
A Tale of Two Cities is an 1859 historical novel by Charles Dickens, set in London and Paris before and during the French Revolution.
</details>
<div search="noResults" hidden>No results</div>
</section>
$accent: #dfa041
$darkGray: #38383a
$lightGray: #ebeef3
$white: #f8f8f8
$boxShadow: 0px 16px 64px -24px rgba(0,0,0,0.45)
$borderRadius: 32px
$animation: .4s cubic-bezier(.5, 0, .2, 1)
html
font-size: 18px
background-color: $lightGray
box-sizing: border-box
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif
color: $darkGray
body
display: flex
padding: 0 1rem
html,
body
height: 100%
margin: 0
section
width: 375px
margin: auto
padding: 2rem
background: white
border-radius: $borderRadius
box-shadow: $boxShadow
.input__container
position: relative
input
font-size: 16px
width: 100%
padding: .75rem
border: 2px solid $lightGray
border-radius: 8px
box-sizing: border-box
::placeholder
opacity: .5
details
margin-top: 2rem
padding-left: 1.35rem
&[open]
animation: opacity $animation
summary
margin-left: -1.1rem
padding-bottom: .5rem
font-weight: 600
[search="clear"]
position: absolute
right: .75rem
top: .75rem
background: none
border: none
padding: 0
svg
height: 24px
width: 24px
pointer-events: none
animation: opacity $animation
[search="counter"]
position: absolute
bottom: -1rem
font-size: .75rem
opacity: .5
[search="noResults"]
margin: 2rem 0 0
animation: opacity $animation
@keyframes opacity
0%
opacity: 0
100%
opacity: 1
(function () {
'use strict';
var controls = {
input: document.querySelector('[search="input"]'),
items: document.querySelectorAll('[search="item"]'),
noResults: document.querySelector('[search="noResults"]'),
clear: document.querySelector('[search="clear"]'),
counter: document.querySelector('[search="counter"]'),
indexedItems: [],
hasControls: function() {
return this.input != undefined && this.items != undefined
}
}
// checks for required search components
if (!controls.hasControls()) return;
// shows/hides no results message
function toggleNoResultsMessage(searchTerm) {
// checks if any items are open
var hasResults = Array.prototype.filter.call(controls.items, function (item) {
return item.hasAttribute('open');
})
if (hasResults.length && searchTerm.length > 1) {
// hide no results message if items are open
controls.noResults.setAttribute('hidden', '');
} else if (searchTerm.length > 1) {
// show no results message
controls.noResults.removeAttribute('hidden');
Array.prototype.forEach.call(controls.items, function (item) {
item.setAttribute('hidden', '')
})
return;
} else {
// hide no results message
controls.noResults.setAttribute('hidden', '');
}
}
// searches and highlights
function searchAndHighlight() {
Array.prototype.forEach.call(controls.items, function (item) {
item.innerHTML = item.innerHTML.replace(/<mark>([^<]+)<\/mark>/gi, '$1');
});
var searchTerm = event.target.value.trim().toLowerCase();
if (searchTerm.length > 1) {
controls.indexedItems.forEach(function (item, i) {
if (controls.indexedItems[i].indexOf(searchTerm) != -1) {
controls.items[i].setAttribute('open', true);
controls.items[i].removeAttribute('hidden'); // removes hidden attribute when deleteing
controls.items[i].innerHTML = controls.items[i].innerHTML.replace(new RegExp(searchTerm + '(?!([^<]+)?>)', 'gi'), '<mark>$&</mark>');
} else {
controls.items[i].removeAttribute('open');
controls.items[i].setAttribute('hidden', '');
}
});
controls.clear.removeAttribute('hidden');
} else {
Array.prototype.forEach.call(controls.items, function (item) {
item.removeAttribute('open');
item.removeAttribute('hidden');
});
controls.clear.setAttribute('hidden', '');
}
countHighlights();
toggleNoResultsMessage(searchTerm);
}
// counts number of matches
function countHighlights() {
var count = document.querySelectorAll('mark').length;
if (count) {
return controls.counter.innerHTML = count + ' results';
}
return controls.counter.innerHTML = '';
}
// sanitize search result matches
Array.prototype.forEach.call(controls.items, function (item) {
controls.indexedItems.push(item.textContent.replace(/\s{2,}/g, ' ').toLowerCase());
});
controls.input.addEventListener('keydown', function(event) {
// prevent submit on enter
if (event.keyCode === 13)
{
event.preventDefault();
return false;
}
})
// input keyup
controls.input.addEventListener('keyup', function(event) {
searchAndHighlight();
});
// clear button click
controls.clear.addEventListener('click', function() {
event.target.setAttribute('hidden', '');
toggleNoResultsMessage('');
searchAndHighlight();
controls.input.focus();
})
})();
Also see: Tab Triggers