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.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text to Speech Application</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
line-height: 1.6;
}
h1 {
text-align: center;
color: #333;
}
.container {
background-color: #f9f9f9;
border-radius: 10px;
padding: 20px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
textarea {
width: 100%;
height: 150px;
padding: 10px;
margin-bottom: 15px;
border-radius: 5px;
border: 1px solid #ddd;
resize: vertical;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 15px;
margin-bottom: 15px;
}
.controls > div {
flex: 1;
min-width: 200px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
select, input {
width: 100%;
padding: 8px;
border-radius: 5px;
border: 1px solid #ddd;
}
.buttons {
display: flex;
gap: 10px;
}
button {
flex: 1;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
button#pauseBtn {
background-color: #f39c12;
}
button#pauseBtn:hover {
background-color: #e67e22;
}
button#stopBtn {
background-color: #e74c3c;
}
button#stopBtn:hover {
background-color: #c0392b;
}
.status {
text-align: center;
margin-top: 15px;
font-style: italic;
color: #666;
}
</style>
</head>
<body>
<h1>Text to Speech Converter</h1>
<div class="container">
<textarea id="textInput" placeholder="Enter text to convert to speech...">Hello! This is a text to speech demo. You can change my voice, rate, and pitch using the controls below.</textarea>
<div class="controls">
<div>
<label for="voiceSelect">Voice:</label>
<select id="voiceSelect"></select>
</div>
<div>
<label for="rate">Rate: <span id="rateValue">1</span></label>
<input type="range" id="rate" min="0.5" max="2" value="1" step="0.1">
</div>
<div>
<label for="pitch">Pitch: <span id="pitchValue">1</span></label>
<input type="range" id="pitch" min="0.5" max="2" value="1" step="0.1">
</div>
</div>
<div class="buttons">
<button id="speakBtn">Speak</button>
<button id="pauseBtn">Pause/Resume</button>
<button id="stopBtn">Stop</button>
</div>
<p class="status" id="statusText">Ready</p>
</div>
<script>
// Initialize speech synthesis
const synth = window.speechSynthesis;
let voices = [];
let currentUtterance = null;
let isPaused = false;
// DOM elements
const textInput = document.getElementById('textInput');
const voiceSelect = document.getElementById('voiceSelect');
const rate = document.getElementById('rate');
const rateValue = document.getElementById('rateValue');
const pitch = document.getElementById('pitch');
const pitchValue = document.getElementById('pitchValue');
const speakBtn = document.getElementById('speakBtn');
const pauseBtn = document.getElementById('pauseBtn');
const stopBtn = document.getElementById('stopBtn');
const statusText = document.getElementById('statusText');
// Populate voice list
function populateVoiceList() {
voices = synth.getVoices();
// Clear existing options
voiceSelect.innerHTML = '';
// Add voices to select element
voices.forEach((voice, index) => {
const option = document.createElement('option');
option.textContent = `${voice.name} (${voice.lang})`;
option.setAttribute('data-lang', voice.lang);
option.setAttribute('data-name', voice.name);
option.value = index;
voiceSelect.appendChild(option);
});
}
// Check if voices are already loaded (Chrome loads asynchronously)
if (synth.onvoiceschanged !== undefined) {
synth.onvoiceschanged = populateVoiceList;
} else {
// For browsers that don't fire onvoiceschanged
populateVoiceList();
}
// Update rate and pitch displays
rate.addEventListener('input', () => {
rateValue.textContent = rate.value;
});
pitch.addEventListener('input', () => {
pitchValue.textContent = pitch.value;
});
// Speak function
function speak() {
// Cancel any ongoing speech
if (synth.speaking) {
synth.cancel();
}
const text = textInput.value.trim();
// Check if there's text to speak
if (text === '') {
statusText.textContent = 'Please enter some text to speak.';
return;
}
// Create speech utterance
currentUtterance = new SpeechSynthesisUtterance(text);
// Set selected voice
if (voices.length > 0) {
const selectedVoice = voices[voiceSelect.value];
currentUtterance.voice = selectedVoice;
}
// Set rate and pitch
currentUtterance.rate = parseFloat(rate.value);
currentUtterance.pitch = parseFloat(pitch.value);
// Add event listeners
currentUtterance.onstart = () => {
statusText.textContent = 'Speaking...';
speakBtn.disabled = true;
};
currentUtterance.onend = () => {
statusText.textContent = 'Finished speaking.';
speakBtn.disabled = false;
isPaused = false;
};
currentUtterance.onerror = (event) => {
statusText.textContent = `Error occurred: ${event.error}`;
speakBtn.disabled = false;
};
// Start speaking
synth.speak(currentUtterance);
}
// Event listeners for buttons
speakBtn.addEventListener('click', speak);
pauseBtn.addEventListener('click', () => {
if (!synth.speaking) {
statusText.textContent = 'Nothing to pause.';
return;
}
if (isPaused) {
synth.resume();
isPaused = false;
statusText.textContent = 'Resumed speaking.';
} else {
synth.pause();
isPaused = true;
statusText.textContent = 'Paused speaking.';
}
});
stopBtn.addEventListener('click', () => {
if (synth.speaking) {
synth.cancel();
isPaused = false;
statusText.textContent = 'Stopped speaking.';
speakBtn.disabled = false;
} else {
statusText.textContent = 'Nothing to stop.';
}
});
</script>
</body>
</html>
Also see: Tab Triggers