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.
<h1>Native Date Picker (USM Script)</h1>
<div class="alert alert-info small">
<p>
Sample that demonstrates how to display and update dates using the native date Picker
control both using just a button or using the default date picker intput box.
</p>
<p>
This example uses a <b>DatePickerNative</b> helper class to facilitate binding
and unbinding of local time values as well as providing the CSS to show a
button-only date picker.
</p>
</div>
<hr>
<h3 class="mt-5">Date Picker Button</h3>
<hr>
<label class="bold">
Select a date:
</label>
<div>
<span id="ActiveDate">
</span>
<button id="DatePickerButton" class="datepicker-native-button btn btn-secondary btn-sm">
<i class="far fa-calendar-alt"></i>
<input id="DatePickerButtonInput" type="date" class="datepicker-native-button-input" />
</button>
</div>
<h3 class="mt-5">Date Picker Input Field</h3>
<hr>
<label class="bold" for="DatePickerInput">
Select a date:
</label>
<div>
<span id="ActiveDateInput">
</span>
<input id="DatePickerInput" type="date"
class="form-control mt-2 date " />
</div>
<style>
body {
padding: 1em;
}
.bold {
font-weight: 600;
}
.date {
width: 10em;
}
</style>
.datepicker-native-button {
position: relative;
}
.datepicker-native-button-input {
position: absolute;
overflow: hidden;
width: 100%;
height: 100%;
right: 0;
top: 0;
opacity: 0;
}
.datepicker-native-button-input::-webkit-calendar-picker-indicator {
position: absolute;
right: 0;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
opacity: 0;
cursor: pointer;
}
var startDate = new Date(2022,10,10);
console.log(startDate.toLocaleString());
var el = document.getElementById("DatePickerButtonInput");
// option configuration syntax
let dpn = new DatePickerNative({
element: el,
activeDate: startDate,
// + or - 5 days - optional
min: 5,
max: 5,
onDateChanged: function(dt, event) {
// update date display
showDate(dt,"ActiveDate");
console.log(dt.toLocaleString()," Callback date value");
// element gets a dateValue property
var el = document.getElementById("DatePickerButtonInput");
console.log(el.dateValue.toLocaleString()," Date on control");
// date control value
//console.log(DateFormatter.formatDate(dpn.options.activeDate,"dw MMM dd, yyyy")," instance.option.activeDate");
}
});
// assign initial display value on page launch
showDate(startDate,"ActiveDate");
var elInput = document.getElementById("DatePickerInput");
// parameter syntax - Standard Date Input control
new DatePickerNative({
element: elInput,
activeDate: startDate,
onDateChanged: function(dt, event, instance) {
showDate(dt, "ActiveDateInput");
console.log(dt.toLocaleString()," Callback date value Date Input control");
}
});
showDate(startDate,"ActiveDateInput");
// display helper
function showDate(dt,elId) {
var sdt = dt.toLocaleString();
document.getElementById(elId).innerText = sdt;
}
// ------------------
function DatePickerNative(el, initialDate, onDateChanged) {
var _this = this;
var opt = null;
if (typeof el == "string")
{
if (el === "uninitialize") {
uninitialize();
return;
}
el = document.getElementById(el);
if (!el) {
throw new Error("Invalid element provided. Provide either an DOM Element or an id string to an element.");
}
}
if (el.element) {
opt = el; // assume options object was passed
}
else {
opt = {
element: el,
onDateChanged: onDateChanged,
activeDate: initialDate,
min: "", // number as string
max: "" // number as string
};
}
this.options = opt;
function intialize(opt) {
if(typeof opt.activeDate != 'object')
opt.activeDate = new Date();
if (opt.element) {
opt.element.addEventListener("change",datePickerUnbind);
datePickerBind(opt.element,opt.activeDate, opt.onDateChanged);
}
}
function uninitialize(){
opt.element.removeEventListener("change",datePickerUnbind);
}
function datePickerBind(element, dt) {
var newDate = localToGmtDate(dt);
opt.element.dateValue = dt; // original date
opt.element.value = newDate;
if (opt.min)
opt.element.min = normalizeMin(opt.min);
if (opt.max)
opt.element.max = normalizeMax(opt.max);
}
function datePickerUnbind(event) {
var dt = event.target.valueAsDate;
let newDate = utcToLocalDate(dt);
opt.element.dateValue = newDate;
opt.activeDate = newDate;
if(opt.onDateChanged){
opt.onDateChanged(newDate, event, _this);
}
}
function localToGmtDate(localDate) {
return localDate && new Date(localDate.getTime()-(localDate.getTimezoneOffset()*60*1000)).toISOString().split('T')[0];
}
function utcToLocalDate(utcDate) {
return new Date(utcDate.getTime()+(utcDate.getTimezoneOffset()*60*1000));
}
function normalizeMin(minVal) {
if (typeof minVal === "string")
return minVal;
if (typeof minVal === "number") {
let dt = new Date();
dt = new Date(dt.setDate(dt.getDate() - minVal));
minVal = dt;
}
return localToGmtDate(minVal);
}
function normalizeMax(maxVal) {
if (typeof maxVal === "string") {
return maxVal;
}
let dt = new Date();
if (typeof maxVal === "number") {
dt = new Date();
dt = new Date(dt.setDate(dt.getDate() + maxVal));
maxVal = dt;
}
return localToGmtDate(maxVal);
}
intialize(opt);
return _this;
}
Also see: Tab Triggers