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.
<div class="widget-wrap">
<div id="talk"></div>
<h1>JAVASCRIPT VOICE SEARCH</h1>
<form id="search-form" onsubmit="return false;">
<!-- (A) USUAL SEARCH FIELD & BUTTON -->
<input type="text" id="search-field"/>
<input type="submit" value="Search" disabled/>
<!-- (B) SPEECH SEARCH -->
<input type="button" id="search-speech" value="Loading" disabled/>
<div>
* Search form will not submit. This is only a demo for populating a search field with voice recognition.
</div>
</form>
<!-- (X) VISIT CODE-BOXX -->
<div id="code-boxx">
Visit
<a href="https://code-boxx.com/voice-search-javascript-speech-recognition/" target="_blank">
Code Boxx
</a> for more details.
</div>
</div>
input {
display: block;
width: 100%;
padding: 10px;
margin: 10px;
}
input[type=button], input[type=submit] {
background: #b90a0a;
color: #fff;
font-weight: 700;
border: 0;
}
/* (X) DOES NOT MATTER - COSMETICS */
/* PAGE & BODY */
* {
font-family: arial, sans-serif;
box-sizing: border-box;
}
body {
display: flex;
align-items: center; justify-content: center;
min-height: 100vh;
background-image: url(https://images.unsplash.com/photo-1619793906000-a89279ed88e0?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTY0NzMxMTIzMQ&ixlib=rb-1.2.1&q=85);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
text-align: center;
}
/* WIDGET */
.widget-wrap {
width: 600px;
padding: 30px;
border-radius: 20px;
background: rgba(255, 255, 255, 0.4);
}
/* SVG */
#talk {
width: 100%; height:120px;
background-image: url('data:image/svg+xml;utf8,<svg viewBox="0 0 512 512" width="100" xmlns="http://www.w3.org/2000/svg"><path d="M256 32C114.6 32 0 125.1 0 240c0 49.6 21.4 95 57 130.7C44.5 421.1 2.7 466 2.2 466.5c-2.2 2.3-2.8 5.7-1.5 8.7S4.8 480 8 480c66.3 0 116-31.8 140.6-51.4 32.7 12.3 69 19.4 107.4 19.4 141.4 0 256-93.1 256-208S397.4 32 256 32zM128 272c-17.7 0-32-14.3-32-32s14.3-32 32-32 32 14.3 32 32-14.3 32-32 32zm128 0c-17.7 0-32-14.3-32-32s14.3-32 32-32 32 14.3 32 32-14.3 32-32 32zm128 0c-17.7 0-32-14.3-32-32s14.3-32 32-32 32 14.3 32 32-14.3 32-32 32z" /></svg>');
background-repeat: no-repeat;
background-position: center;
}
/* FOOTER */
#code-boxx {
font-weight: 600;
margin-top: 30px;
}
#code-boxx a {
display: inline-block;
border: 0;
padding: 5px;
text-decoration: none;
background: #b90a0a;
color: #fff;
}
var voice = {
// (A) INIT SPEECH RECOGNITION
sform : null, // HTML SEARCH FORM
sfield : null, // HTML SEARCH FIELD
sbtn : null, // HTML VOICE SEARCH BUTTON
recog : null, // SPEECH RECOGNITION OBJECT
init : function () {
// (A1) GET HTML ELEMENTS
voice.sfrom = document.getElementById("search-form");
voice.sfield = document.getElementById("search-field");
voice.sbtn = document.getElementById("search-speech");
// (A2) GET MICROPHONE ACCESS
navigator.mediaDevices.getUserMedia({ audio: true })
.then((stream) => {
// (A3) SPEECH RECOGNITION OBJECT + SETTINGS
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
voice.recog = new SpeechRecognition();
voice.recog.lang = "en-US";
voice.recog.continuous = false;
voice.recog.interimResults = false;
// (A4) POPUPLATE SEARCH FIELD ON SPEECH RECOGNITION
voice.recog.onresult = (evt) => {
let said = evt.results[0][0].transcript.toLowerCase();
voice.sfield.value = said;
// voice.sform.submit();
// OR RUN AN AJAX/FETCH SEARCH
voice.stop();
};
// (A5) ON SPEECH RECOGNITION ERROR
voice.recog.onerror = (err) => { console.error(err); };
// (A6) READY!
voice.sbtn.disabled = false;
voice.stop();
})
.catch((err) => {
console.error(err);
voice.sbtn.value = "Please enable access and attach microphone.";
});
},
// (B) START SPEECH RECOGNITION
start : () => {
voice.recog.start();
voice.sbtn.onclick = voice.stop;
voice.sbtn.value = "Speak Now Or Click Again To Cancel";
},
// (C) STOP/CANCEL SPEECH RECOGNITION
stop : () => {
voice.recog.stop();
voice.sbtn.onclick = voice.start;
voice.sbtn.value = "Press To Speak";
}
};
window.addEventListener("DOMContentLoaded", voice.init);
Also see: Tab Triggers