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 id="wrapper">
<h1>Multilingual Text To Speech</h1>
<div class="uiunit">
<label for="speakerMenu">Voice: </label>
<select id="speakerMenu"></select> speaks
<span id="language">English.</span>
</div>
<p id="warning">Sorry, your browser does not support the Speech Synthesis API.</p>
<textarea id="txtFld">Choose any voice. Type here in the chosen language.</textarea>
<label for="txtFld">Type text above in the selected language (or copy from Google Translate). Then click the Speak button.</label>
<div>
<button type="button" id="speakBtn">Speak</button>
<br>
<p>Note: For best results on a Mac, use the latest version of Chrome, Safari, or FireFox. On Windows, use Chrome.</p>
</div>
</div>
@charset "UTF-8";
*, *:before, *:after {box-sizing: border-box;}
:root {
--textcolor:black;
--accentcolor:#269;
--lightaccentcolor:#9df;
--reversecolor:white;
}
html {
font-size: 16px;
font-family: Trebuchet,sans-serif;
}
body {
background: var(--accentcolor);
color: var(--reversecolor);
margin: 0;
padding: 0;
line-height: 1.5;
}
strong, b {font-weight: bold;}
#wrapper {
margin: 0 auto;
padding: 1.2rem;
width: 100%;
height: auto;
min-width: 320px;
max-width: 960px;
background: var(--reversecolor);
color: var(--textcolor);
text-align: left;
border:2px solid var(--accentcolor);
border-radius:0 0 16px 16px;
box-shadow:4px 4px 16px #333;
}
h1 {
color: var(--accentcolor);
font-size: 3rem;
margin: 0;
}
p {
display:block;
font-size: 1rem;
margin: 1rem 0 1rem 0;
}
a:link, a:visited {color: var(--accentcolor);}
a:hover {text-decoration: none;}
#txtFld {
width: 100%;
height: 4.4rem;
font-size: 1.6rem;
font-family: inherit;
}
button {
-webkit-appearance: none;
font-size: 1.4rem;
font-weight: normal;
background: var(--lightaccentcolor);
border-radius: 12px;
border: 2px solid var(--accentcolor);
color: var(--textcolor);
padding: 0.3rem 0.5rem 0.3rem 0.5rem;
cursor: pointer;
margin: 10px 20px 10px 20px;
}
button:hover {
color: var(--reversecolor);
background: var(--accentcolor);
}
button:disabled{
opacity:0.3;
cursor: not-allowed;
}
.uiunit {
display: inline-block;
margin: 1rem;
text-align: left;
}
#warning {
color: red;
font-size: 1.4rem;
display: none;
}
let speakBtn, txtFld, speakerMenu, language;
let allVoices, langtags;
let voiceIndex = 0;
function init(){
speakBtn = qs("#speakBtn");
txtFld = qs("#txtFld");
speakerMenu = qs("#speakerMenu");
language = qs("#language");
langtags = getLanguageTags();
speakBtn.addEventListener("click",talk,false);
speakerMenu.addEventListener("change",selectSpeaker,false);
if (window.speechSynthesis) {
if (speechSynthesis.onvoiceschanged !== undefined) {
//Chrome gets the voices asynchronously so this is needed
speechSynthesis.onvoiceschanged = setUpVoices;
}
setUpVoices(); //for all the other browsers
}else{
speakBtn.disabled = true;
speakerMenu.disabled = true;
qs("#warning").style.display = "block";
}
}
function setUpVoices(){
allVoices = getAllVoices();
createSpeakerMenu(allVoices);
}
function getAllVoices() {
let voicesall = speechSynthesis.getVoices();
let vuris = [];
let voices = [];
voicesall.forEach(function(obj,index){
let uri = obj.voiceURI;
if (!vuris.includes(uri)){
vuris.push(uri);
voices.push(obj);
}
});
voices.forEach(function(obj,index){obj.id = index;});
return voices;
}
function createSpeakerMenu(voices){
let code = ``;
voices.forEach(function(vobj,i){
code += `<option value=${vobj.id}>`;
code += `${vobj.name} (${vobj.lang})`;
code += vobj.voiceURI.includes(".premium") ? ' (premium)' : ``;
code += `</option>`;
});
speakerMenu.innerHTML = code;
speakerMenu.selectedIndex = voiceIndex;
}
//code for when the user selects a speaker
function selectSpeaker(){
voiceIndex = speakerMenu.selectedIndex;
let sval = Number(speakerMenu.value);
let voice = allVoices[sval];
let langcode = voice.lang.substring(0,2);
let langcodeobj = searchObjects(langtags,"code",langcode);
language.innerHTML = langcodeobj[0].name;
}
function talk(){
let sval = Number(speakerMenu.value);
let u = new SpeechSynthesisUtterance();
u.voice = allVoices[sval];
u.lang = u.voice.lang;
u.text = txtFld.value;
u.rate = 0.8;
speechSynthesis.speak(u);
}
function getLanguageTags(){
let langs = ["ar-Arabic","cs-Czech","da-Danish","de-German","el-Greek","en-English","eo-Esperanto","es-Spanish","et-Estonian","fi-Finnish","fr-French","he-Hebrew","hi-Hindi","hu-Hungarian","id-Indonesian","it-Italian","ja-Japanese","ko-Korean","la-Latin","lt-Lithuanian","lv-Latvian","nb-Norwegian Bokmal","nl-Dutch","nn-Norwegian Nynorsk","no-Norwegian","pl-Polish","pt-Portuguese","ro-Romanian","ru-Russian","sk-Slovak","sl-Slovenian","sq-Albanian","sr-Serbian","sv-Swedish","th-Thai","tr-Turkish","zh-Chinese"];
let langobjects = [];
for (let i=0;i<langs.length;i++){
let langparts = langs[i].split("-");
langobjects.push({"code":langparts[0],"name":langparts[1]});
}
return langobjects;
}
// Generic utility functions
function searchObjects(array,prop,term,casesensitive = false){
//searches an array of objects for a given term in a given property
//returns only those objects that test positive
let regex = new RegExp(term, casesensitive ? "" : "i");
let newArrayOfObjects = array.filter(obj => regex.test(obj[prop]));
return newArrayOfObjects;
}
function qs(selectorText){
//saves lots of typing for those who eschew Jquery
return document.querySelector(selectorText);
}
document.addEventListener('DOMContentLoaded', function (e) {
try {init();} catch (error){
console.log("Data didn't load", error);}
});
Also see: Tab Triggers