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.
<div class="max-w-lg my-0 mx-auto">
<h1 class="text-2xl mb-4 underline">Detect Browser</h1>
<div class="result">
<h2 class="text-1xl my-4 italic underline">Navigator</h2>
<p class="my-1"><span class="font-bold">userAgent:</span> <span id="useragent">-</span></p>
<p class="my-1"><span class="font-bold">appVersion:</span> <span id="appVersion">-</span></p>
<p class="my-1"><span class="font-bold">platform:</span> <span id="platform">-</span></p>
<p class="my-1"><span class="font-bold">vendor:</span> <span id="vendor">-</span></p>
<h2 class="text-1xl my-4 italic underline">Feature Detection</h2>
<p class="my-1"><span class="font-bold">Apple OS:</span> <span id="AppleOS">-</span></p>
<p class="my-1"><span class="font-bold">Apple Device:</span> <span id="AppleDevice">-</span></p>
<p class="my-1"><span class="font-bold">Device Detection:</span> <span id="DeviceDetection">-</span></p>
<p class="my-1"><span class="font-bold">Browser:</span> <span id="Browser">-</span></p>
<p class="my-1"><span class="font-bold">iOS Version:</span> <span id="iOSVersion">-</span></p>
</div>
<div class="m-16 text-xs text-center underline"><a href="https://linktr.ee/natterstefan" target="_blank">made with <span aria-label="a red heart" role="img">❤️</span> by @natterstefan</a></div>
</div>
body {
margin: 1.75rem;
background: #efefef;
}
/**
* # Detect Current Apple Device (macOS or iOS) and Browser
* (Chrome or not Chrome)
*
* Misc Notes
* - https://github.com/DamonOehlman/detect-browser (not tested)
*/
/**
* The problem with > iOS 13 in general is that it mimics the user agent of Macs.
* Therefore it is difficult to get the correct iOS Version >= 13.
* @see https://stackoverflow.com/a/57838385/1238150
*
* inspired by
* @see https://codepen.io/niggi/pen/DtIfy
*/
function detectIosVersion() {
if (/iP(hone|od|ad)/.test(navigator.platform)) {
var v = navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/);
const result = [
parseInt(v[1], 10),
parseInt(v[2], 10),
parseInt(v[3] || 0, 10)
].join(".");
return result;
}
return "-";
}
/**
* Detect Apple OS by testing if `window.ontouchstart` exists
*
* @see https://stackoverflow.com/q/58019463/1238150
*
* Alternatives
* - https://stackoverflow.com/a/57654258/1238150 (with `TouchEvent`)
* - https://stackoverflow.com/a/60553965/1238150 (with `Canvas`)
*/
function detectAppleOs() {
if (/Safari[\/\s](\d+\.\d+)/.test(window.navigator.userAgent)) {
return "ontouchstart" in window ? "iOS" : "macOS";
}
if (window.navigator.platform === "MacIntel") {
return "macOS";
}
return "No Apple Device";
}
/**
* Apple Device detection with >iOS13 support
*
* What is `navigator.standalone`?
* > Returns a boolean indicating whether the browser is running in standalone mode.
* > Available on Apple's iOS Safari only.
* > (https://developer.mozilla.org/de/docs/Web/API/Navigator)
*
* @see https://stackoverflow.com/a/62980780/1238150
* @see https://racase.com.np/javascript-how-to-detect-if-device-is-ios/
*
* Misc:
* - detect if site is running in standalone (added to home screen):
* https://stackoverflow.com/a/40932368/1238150
^ */
function detectIosDevices() {
// devices prior to iOS 13
if (/iPad|iPhone|iPod/.test(navigator.platform)) {
return navigator.platform;
}
// or with document.createEvent('TouchEvent'), which is only available on iPads (and iPhones)
return !!(
navigator.platform === "MacIntel" &&
typeof navigator.standalone !== "undefined"
)
? "iPad"
: navigator.platform === "MacIntel"
? "Macbook"
: "No Apple Device";
}
/**
* Detects iPhone, iPad and Desktop computer
*
* inspired by
* @see https://stackoverflow.com/a/59016446/1238150 (Nov. 2019)
*/
function detectDevice() {
var agent = window.navigator.userAgent;
// iPhone
IsIPhone = agent.match(/iPhone/i) != null;
// iPad up to IOS12
IsIPad = agent.match(/iPad/i) != null;
if (IsIPad) {
IsIPhone = false;
}
// iPad from IOS13
var macApp = agent.match(/Macintosh/i) != null;
// also used in https://stackoverflow.com/a/60553965/1238150
if (macApp) {
// need to distinguish between Macbook and iPad
var canvas = document.createElement("canvas");
if (canvas != null) {
var context =
canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
if (context) {
var info = context.getExtension("WEBGL_debug_renderer_info");
if (info) {
var renderer = context.getParameter(info.UNMASKED_RENDERER_WEBGL);
if (renderer.indexOf("Apple") != -1) {
IsIPad = true;
}
}
}
}
}
if (IsIPhone) {
return "iPhone";
} else if (IsIPad) {
return "iPad";
} else {
// right now we do not distinguish between the two of them
return "Desktop computer";
}
}
/**
* Detects if current Browser is Chrome. Also works on iOS devices.
*/
function detectChrome() {
// http://browserhacks.com/
// https://stackoverflow.com/a/9851769/1238150
const isChrome =
!!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
// https://stackoverflow.com/a/37178303/1238150
return /Chrome|CriOS/i.test(navigator.userAgent) || isChrome;
}
function detectFirefox() {
return /Firefox|FxiOS/i.test(navigator.userAgent);
}
function detectBrowser() {
if (detectChrome()) {
return "Chrome";
}
if (detectFirefox()) {
return "Firefox";
}
// Attention: catchall-fallback with no guarantee
return "Safari";
}
// DOMContentLoaded
// @see https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded
document.addEventListener("DOMContentLoaded", function () {
// navigator
document.getElementById("useragent").innerText = navigator.userAgent || "-";
document.getElementById("platform").innerText = navigator.platform || "-";
document.getElementById("appVersion").innerText = navigator.appVersion || "-";
document.getElementById("vendor").innerText = navigator.vendor || "-";
// feature detection
document.getElementById("AppleOS").innerText = detectAppleOs();
document.getElementById("AppleDevice").innerText = detectIosDevices();
document.getElementById("DeviceDetection").innerText = detectDevice();
document.getElementById("Browser").innerText = detectBrowser();
document.getElementById("iOSVersion").innerText = detectIosVersion();
});
Also see: Tab Triggers