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.
<body>
<!-- No need to add anything to your HTML -->
<div id="main">
<section class="container-1">
<h1>Notifs</h1>
<h2>Clean, yet beautiful notifications in your website</h2>
<button onclick="createNotification()">Show notification</button>
</section>
<input id="input-content" type="text" value="Your notification text" placeholder="Your notification text">
<section class="container-2">
<label for="select-theme">Theme</label>
<div class="select-style">
<select id="select-theme" name="select-theme">
<option value="0">Light</option>
<option value="1" selected>Dark</option>
</select>
</div>
<label for="select-content-type">Content type</label>
<div class="select-style">
<select id="select-content-type" name="select-content-type">
<option value="0" selected>Default</option>
<option value="1">Success</option>
<option value="2">Error</option>
<option value="3">Warning</option>
<option value="4">Info</option>
</select>
</div>
<label for="select-position">Position</label>
<div class="select-style">
<select id="select-position" name="select-position">
<option value="0" selected>Whole screen</option>
<option value="1">Right side</option>
<option value="2">Bottom side</option>
<option value="3">Bottom right corner</option>
</select>
</div>
<label for="select-regime">Regime</label>
<div class="select-style">
<select id="select-regime" name="select-regime">
<option value="0" selected>Just one</option>
<option value="1">Dominant</option>
<option value="2">Side by side</option>
<option value="3">Clear</option>
</select>
</div>
<label for="select-lifespan">Lifespan</label>
<div class="select-style">
<select id="select-lifespan" name="select-lifespan">
<option value="0">One second</option>
<option value="1" selected>Two seconds</option>
<option value="2">Three seconds</option>
<option value="3">Four seconds</option>
<option value="4">Five seconds</option>
<option value="5">Till close</option>
</select>
</div>
</section>
</div>
</body>
/* notifs styles */
:root {
--notifs-background-color: rgba(255, 255, 255, 0.9);
--notifs-content-color: #000000;
--notifs-content-size: 1.5em;
--notifs-content-opaque-color: rgba(177, 177, 177, 0.5);
--notifs-content-type-success-color: #2eb74d;
--notifs-content-type-error-color: #c43b48;
--notifs-content-type-warning-color: #dba810;
--notifs-content-type-info-color: #38a2b3;
}
.notifs-close-button {
font-size: xxx-large;
cursor: pointer;
}
.notifs-canvas {
display: flex;
right: 0;
bottom: 0;
background-color: var(--notifs-background-color);
position: fixed;
overflow-x: hidden;
z-index: 9999;
}
.notifs-canvas-hidden {
width: 0;
height: 0;
transition: height 0s linear 0.5s, width 0.5s cubic-bezier(0.645, 0.045, 0.355, 1);
}
.notifs-canvas-whole-screen {
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
transition: width 0.5s cubic-bezier(0.645, 0.045, 0.355, 1);
}
.notifs-canvas-whole-screen .notifs-content {
max-width: 100vw;
max-height: 100vh;
}
.notifs-canvas-right-side {
justify-content: center;
align-items: center;
width: 20em;
height: 100vh;
transition: width 0.5s cubic-bezier(0.645, 0.045, 0.355, 1);
}
.notifs-canvas-right-side .notifs-content {
max-width: 20em;
max-height: 100vh;
}
.notifs-canvas-bottom-side {
justify-content: center;
align-items: center;
width: 100vw;
height: 10em;
transition: width 0.5s cubic-bezier(0.645, 0.045, 0.355, 1);
}
.notifs-canvas-bottom-side .notifs-content {
max-width: 100vw;
max-height: 10em;
}
.notifs-canvas-bottom-right-corner {
justify-content: center;
align-items: center;
width: 20em;
height: 10em;
transition: width 0.5s cubic-bezier(0.645, 0.045, 0.355, 1);
}
.notifs-canvas-bottom-right-corner .notifs-content {
max-width: 20em;
max-height: 10em;
}
.notifs-content {
display: flex;
flex-direction: column;
align-content: space-between;
margin: 5%;
padding: unset;
font-size: var(--notifs-content-size);
color: var(--notifs-content-color);
}
.notifs-content-type-1 {
color: var(--notifs-content-type-success-color);
}
.notifs-content-type-2 {
color: var(--notifs-content-type-error-color);
}
.notifs-content-type-3 {
color: var(--notifs-content-type-warning-color);
}
.notifs-content-type-4 {
color: var(--notifs-content-type-info-color);
}
.notifs-content-hidden {
display: none;
}
.notifs-content li {
list-style: none;
text-align: center;
}
.notifs-content li:not(:last-child) {
padding-bottom: 3%;
}
.notifs-hidden {
display: none;
}
.notifs-opaque {
color: var(--notifs-content-opaque-color);
}
/* end of notifs styles*/
/*-----------------------------------------------------*/
/* showcase page styles */
body {
margin: 0;
padding: 0;
font-family: Montserrat Light, Arial, Sans Serif;
color: #343434;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(to bottom right, #fff, #bfbfbf);
min-height: 100vh;
}
h1 {
font-size: 3em;
font-weight: normal;
margin: 1em 0 0.5em 0;
}
h2 {
font-size: 1.2em;
font-weight: normal;
}
.container-1, .container-2 {
margin-left: 2.5em;
margin-right: 2.5em;
display: flex;
flex-direction: column;
align-content: space-between;
}
.container-2 {
align-items: center;
}
.container-2 *:not(label) {
margin-top: 0.5em;
margin-bottom: 0.5em;
}
.container-2:last-child {
margin-bottom: 4em;
}
button, input, select {
width: 100%;
height: 1.5em;
font-family: Montserrat Light, Arial, Sans Serif;
font-size: 1.3em;
color: #343434;
border: none;
}
button {
height: 3em;
margin-top: 3em;
background-color: #fff;
cursor: pointer;
border-radius: 50px;
-moz-box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
-webkit-box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
transition: all .2s linear;
}
input {
width: 99%;
background: none;
border-bottom: #878787 solid 2px;
text-align: center;
font-size: 1.7em;
margin-top: 3.5em;
margin-bottom: 3em;
}
label {
font-size: 1.3em;
margin-top: 1.5em;
margin-bottom: 1em;
}
/*select*/
.select-style {
padding: 0;
margin: 0;
width: 100%;
border-radius: 10px;
overflow: hidden;
background-color: #fff;
-moz-box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
-webkit-box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
}
.select-style select {
height: 2em;
text-indent: 0.5em;
width: 100%;
border: none;
box-shadow: none;
background-color: transparent;
background-image: none;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
.select-style select:focus {
outline: none;
}
/*end of select*/
button:hover {
transform: scale(1.02, 1.02);
}
button:active {
transform: scale(1, 1);
}
/** Enums representing notification's options. */
/** Light and dark theme */
enum NTheme { Light, Dark, }
/** Notification content type - differs in content color */
enum NContentType { Default, Success, Error, Warning, Info, }
/** Position of notification (with BottomRightCorner consider setting Regime to JustOne or Clear) */
enum NPosition { WholeScreen, RightSide, BottomSide, BottomRightCorner, }
/** Regime - JustOne (shows only last notification), Dominant (shows all notifications, but the last one is more dominant), SideBySide (shows all notifications), Clear (shown only last and clears history) */
enum NRegime { JustOne, Dominant, SideBySide, Clear, }
/** Lifespan of notification, when TillClosed set, close button will appear */
enum NLifespan { OneSecond, TwoSeconds, ThreeSeconds, FourSeconds, FiveSeconds, TillClosed, }
/*-----------------------------------------------------*/
/**
* Necessary to call to make notifs work
*/
function initNotifs(): void {
let olNotifications = document.createElement('ol');
olNotifications.classList.add('notifs-content', 'notifs-content-hidden');
let divNotifications = document.createElement('div');
divNotifications.classList.add('notifs-canvas', 'notifs-canvas-hidden');
divNotifications.appendChild(olNotifications);
//append necessary notifs element to body
document.getElementsByTagName('body')[0].appendChild(divNotifications);
}
initNotifs(); //calling to init notifs
/*-----------------------------------------------------*/
/**
* Call this function with desired parameters to build notification.
*
* @param {string} text Content text of notification.
* @param {NTheme} theme Theme of notification.
* @param {NContentType} contentType Differentiates content text.
* @param {NPosition} position Whole screen / Right side / Bottom right corner.
* @param {NRegime} regime Option to show just the last one or the other notifications too.
* @param {NLifespan} lifespan Lifespan of notification, could disappear after few seconds or stay there till user dismisses it.
*/
function buildNotification(
text: string,
theme: NTheme = NTheme.Dark,
contentType: NContentType = NContentType.Default,
position: NPosition = NPosition.BottomRightCorner,
regime: NRegime = NRegime.JustOne,
lifespan: NLifespan = NLifespan.OneSecond): void {
//remove close button
removeCloseButton();
//theme
document.documentElement.style.setProperty('--notifs-background-color', theme === NTheme.Dark ? 'rgba(0, 0, 0, 0.8)' : 'rgba(255, 255, 255, 0.9)');
document.documentElement.style.setProperty('--notifs-content-color', theme === NTheme.Dark ? '#ffffff' : '#000000');
//position
getCanvas().classList.remove('notifs-canvas-hidden');
getContent().classList.remove('notifs-content-hidden');
switch (position) {
case NPosition.WholeScreen:
getCanvas().classList.add('notifs-canvas-whole-screen');
break;
case NPosition.RightSide:
getCanvas().classList.add('notifs-canvas-right-side');
break;
case NPosition.BottomSide:
getCanvas().classList.add('notifs-canvas-bottom-side');
break;
default:
getCanvas().classList.add('notifs-canvas-bottom-right-corner');
break;
}
//regime
//recommended regime for bottom right corner notification is JustOne or Clear
switch (regime) {
case NRegime.Clear:
clearActives();
break;
case NRegime.JustOne:
hideAll();
break;
case NRegime.Dominant:
makeAllDefault();
makeAllOpaque();
break;
default:
makeAllDefault();
break;
}
//content and content type
//it takes roughly about 100 characters to correctly fit bottom right corner notification
addNotification(text, contentType);
//lifespan
if (lifespan !== NLifespan.TillClosed) {
let timeout = 0;
switch (lifespan) {
case NLifespan.TwoSeconds:
timeout = 2000;
break;
case NLifespan.ThreeSeconds:
timeout = 3000;
break;
case NLifespan.FourSeconds:
timeout = 4000;
break;
case NLifespan.FiveSeconds:
timeout = 5000;
break;
default:
timeout = 1000;
break;
}
setTimeout(() => {
hide();
}, timeout);
} else {
addCloseButton();
}
}
/*-----------------------------------------------------*/
function getCanvas(): HTMLElement {
return document.getElementsByClassName('notifs-canvas')[0] as HTMLElement;
}
function getCloseButton(): HTMLElement {
return document.getElementsByClassName('notifs-cross')[0] as HTMLElement;
}
function getContent(): HTMLOListElement {
return document.getElementsByClassName('notifs-content')[0] as HTMLOListElement;
}
function getActiveCount(): number {
return document.getElementById('notifs-content').childElementCount;
}
/**
* Adds notification to html document. Called from buildNotification.
*/
function addNotification(text: string, contentType: NContentType = NContentType.Default): void {
let newNotification = document.createElement('li');
newNotification.innerHTML = text;
if (contentType !== NContentType.Default) {
newNotification.classList.add(`notifs-content-type-${contentType}`);
}
getContent().appendChild(newNotification);
}
function addCloseButton(): void {
let closeButton = document.createElement('li');
closeButton.innerHTML = '×';
closeButton.id = 'notifs-close-button';
closeButton.classList.add('notifs-close-button');
closeButton.addEventListener('click', hide);
getContent().appendChild(closeButton);
}
function removeCloseButton(): void {
if (getContent().lastChild !== null && ((getContent().lastChild as HTMLElement).id === 'notifs-close-button')) {
getContent().removeChild(getContent().lastChild);
}
}
function hide(): void {
getCanvas().classList.remove('notifs-canvas-whole-screen', 'notifs-canvas-right-side', 'notifs-canvas-bottom-side', 'notifs-canvas-bottom-right-corner');
getCanvas().classList.add('notifs-canvas-hidden');
getCanvas().removeEventListener('click', hide);
getContent().classList.add('notifs-content-hidden');
}
function clearActives(): void {
while (getContent().hasChildNodes()) {
getContent().removeChild(getContent().lastChild);
}
}
function makeAllDefault(): void {
for (let i = 0; i < getContent().children.length; i++) {
getContent().children[i].classList.remove('notifs-hidden', 'notifs-opaque');
}
}
function hideAll(): void {
for (let i = 0; i < getContent().children.length; i++) {
getContent().children[i].classList.add('notifs-hidden');
}
}
function makeAllOpaque(): void {
for (let i = 0; i < getContent().children.length; i++) {
getContent().children[i].classList.add('notifs-opaque');
}
}
/*-----------------------------------------------------*/
/**
* This function is called from showcase site. For your project, call buildNotification above to create notifs.
*/
function createNotification() {
const content = (document.getElementById('input-content') as HTMLInputElement).value;
const theme = Number((document.getElementById('select-theme') as HTMLSelectElement).value);
const contentType = Number((document.getElementById('select-content-type') as HTMLSelectElement).value);
const position = Number((document.getElementById('select-position') as HTMLSelectElement).value);
const regime = Number((document.getElementById('select-regime') as HTMLSelectElement).value);
const lifespan = Number((document.getElementById('select-lifespan') as HTMLSelectElement).value);
buildNotification(content, theme, contentType, position, regime, lifespan);
}
Also see: Tab Triggers