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.
<form class="synth chord" id="app">
<h2>CHORD VISUALIZER</h2>
<fieldset>
<label><strong>Key:</strong><select id="key"><option value="A">A</option><option value="A#">A#</option><option value="B">B</option><option value="C">C</option><option value="C#">C#</option><option value="D">D</option><option value="D#">D#</option><option value="E">E</option><option value="F">F</option><option value="F#">F#</option><option value="G">G</option><option value="G#">G#</option></select></label>
<label><strong>Chord:</strong><select id="chord"><option selected="" disabled="">Select Chord</option><option value="Major triad">Major triad</option><option value="Minor triad">Minor triad</option><option value="Augmented triad">Augmented triad</option><option value="Diminished triad">Diminished triad</option><option value="Dominanth seventh">Dominanth seventh</option><option value="Major seventh">Major seventh</option><option value="Minor-major seventh">Minor-major seventh</option><option value="Minor seventh">Minor seventh</option><option value="Augmented-major seventh">Augmented-major seventh</option><option value="Augmented seventh">Augmented seventh</option><option value="Half-diminished seventh">Half-diminished seventh</option><option value="Diminished seventh">Diminished seventh</option><option value="Dominant seventh flat five">Dominant seventh flat five</option><option value="Major ninth">Major ninth</option><option value="Dominant ninth">Dominant ninth</option><option value="Dominant minor ninth">Dominant minor ninth</option><option value="Minor-major ninth">Minor-major ninth</option><option value="Minor ninth">Minor ninth</option><option value="Augmented major ninth">Augmented major ninth</option><option value="Augmented dominant ninth">Augmented dominant ninth</option><option value="Half-diminished ninth">Half-diminished ninth</option><option value="Half-diminished minor ninth">Half-diminished minor ninth</option><option value="Diminished ninth">Diminished ninth</option><option value="Diminished minor ninth">Diminished minor ninth</option></select></label>
</fieldset>
<div id="kb49" class="kb kb--49"><button aria-label="C3" data-freq="130.8127826502993" name="midi_48" "="" style="--gcs:1" type="button>"></button>
<button aria-label="C#3" data-freq="138.59131548843604" name="midi_49" "="" style="--gcs:3" type="button>"></button>
<button aria-label="D3" data-freq="146.8323839587038" name="midi_50" "="" style="--gcs:4" type="button>"></button>
<button aria-label="D#3" data-freq="155.56349186104043" name="midi_51" "="" style="--gcs:6" type="button>"></button>
<button aria-label="E3" data-freq="164.81377845643496" name="midi_52" "="" style="--gcs:7" type="button>"></button>
<button aria-label="F3" data-freq="174.61411571650194" name="midi_53" "="" style="--gcs:10" type="button>"></button>
<button aria-label="F#3" data-freq="184.99721135581723" name="midi_54" "="" style="--gcs:12" type="button>"></button>
<button aria-label="G3" data-freq="195.99771799087463" name="midi_55" "="" style="--gcs:13" type="button>"></button>
<button aria-label="G#3" data-freq="207.65234878997256" name="midi_56" "="" style="--gcs:15" type="button>"></button>
<button aria-label="A3" data-freq="220" name="midi_57" "="" style="--gcs:16" type="button>"></button>
<button aria-label="A#3" data-freq="233.08188075904496" name="midi_58" "="" style="--gcs:18" type="button>"></button>
<button aria-label="B3" data-freq="246.94165062806206" name="midi_59" "="" style="--gcs:19" type="button>"></button>
<button aria-label="C4" data-freq="261.6255653005986" name="midi_60" "="" style="--gcs:22" type="button>"></button>
<button aria-label="C#4" data-freq="277.1826309768721" name="midi_61" "="" style="--gcs:24" type="button>"></button>
<button aria-label="D4" data-freq="293.6647679174076" name="midi_62" "="" style="--gcs:25" type="button>"></button>
<button aria-label="D#4" data-freq="311.12698372208087" name="midi_63" "="" style="--gcs:27" type="button>"></button>
<button aria-label="E4" data-freq="329.6275569128699" name="midi_64" "="" style="--gcs:28" type="button>"></button>
<button aria-label="F4" data-freq="349.2282314330039" name="midi_65" "="" style="--gcs:31" type="button>"></button>
<button aria-label="F#4" data-freq="369.99442271163446" name="midi_66" "="" style="--gcs:33" type="button>"></button>
<button aria-label="G4" data-freq="391.99543598174927" name="midi_67" "="" style="--gcs:34" type="button>"></button>
<button aria-label="G#4" data-freq="415.3046975799451" name="midi_68" "="" style="--gcs:36" type="button>"></button>
<button aria-label="A4" data-freq="440" name="midi_69" "="" style="--gcs:37" type="button>"></button>
<button aria-label="A#4" data-freq="466.1637615180899" name="midi_70" "="" style="--gcs:39" type="button>"></button>
<button aria-label="B4" data-freq="493.8833012561241" name="midi_71" "="" style="--gcs:40" type="button>"></button>
<button aria-label="C5" data-freq="523.2511306011972" name="midi_72" "="" style="--gcs:43" type="button>"></button>
<button aria-label="C#5" data-freq="554.3652619537442" name="midi_73" "="" style="--gcs:45" type="button>"></button>
<button aria-label="D5" data-freq="587.3295358348151" name="midi_74" "="" style="--gcs:46" type="button>"></button>
<button aria-label="D#5" data-freq="622.2539674441618" name="midi_75" "="" style="--gcs:48" type="button>"></button>
<button aria-label="E5" data-freq="659.2551138257398" name="midi_76" "="" style="--gcs:49" type="button>"></button>
<button aria-label="F5" data-freq="698.4564628660078" name="midi_77" "="" style="--gcs:52" type="button>"></button>
<button aria-label="F#5" data-freq="739.9888454232689" name="midi_78" "="" style="--gcs:54" type="button>"></button>
<button aria-label="G5" data-freq="783.9908719634986" name="midi_79" "="" style="--gcs:55" type="button>"></button>
<button aria-label="G#5" data-freq="830.6093951598903" name="midi_80" "="" style="--gcs:57" type="button>"></button>
<button aria-label="A5" data-freq="880" name="midi_81" "="" style="--gcs:58" type="button>"></button>
<button aria-label="A#5" data-freq="932.3275230361799" name="midi_82" "="" style="--gcs:60" type="button>"></button>
<button aria-label="B5" data-freq="987.7666025122483" name="midi_83" "="" style="--gcs:61" type="button>"></button>
<button aria-label="C6" data-freq="1046.5022612023945" name="midi_84" "="" style="--gcs:64" type="button>"></button>
<button aria-label="C#6" data-freq="1108.7305239074883" name="midi_85" "="" style="--gcs:66" type="button>"></button>
<button aria-label="D6" data-freq="1174.6590716696303" name="midi_86" "="" style="--gcs:67" type="button>"></button>
<button aria-label="D#6" data-freq="1244.5079348883235" name="midi_87" "="" style="--gcs:69" type="button>"></button>
<button aria-label="E6" data-freq="1318.5102276514797" name="midi_88" "="" style="--gcs:70" type="button>"></button>
<button aria-label="F6" data-freq="1396.9129257320155" name="midi_89" "="" style="--gcs:73" type="button>"></button>
<button aria-label="F#6" data-freq="1479.9776908465378" name="midi_90" "="" style="--gcs:75" type="button>"></button>
<button aria-label="G6" data-freq="1567.981743926997" name="midi_91" "="" style="--gcs:76" type="button>"></button>
<button aria-label="G#6" data-freq="1661.2187903197805" name="midi_92" "="" style="--gcs:78" type="button>"></button>
<button aria-label="A6" data-freq="1760" name="midi_93" "="" style="--gcs:79" type="button>"></button>
<button aria-label="A#6" data-freq="1864.6550460723597" name="midi_94" "="" style="--gcs:81" type="button>"></button>
<button aria-label="B6" data-freq="1975.533205024496" name="midi_95" "="" style="--gcs:82" type="button>"></button>
<button aria-label="C7" data-freq="2093.004522404789" name="midi_96" "="" style="--gcs:85" type="button>"></button></div>
</form>
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;900&display=swap');
.kb {
block-size: var(--_bs, 8em);
display: grid;
grid-column-gap: 1px;
grid-template-columns: repeat(var(--_r, 156), 1fr);
grid-template-rows: repeat(5, 1fr);
}
.kb [aria-label] {
--_focus-bxsh: inset 0 0 50px #fff, inset 20px 0 80px hsl(var(--_h,0), 70%, 35%), inset -20px 0 80px hsl(var(--_h,0), 100%, 50%);
--_hover-bg: linear-gradient(to bottom, #FFF 0%, hsl(0, 0%, 90%) 100%);
background-color: var(--_bgc, #FFF);
border: 0;
border-radius: 0 0 3px 3px;
grid-column: var(--gcs) / span var(--_csp, 3);
grid-row: 1 / span var(--_rsp, 5);
position: relative;
}
.kb [aria-label*="#"] {
--_csp: 2;
--_focus-bxsh: inset 0 0 50px hsl(var(--_h,0), 25%, 35%), inset 20px 0 80px hsl(var(--_h,0), 70%, 35%), inset -20px 0 80px hsl(var(--_h,0), 100%, 50%);
--_hover-bg: linear-gradient(to top, #000 0%, hsl(0, 0%, 25%) 100%);
--_rsp: 3;
background-color: var(--_bgcs, #000);
z-index: 1;
}
.kb [aria-label]:not(.keydown):hover { background: var(--_hover-bg); }
.kb [aria-label]:not(.keydown):focus-visible { box-shadow: var(--_focus-bxsh); outline: none; }
.synth {
background: var(--synth-bgc, linear-gradient(215deg, hsl(0, 0%, 25%), hsl(0, 0%, 15%), hsl(0, 0%, 5%)));
border-radius: .33em .33em .75em .75em;
color: var(--synth-c, #FFF);
font-family: var(--synth-ff, 'Russo One', sans-serif);
inline-size: max-content;
padding: .5em 1em 1em 1em;
}
.synth h2 {
font-size: var(--synth-h-fz, 1.33em);
font-weight: var(--synth-h-fw, 700);
margin: 0 0 .5em 0;
}
/* modifiers */
.kb--61 { --_r: 108; }
.kb--49 { --_r: 87; }
.kb--32 { --_bs: 6em; --_r: 57;}
.kb--25 { --_bs: 6em; --_r: 45; }
.keydown {
--_l: calc((var(--v, 1) * 0.394%));
--_bgc: hsla(var(--_h), 50%, calc(95% - var(--_l)));
--_bgcs: var(--_bgc);
}
.keydown::after {
background-color: #000;
block-size: .5em;
border-radius: 50%;;
content: "";
display: block;
inline-size: .5em;
inset-block-end: 1em;
inset-inline-start: 50%;
position: absolute;
transform: translateX(-50%);
}
.chord {
--synth-bgc: hsl(351, 68%, 30%);
--synth-ff: 'Orbitron', sans-serif;
--synth-h-fz: 1em;
--synth-h-fw: 900;
--_h: 351;
--v: 70;
}
.chord fieldset {
border: 0;
display: flex;
gap: 1em;
padding: 0 0 1em 0;
}
.chord label {
align-items: center;
border: 1px solid hsl(351, 68%, 90%);
border-radius: .2em;
display: flex;
gap: .5em;
padding: 1ch;
}
.chord select {
/* -webkit-appearance: none; */
background: none;
border: none;
color: inherit;
font-family: inherit;
font-size: inherit;
outline: none;
}
.chord label:has(select:focus-visible) {
background: hsl(351, 68%, 90%);
color: hsl(351, 68%, 10%);
}
/* keyboard.js */
const getHz = (N = 0) => 440 * Math.pow(2, N / 12);
const notes = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'];
function getNote(N) {
const key = N % 12;
const note = notes[key < 0 ? 12 + key : key];
const octave = Math.ceil(4 + (N / 12)); /* 4 is octave of root, 440 */
return {
freq: getHz(N),
midi: N + 69, /* A4 === MIDI note number 69 */
note,
octave: note === 'B' || note === 'A#' ? octave - 1 : octave
}
}
function keyboard(start = -48, end = 40, chord) {
const data = freqs(start, end, chord);
return data.map(item => `<button aria-label="${item.note}${item.octave}" data-freq="${item.freq}" name="midi_${item.midi}"" style="--gcs:${item.offset}"${item.highlight ? ` class="keydown"`:''} type="button>"></button>`).join('\n')
}
/* chord.js */
const chords = {
'Major triad': [0, 4, 7],
'Minor triad': [0, 3, 7],
'Augmented triad': [0, 4, 8],
'Diminished triad': [0, 3, 6],
'Dominanth seventh': [0, 4, 7, 10],
'Major seventh': [0, 4, 7, 11],
'Minor-major seventh': [0, 3, 7, 11],
'Minor seventh': [0, 3, 7, 10],
'Augmented-major seventh': [0, 4, 8, 11],
'Augmented seventh': [0, 4, 8, 10],
'Half-diminished seventh': [0, 3, 8, 10],
'Diminished seventh': [0, 3, 6, 10],
'Dominant seventh flat five': [0, 4, 6, 10],
'Major ninth': [0, 4, 7, 11, 14],
'Dominant ninth': [0, 4, 7, 10, 14],
'Dominant minor ninth': [0, 4, 7, 10, 13],
'Minor-major ninth': [0, 3, 7, 11, 14],
'Minor ninth': [0, 3, 7, 10, 14],
'Augmented major ninth': [0, 4, 8, 11, 14],
'Augmented dominant ninth': [0, 4, 8, 10, 14],
'Half-diminished ninth': [0, 3, 6, 10, 14],
'Half-diminished minor ninth': [0, 3, 6, 10, 13],
'Diminished ninth': [0, 3, 6, 10, 14],
'Diminished minor ninth': [0, 3, 6, 10, 13],
}
function getChord(key, chord) {
const index = notes.indexOf(key);
return chords[chord].map(note => getNote(note + index))
}
/* audio.js */
const audioCtx = new (window.AudioContext || window.webkitAudioContext)
const gainNode = audioCtx.createGain()
const notemap = new Map();
gainNode.connect(audioCtx.destination);
function createOscillator(freq) {
const oscNode = audioCtx.createOscillator()
oscNode.type = 'triangle'
oscNode.frequency.value = freq
oscNode.connect(gainNode)
return oscNode
}
function noteon(key) {
if (!key.classList.contains('keydown')) {
gainNode.gain.value = 0.33
notemap.set(key.name, createOscillator(key.dataset.freq))
notemap.get(key.name).start(0)
key.classList.add('keydown')
}
}
function noteoff(key) {
key.classList.remove('keydown')
const oscNode = notemap.get(key.name)
if (oscNode) oscNode.stop(0)
notemap.delete(key.name)
}
function playChord(app, notes) {
app.querySelectorAll('.keydown').forEach(key => key.classList.remove('keydown'))
gainNode.gain.value = 1 / notes.length
notes.forEach(note => {
const key = app.elements[`midi_${note.midi}`]
key.classList.add('keydown')
const oscNode = createOscillator(note.freq)
oscNode.start(0)
oscNode.stop(audioCtx.currentTime + 1.5)
gainNode.gain.setTargetAtTime(0, 0.75, 0.25)
})
}
/* init */
app.addEventListener('submit', event => event.preventDefault());
app.addEventListener('change', () => {
const notes = getChord(key.value, chord.value);
playChord(app, notes)
})
/* safari hack */
app.addEventListener('pointerdown', () => audioCtx.resume());
Also see: Tab Triggers