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.
<html>
<head>
<title>Modernizing legacy web frontends</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- html structure that was originally build by jQuery in showDetail as EJS template -->
<script type="text/template" id="heroDetailTemplate">
<img style="width: 150px; float: right;" src="<%= hero.image %>">
<h1>Details of <%= hero.name %></h1>
<p>The secret identity of <%= hero.name %> is <%= hero.secretIdentity %>
and he first appeared in a comic in the year <%= hero.firstAppearance %>.</p>
<a class="button jsShowAllHeroesButton" href="#">Show all heroes</a>
</script>
<!-- container that contains the rendered web application -->
<div id="app" class="container">
<div id="heroList" class="row"></div>
<div id="heroDetail" class="row" style="display: none;"></div>
</div>
</body>
</html>
"use strict";
// this example is part of a blog post:
// https://www.lars-berning.de/modernizing-legacy-web-frontends-adding-templates-part-1
// for demo purposes the data is embedded, usually this would be the result of an ajax fetch or similar
var heroData = [
{ name: "Superman", secretIdentity: "Clark Kent", firstAppearance: "1932", image: "https://upload.wikimedia.org/wikipedia/en/thumb/e/eb/SupermanRoss.png/250px-SupermanRoss.png"},
{ name: "Batman", secretIdentity: "Bruce Wayne", firstAppearance: "1939", image: "https://upload.wikimedia.org/wikipedia/en/thumb/7/74/Batman_Detective_Comics_Vol_2_1.png/250px-Batman_Detective_Comics_Vol_2_1.png"},
{ name: "Flash", secretIdentity: "Barry Allen", firstAppearance: "1956", image: "https://upload.wikimedia.org/wikipedia/en/thumb/b/b7/Flash_v1_105.jpg/250px-Flash_v1_105.jpg"},
{ name: "Green Lantern", secretIdentity: "Hal Jordan", firstAppearance: "1959", image: "https://upload.wikimedia.org/wikipedia/en/thumb/8/80/Green_Lantern_Rebirth_6.jpg/220px-Green_Lantern_Rebirth_6.jpg"},
{ name: "Hulk", secretIdentity: "Robert Bruce Banner", firstAppearance: "1962", image: "https://upload.wikimedia.org/wikipedia/en/5/59/Hulk_%28comics_character%29.png"},
{ name: "Spider-Man", secretIdentity: "Peter Parker", firstAppearance: "1962", image: "https://upload.wikimedia.org/wikipedia/en/thumb/2/21/Web_of_Spider-Man_Vol_1_129-1.png/250px-Web_of_Spider-Man_Vol_1_129-1.png"},
{ name: "Iron Man", secretIdentity: "Tony Stark", firstAppearance: "1963", image: "https://upload.wikimedia.org/wikipedia/en/thumb/e/e0/Iron_Man_bleeding_edge.jpg/220px-Iron_Man_bleeding_edge.jpg"},
{ name: "Wolverine", secretIdentity: "James Howlett", firstAppearance: "1974", image: "https://upload.wikimedia.org/wikipedia/en/c/c8/Marvelwolverine.jpg"}
]
// bootstrap web frontend
function init(){
renderHeroes();
}
// renders the list of superheroes
function renderHeroes() {
var heroList = $("#heroList");
var table = $("<table />").addClass("u-full-width");
table.append($("<thead><tr><th>Name</th><th>Secret Identity</th><th>Details</th></tr></thead>"));
var tableBody = $("<tbody />");
for (var index = 0; index < heroData.length; index++){
tableBody.append(buildHero(heroData[index], index));
}
table.append(tableBody);
heroList.append(table);
};
// used by function renderHeroes to create the jQuery representation of a superhero
function buildHero(item, index) {
var tableRow = $("<tr />");
var nameElem = $("<td />").html(item.name);
var secretIdentityElem = $("<td />").html(item.secretIdentity);
var detailLink = $("<a />").attr("href", "#").html("Show Details").click({index: index}, showDetail);
var showDetailElem = $("<td />").append(detailLink);
tableRow.append(nameElem).append(secretIdentityElem).append(showDetailElem);
return tableRow;
};
// renders the detail view of a hero, event contains the index of the selected hero
function showDetail(event) {
event.preventDefault();
var hero = heroData[event.data.index];
var detail = $("#heroDetail");
detail.empty();
// get the template as string (defined in html file)
var heroDetailTemplate = $('#heroDetailTemplate').html();
// use lodash to compile the template
var compiledHeroDetailTemplate = _.template(heroDetailTemplate);
// render the template with the data of a hero and attach it to the DOM
detail.append(compiledHeroDetailTemplate({hero: hero}));
// since the template is a plain string we have to add the click listener afterwards
detail.find(".jsShowAllHeroesButton").click(showHeroes);
$("#heroList").hide();
detail.show();
};
function showHeroes(event) {
event.preventDefault();
$("#heroDetail").hide();
$("#heroList").show();
};
// call the init function when the page is loaded
$( init );
Also see: Tab Triggers