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.
<!-- (A) PERIOD SELECTOR -->
<div id="calPeriod">
<select id="calMonth"></select>
<input type="number" id="calYear">
</div>
<!-- (B) CALENDAR -->
<div id="calWrap"></div>
<!-- (C) EVENT FORM -->
<dialog id="calForm"><form method="dialog">
<div id="evtClose">X</div>
<h2>CALENDAR EVENT</h2>
<label>Date</label>
<input type="text" id="evtDate" readonly>
<label>Details</label>
<textarea id="evtTxt" required></textarea>
<input id="evtDel" type="button" value="Delete">
<input id="evtSave" type="submit" value="Save">
</form></dialog>
<!-- (X) VISIT CODE-BOXX -->
<div id="code-boxx">
Visit
<a href="https://code-boxx.com/simple-pure-javascript-calendar-events/" target="_blank">
Code Boxx
</a> for more details.
</div>
/* (A) ENTIRE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
body {
position: relative;
padding: 10px; margin: 0;
overflow-x: hidden;
}
:root {
--bg-color-a: #3b39af; /* page header + form label */
--bg-color-b: #404040; /* calendar header */
--bg-color-c: #d73a3a; /* buttons */
}
.hide { display: none !important; }
/* (B) PERIOD SELECTOR */
#calPeriod {
padding: 15px;
background: var(--bg-color-a);
}
#calPeriod input, #calPeriod select {
font-size: 24px;
border: 0; outline: none;
color: #fff; background: 0;
cursor: pointer;
}
#calPeriod option { color: #000; }
#calYear {
width: 100px;
margin-left: 10px;
}
/* (C) CALENDAR */
/* (C1) SHARED */
.calHead, .calRow { display: flex; }
.calCell { width: 14.2857%; }
/* (C2) HEADER - DAY NAMES */
.calHead .calCell {
color: #fff; background: var(--bg-color-b);
font-weight: 700; text-align: center;
padding: 15px 0;
}
/* (C3) BODY - INDIVIDUAL DAYS */
.calBlank { background: #eee; }
.calToday { background: #feffd3; }
.calToday .cellDate { color: #ff9b9b; }
.calBody .calCell {
border: 1px solid #f5f5f5;
min-height: 80px;
}
.cellDate, .evt { padding: 5px; }
.cellDate {
font-size: 14px;
font-weight: 700;
color: #b5b5b5;
}
.evt {
font-size: 14px;
white-space: nowrap; text-overflow: ellipsis;
overflow: hidden;
cursor: pointer;
}
/* (D) EVENT FORM */
#calForm {
position: absolute; top: 0; left: 0; z-index: 999;
width: 100vw; height: 100%; min-height: 100vh;
padding: 0; margin: 0; border: 0;
background: rgba(0,0,0,0.5);
}
#calForm:is([open]) {
display: flex; flex-wrap: wrap;
align-items: center; justify-content: center;
}
#calForm form {
position: relative;
width: 450px; padding: 15px;
border-radius: 10px;
background: #fff;
}
#evtClose {
position: absolute;
top: 0; right: 0;
padding: 10px;
color: #b12020;
font-size: 28px;
font-weight: 700;
cursor: pointer;
}
#calForm h2 { margin: 0 0 15px 0; }
#calForm label, #calForm input[type=text], #calForm textarea { width: 100%; }
#calForm label {
color: #a1a1a1;
font-weight: 700;
font-size: 12px;
margin-bottom: 5px;
}
#calForm input[type=text], #calForm textarea {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #eee;
background: #f5f5f5;
}
#calForm textarea {
height: 100px;
resize: none;
}
#calForm input[type=button], #calForm input[type=submit] {
border: 0; padding: 10px; margin: 10px 2px 0 0;
color: #fff; background: var(--bg-color-c);
cursor: pointer;
}
/* (X) DOES NOT MATTER */
#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 cal = {
// (A) PROPERTIES
// (A1) FLAGS & DATA
sMon : false, // week start on monday
data : null, // events for selected period
sDay : 0, sMth : 0, sYear : 0, // selected day month year
// (A2) MONTHS & DAY NAMES
months : [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
],
days : ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"],
// (A3) HTML ELEMENTS
hMth : null, hYear : null, // month/year selector
hWrap : null, // calendar wrapper
hFormWrap : null, hForm : null, // event form
hfDate : null, hfTxt : null, hfDel : null, // form fields
// (B) INIT CALENDAR
init : () => {
// (B1) GET HTML ELEMENTS
cal.hMth = document.getElementById("calMonth");
cal.hYear = document.getElementById("calYear");
cal.hWrap = document.getElementById("calWrap");
cal.hFormWrap = document.getElementById("calForm");
cal.hForm = cal.hFormWrap.querySelector("form");
cal.hfDate = document.getElementById("evtDate");
cal.hfTxt = document.getElementById("evtTxt");
cal.hfDel = document.getElementById("evtDel");
// (B2) APPEND MONTHS/YEAR
let now = new Date(), nowMth = now.getMonth();
cal.hYear.value = parseInt(now.getFullYear());
for (let i=0; i<12; i++) {
let opt = document.createElement("option");
opt.value = i;
opt.innerHTML = cal.months[i];
if (i==nowMth) { opt.selected = true; }
cal.hMth.appendChild(opt);
}
// (B3) ATTACH CONTROLS
cal.hMth.onchange = cal.draw;
cal.hYear.onchange = cal.draw;
cal.hForm.onsubmit = cal.save;
document.getElementById("evtClose").onclick = () => cal.hFormWrap.close();
cal.hfDel.onclick = cal.del;
// (B4) START - DRAW CALENDAR
if (cal.sMon) { cal.days.push(cal.days.shift()); }
cal.draw();
},
// (C) DRAW CALENDAR FOR SELECTED MONTH
draw : () => {
// (C1) DAYS IN MONTH + START/END DAYS
// note - jan is 0 & dec is 11
// note - sun is 0 & sat is 6
cal.sMth = parseInt(cal.hMth.value); // selected month
cal.sYear = parseInt(cal.hYear.value); // selected year
let daysInMth = new Date(cal.sYear, cal.sMth+1, 0).getDate(), // number of days in selected month
startDay = new Date(cal.sYear, cal.sMth, 1).getDay(), // first day of the month
endDay = new Date(cal.sYear, cal.sMth, daysInMth).getDay(), // last day of the month
now = new Date(), // current date
nowMth = now.getMonth(), // current month
nowYear = parseInt(now.getFullYear()), // current year
nowDay = cal.sMth==nowMth && cal.sYear==nowYear ? now.getDate() : null ;
// (C2) LOAD DATA FROM LOCALSTORAGE
cal.data = localStorage.getItem("cal-" + cal.sMth + "-" + cal.sYear);
if (cal.data==null) {
localStorage.setItem("cal-" + cal.sMth + "-" + cal.sYear, "{}");
cal.data = {};
} else { cal.data = JSON.parse(cal.data); }
// (C3) DRAWING CALCULATIONS
// (C3-1) BLANK SQUARES BEFORE START OF MONTH
let squares = [];
if (cal.sMon && startDay != 1) {
let blanks = startDay==0 ? 7 : startDay ;
for (let i=1; i<blanks; i++) { squares.push("b"); }
}
if (!cal.sMon && startDay != 0) {
for (let i=0; i<startDay; i++) { squares.push("b"); }
}
// (C3-2) DAYS OF THE MONTH
for (let i=1; i<=daysInMth; i++) { squares.push(i); }
// (C3-3) BLANK SQUARES AFTER END OF MONTH
if (cal.sMon && endDay != 0) {
let blanks = endDay==6 ? 1 : 7-endDay;
for (let i=0; i<blanks; i++) { squares.push("b"); }
}
if (!cal.sMon && endDay != 6) {
let blanks = endDay==0 ? 6 : 6-endDay;
for (let i=0; i<blanks; i++) { squares.push("b"); }
}
// (C4) "RESET" CALENDAR
cal.hWrap.innerHTML = `<div class="calHead"></div>
<div class="calBody">
<div class="calRow"></div>
</div>`;
// (C5) CALENDAR HEADER - DAY NAMES
wrap = cal.hWrap.querySelector(".calHead");
for (let d of cal.days) {
let cell = document.createElement("div");
cell.className = "calCell";
cell.innerHTML = d;
wrap.appendChild(cell);
}
// (C6) CALENDAR BODY - INDIVIDUAL DAYS & EVENTS
wrap = cal.hWrap.querySelector(".calBody");
row = cal.hWrap.querySelector(".calRow");
for (let i=0; i<squares.length; i++) {
// (C6-1) GENERATE CELL
let cell = document.createElement("div");
cell.className = "calCell";
if (nowDay==squares[i]) { cell.classList.add("calToday"); }
if (squares[i]=="b") { cell.classList.add("calBlank"); }
else {
cell.innerHTML = `<div class="cellDate">${squares[i]}</div>`;
if (cal.data[squares[i]]) {
cell.innerHTML += "<div class='evt'>" + cal.data[squares[i]] + "</div>";
}
cell.onclick = () => { cal.show(cell); };
}
row.appendChild(cell);
// (C6-2) NEXT ROW
if (i!=(squares.length-1) && i!=0 && (i+1)%7==0) {
row = document.createElement("div");
row.className = "calRow";
wrap.appendChild(row);
}
}
},
// (D) SHOW EDIT EVENT DOCKET FOR SELECTED DAY
show : cell => {
cal.hForm.reset();
cal.sDay = cell.querySelector(".cellDate").innerHTML;
cal.hfDate.value = `${cal.sDay} ${cal.months[cal.sMth]} ${cal.sYear}`;
if (cal.data[cal.sDay] !== undefined) {
cal.hfTxt.value = cal.data[cal.sDay];
cal.hfDel.classList.remove("hide");
} else { cal.hfDel.classList.add("hide"); }
cal.hFormWrap.show();
},
// (E) SAVE EVENT
save : () => {
cal.data[cal.sDay] = cal.hfTxt.value;
localStorage.setItem(`cal-${cal.sMth}-${cal.sYear}`, JSON.stringify(cal.data));
cal.draw();
cal.hFormWrap.close();
return false;
},
// (F) DELETE EVENT FOR SELECTED DATE
del : () => { if (confirm("Delete event?")) {
delete cal.data[cal.sDay];
localStorage.setItem(`cal-${cal.sMth}-${cal.sYear}`, JSON.stringify(cal.data));
cal.draw();
cal.hFormWrap.close();
}}
};
window.onload = cal.init;
Also see: Tab Triggers