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.
.card
.song
h1.song__title
p.song__artist
p.song-duration
span.song-duration__lapsed 0:00
span |
span.song-duration__total 5:00
.disc
.disc-arm
.speakers.speakers_right
span.material-symbols-rounded apps
.speakers.speakers_left
span.material-symbols-rounded apps
.player-controls
audio.audio
button.player-controls__prev
span.material-symbols-rounded chevron_left
button.player-controls__play
span.material-symbols-rounded play_arrow
span.js-wave
button.player-controls__next
span.material-symbols-rounded chevron_right
.music-notes.music-notes_one ♫ ♩
.music-notes.music-notes_two ♯ ♪
.music-notes.music-notes_three ♪
.music-notes.music-notes_four ♩
.music-notes.music-notes_five ♫
.music-notes.music-notes_six ♪
@import url("https://fonts.googleapis.com/css2?family=Alkatra&display=swap");
/* --------------------------- Colors ------------------------- */
$white: rgba(255, 255, 255, 1);
$black: rgba(40, 40, 43, 1);
$onyx-black: rgba(53, 57, 53, 1);
$gray: rgb(113, 121, 126, 1);
$card-bg-gray: rgba(250, 250, 250, 1);
$card-bg-blue: rgba(188, 229, 247, 1);
$card-border-blue: rgba(0, 102, 178, 1);
$card-title: rgba(0, 102, 178, 1);
$disc-dark-red-circle: rgba(207, 16, 32, 1);
$disc-dark-red-stripe: rgba(255, 56, 0, 1);
$disc-light-red-stripe: rgba(228, 28, 56, 1);
/* ---------------------- Width & Heights --------------------- */
$card-width: 500px;
$card-height: 500px;
$card-radius: 150px;
$disc-width: 200px;
$disc-height: 200px;
$disc-radius: 50%;
/* --------------------------- Mixins -------------------------- */
@mixin flex {
display: flex;
}
@mixin flex-center {
justify-content: center;
align-items: center;
}
@mixin flex-column {
flex-direction: column;
justify-content: space-between;
align-items: center;
}
/* ------------------------ Reset CSS ----------------------- */
*,
*::after,
*::before {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.material-symbols-rounded {
font-variation-settings: "FILL" 1, "wght" 400, "GRAD" 200, "opsz" 48;
font-size: 30px;
color: $card-border-blue;
}
.player-controls__play {
.material-symbols-rounded {
font-size: 40px;
}
}
/* --------------------------- CSS -------------------------- */
body {
@include flex;
@include flex-center;
min-height: 100vh;
}
.card {
@include flex;
@include flex-column;
position: relative;
padding: 40px;
width: 100%;
max-width: $card-width;
height: $card-height;
border: 1px solid $card-border-blue;
border-radius: $card-radius;
background: radial-gradient(
circle at 18.7% 37.8%,
$card-bg-gray 0%,
$card-bg-blue 90%
);
box-shadow: 0 0 20px 1px rgba(0, 0, 0, 0.2);
}
.song {
@include flex;
@include flex-column;
font-family: "Alkatra", cursive;
&__title {
font-size: 26px;
color: $card-title;
letter-spacing: 1px;
line-height: 1.2;
}
&__artist {
font-size: 16px;
color: $black;
}
}
.song-duration {
@include flex;
gap: 2px;
font-family: "Alkatra", cursive;
font-size: 12px;
color: $gray;
&__lapsed,
&__total {
display: inline-block;
}
}
.disc {
width: $disc-width;
height: $disc-height;
border-radius: $disc-radius;
background: radial-gradient(
circle,
$white 0px,
$white 10px,
$black 10px,
$black 12px,
transparent 12px,
transparent 20px,
$disc-dark-red-circle 20px,
$disc-dark-red-circle 24px,
transparent 24px,
transparent 44px,
$black 44px,
$black 54px,
$onyx-black 54px,
$onyx-black 64px,
rgba(0, 0, 0, 0.3) 64px,
rgba(0, 0, 0, 0.3) 94px,
$gray 94px,
$gray 95px,
$black 95px
),
repeating-linear-gradient(
45deg,
$disc-light-red-stripe 0px,
$disc-light-red-stripe 2px,
$disc-dark-red-stripe 2px,
$disc-dark-red-stripe 4px
);
&_play {
animation: rotate-disc 3s linear 0s infinite forwards;
}
}
.disc-arm {
position: absolute;
top: 35%;
right: 22%;
height: 140px;
width: 10px;
background: black;
border-radius: 0 0 50% 50%;
box-shadow: 1px 1px 10px 0 rgba(0, 0, 0, 0.5);
transform-origin: top left;
transition: all 0.4s ease-in-out;
&::before {
content: "";
position: absolute;
top: -10px;
left: -100%;
width: 30px;
height: 50px;
border-radius: 35%;
background-image: linear-gradient(120deg, $gray 20%, darken(gray, 20%) 60%);
}
&_play {
transform: rotate(25deg);
animation: wiggle 500ms infinite linear;
}
}
.speakers {
position: absolute;
top: 45%;
&_right {
right: 10%;
}
&_left {
left: 10%;
}
}
.player-controls {
@include flex;
justify-content: space-between;
align-items: center;
position: relative;
width: 50%;
.audio {
position: absolute;
top: 0;
left: 0;
}
&__prev,
&__play,
&__next {
@include flex;
@include flex-center;
width: 40px;
height: 40px;
border-radius: 50%;
border: none;
background-image: linear-gradient(
120deg,
rgba(lighten($card-bg-blue, 15%), 0.3) 50%,
$card-bg-blue 51%
);
border-radius: 50%;
box-shadow: 0 0 0 2px $card-border-blue;
transition: all 0.2s ease-in-out;
&:hover {
box-shadow: 0 0 0 1px $card-border-blue;
cursor: pointer;
opacity: 0.8;
}
}
&__play {
position: relative;
width: 60px;
height: 60px;
.wave {
position: absolute;
top: 50%;
left: 50%;
content: "";
background: $card-border-blue;
border-radius: 50%;
transform: translate(-50%, -50%);
animation: animate-wave 0.6s linear infinite;
pointer-events: none;
}
}
}
.music-notes {
position: absolute;
font-size: 40px;
color: $card-border-blue;
opacity: 0;
&_one {
top: -20%;
animation-delay: 0.4s;
}
&_two {
bottom: -20%;
left: -10%;
animation-delay: 1s;
}
&_three {
top: 30%;
left: -10%;
animation-delay: 1.5s;
}
&_four {
bottom: -30%;
animation-delay: 0.8s;
}
&_five {
bottom: -5%;
right: -10%;
animation-delay: 1.2s;
}
&_six {
top: 10%;
right: -10%;
animation-delay: 0.2s;
}
&_play {
opacity: 1;
animation: musical-notes-playing 4s infinite linear;
}
}
@keyframes animate-wave {
0% {
width: 0px;
height: 0px;
opacity: 0.3;
}
100% {
width: 60px;
height: 60px;
opacity: 0;
}
}
@keyframes rotate-disc {
0% {
transform: rotateZ(0);
}
100% {
transform: rotateZ(360deg);
}
}
@keyframes musical-notes-playing {
0% {
transform: scale(1) translate(0, 0);
opacity: 0;
}
50% {
opacity: 1;
transform: scale(1.5) translate(50%, -50%);
}
80% {
opacity: 0;
transform: scale(1.5) translate(100%, -100%);
}
100% {
transform: scale(1.5) translate(100%, -100%);
opacity: 0;
}
}
@keyframes wiggle {
0% {
transform: translateY(-1px) rotate(25deg);
}
50% {
transform: translateY(0px) rotate(26deg);
}
100% {
transform: translateY(-1px) rotate(25deg);
}
}
/* ------------------------- Helper Functions ------------------------- */
const getDomElement = (baseElement = document, cssSelector = "") =>
baseElement.querySelector(cssSelector);
const convertSecsToMins = (secs) => {
return (secs / 60).toFixed(2);
};
/* ------------------------------ Songs Data ---------------------------- */
const songs = [
{
title: "Take Me Home",
artist: "John Denver",
url:
"https://raw.githubusercontent.com/5hraddha/misc/master/images/songs/take-me-home.mp3"
},
{
title: "I will be there for you",
artist: "The Rembrandts",
url:
"https://raw.githubusercontent.com/5hraddha/misc/master/images/songs/i-will-be-there-for-you.mp3"
},
{
title: "Count On Me",
artist: "Bruno Mars",
url:
"https://raw.githubusercontent.com/5hraddha/misc/master/images/songs/count-on-me.mp3"
},
{
title: "Happy",
artist: "Pharrell Williams",
url:
"https://raw.githubusercontent.com/5hraddha/misc/master/images/songs/happy.mp3"
}
];
/* ------------------------- Select DOM Elements ------------------------- */
const musicPlayer = getDomElement(document, ".card");
const musicalNotes = musicPlayer.querySelectorAll(".music-notes");
const playerDisc = getDomElement(musicPlayer, ".disc");
const playerDiscArm = getDomElement(musicPlayer, ".disc-arm");
const playBtn = getDomElement(musicPlayer, ".player-controls__play");
const playBtnIcon = getDomElement(playBtn, ".material-symbols-rounded");
const playBtnWaveAnimation = getDomElement(playBtn, ".js-wave");
const previousBtn = getDomElement(musicPlayer, ".player-controls__prev");
const nextBtn = getDomElement(musicPlayer, ".player-controls__next");
const audioElement = getDomElement(musicPlayer, ".audio");
const songTitle = getDomElement(musicPlayer, ".song__title");
const songArtist = getDomElement(musicPlayer, ".song__artist");
const songDurationLapsed = getDomElement(musicPlayer, ".song-duration__lapsed");
const songDurationTotal = getDomElement(musicPlayer, ".song-duration__total");
// Current song index
let currentSongIndex = 0;
// Load song once the music player is loaded
const loadSong = (song) => {
songTitle.textContent = song.title;
songArtist.textContent = song.artist;
audioElement.src = song.url;
};
// Play a song
const playSong = (playBtn, audioElement) => {
playBtn.classList.add("js-play");
playBtnWaveAnimation.classList.add("wave");
playerDisc.classList.add("disc_play");
playerDiscArm.classList.add("disc-arm_play");
musicalNotes.forEach((note) => note.classList.add("music-notes_play"));
playBtnIcon.textContent = "pause";
setTimeout("audioElement.play()", 500);
};
// Pause a song
const pauseSong = (playBtn, audioElement) => {
playBtn.classList.remove("js-play");
playBtnWaveAnimation.classList.remove("wave");
playerDisc.classList.remove("disc_play");
playerDiscArm.classList.remove("disc-arm_play");
musicalNotes.forEach((note) => note.classList.remove("music-notes_play"));
playBtnIcon.textContent = "play_arrow";
audioElement.pause();
};
// Get song's total duration after the it's metadata is loaded
const handleLoadedAudioMetadata = (event, songTotalDurationElement) => {
const songDurationInSecs = event.target.duration;
songTotalDurationElement.textContent = convertSecsToMins(songDurationInSecs);
};
// Play or pause the song whenever the play button is clicked
const handlePlayButton = (playBtn, audioElement) => {
const isSongPlaying = playBtn.classList.contains("js-play");
isSongPlaying
? pauseSong(playBtn, audioElement)
: playSong(playBtn, audioElement);
};
// Go to the previous song whenever the previous button is clicked
const handlePreviousBtn = () => {
currentSongIndex--;
if (currentSongIndex < 0) {
currentSongIndex = songs.length - 1;
}
loadSong(songs[currentSongIndex]);
playSong(playBtn, audioElement);
};
// Go to the next song whenever the previous button is clicked
const handleNextBtn = () => {
currentSongIndex++;
if (currentSongIndex == songs.length) {
currentSongIndex = 0;
}
loadSong(songs[currentSongIndex]);
playSong(playBtn, audioElement);
};
// Play the next song when the current song has ended
const handleCurrentSongEnded = () => handleNextBtn();
// Update the current song duration when the song is playing
const handleUpdateProgress = (event, songLapsedDurationElement) => {
const { currentTime, duration } = event.target;
songLapsedDurationElement.textContent = convertSecsToMins(currentTime);
};
// Add Event Listeners
playBtn.addEventListener("click", () =>
handlePlayButton(playBtn, audioElement)
);
previousBtn.addEventListener("click", handlePreviousBtn);
nextBtn.addEventListener("click", handleNextBtn);
audioElement.addEventListener("timeupdate", (event) => {
handleUpdateProgress(event, songDurationLapsed);
});
audioElement.addEventListener("ended", handleCurrentSongEnded);
audioElement.onloadedmetadata = (event) =>
handleLoadedAudioMetadata(event, songDurationTotal);
// Load song
loadSong(songs[currentSongIndex]);
Also see: Tab Triggers