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.
<figure>
<figcaption id="caption">
<h1>Countries with most population</h1>
</figcaption>
<div class="table-wrapper" role="group" aria-labelledby="caption">
<table>
<tbody>
<tr>
<th>Rank</th>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
<tr>
<th>Country</th>
<td>China</td>
<td>United States</td>
<td>Indonesia</td>
<td>Pakistan</td>
<td>Brazil</td>
<td>Nigeria</td>
<td>Bangladesh</td>
</tr>
<tr>
<th>Population</th>
<td>1,439,323,776</td>
<td>331,002,651</td>
<td>273,523,615</td>
<td>220,892,340</td>
<td>212,559,417</td>
<td>206,139,589</td>
<td>164,689,383</td>
</tr>
<tr>
<th>World-share %</th>
<td>18.47</td>
<td>4.25</td>
<td>3.51</td>
<td>2.83</td>
<td>2.73</td>
<td>2.64</td>
<td>2.11</td>
</tr>
</tbody>
</table>
</div>
</figure>
* {
box-sizing: border-box;
}
:root {
--color-primary: #2e4a40;
--color-secondary: #e6efa3;
--color-tertiary: rgba(0, 0, 0, 0.2);
--font-body: "Switzer", sans-serif;
--font-headings: "Gambarino", sans-serif;
}
body {
color: var(--color-primary);
font-family: var(--font-body);
font-size: 22px;
line-height: 1.5;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
padding: 2rem;
background-color: var(--color-secondary);
}
figure {
width: 100%;
max-width: 800px;
}
table {
text-align: left;
border-collapse: collapse;
margin-bottom: 1rem;
table-layout: fixed;
white-space: nowrap;
border-radius: 3rem;
width: min(auto, 100%);
}
table tbody {
border: 2px solid var(--color-tertiary);
}
tbody td {
vertical-align: top;
}
th {
font-family: var(--font-headings);
font-weight: bold;
}
tr > * {
padding: 0.75rem 1rem;
}
tr > *:nth-child(even) {
background-color: var(--color-tertiary);
}
tr:first-child > td {
font-family: var(--font-headings);
font-weight: bold;
}
thead {
border-bottom: 1px solid var(--color-primary);
font-size: var(--step-1);
}
tr > * {
border-bottom: 2px solid var(--color-tertiary);
border-top: 2px solid var(--color-tertiary);
}
.table-wrapper {
overflow: auto;
}
.force-mobile {
background-color: var(--color-secondary);
min-height: calc(100vh - 6rem);
width: 100%;
max-width: 720px; margin: 0 auto;
}
::-webkit-scrollbar {
height: 1rem;
z-index: 100;
position: relative;
}
::-webkit-scrollbar-thumb {
background: var(--color-primary);
}
::-webkit-scrollbar-track {
background: var(--color-tertiary);
}
caption {
text-align: left;
margin-bottom: 1rem;
}
h1 {
margin-top: 0;
margin-bottom: 1rem;
font-family: var(--font-headings);
font-size: 1.5rem;
display: block;
}
// Thanks to Adrian Roselli for this really useful snippet https://adrianroselli.com/2017/11/a-responsive-accessible-table.html
// https://adrianroselli.com/2018/02/tables-css-display-properties-and-aria.html
// https://adrianroselli.com/2018/05/functions-to-add-aria-to-tables-and-lists.html
function AddTableARIA() {
try {
var allTables = document.querySelectorAll("table");
for (var i = 0; i < allTables.length; i++) {
allTables[i].setAttribute("role", "table");
}
var allRowGroups = document.querySelectorAll("thead, tbody, tfoot");
for (var i = 0; i < allRowGroups.length; i++) {
allRowGroups[i].setAttribute("role", "rowgroup");
}
var allRows = document.querySelectorAll("tr");
for (var i = 0; i < allRows.length; i++) {
allRows[i].setAttribute("role", "row");
}
var allCells = document.querySelectorAll("td");
for (var i = 0; i < allCells.length; i++) {
allCells[i].setAttribute("role", "cell");
}
var allHeaders = document.querySelectorAll("th");
for (var i = 0; i < allHeaders.length; i++) {
allHeaders[i].setAttribute("role", "columnheader");
}
// this accounts for scoped row headers
var allRowHeaders = document.querySelectorAll("th[scope=row]");
for (var i = 0; i < allRowHeaders.length; i++) {
allRowHeaders[i].setAttribute("role", "rowheader");
}
// caption role not needed as it is not a real role and
// browsers do not dump their own role with display block
} catch (e) {
console.log("AddTableARIA(): " + e);
}
}
AddTableARIA();
const MAX_COLS = 5;
const MIN_COLS = 2;
function cropTable(visibleCols) {
const table = document.querySelector("figure");
const { width: tableWidth } = table.getBoundingClientRect();
const cols = table.querySelectorAll("th, td");
const newWidth = tableWidth / visibleCols;
// Resize columns to fit table
cols.forEach(function (col) {
// Always make sure that col is cropped by at least 15%
col.style.minWidth = newWidth + (newWidth * 0.15) + "px";
});
// Return if we are about to fall below min column count
if (visibleCols <= MIN_COLS) {
return;
}
// Measure a sample table column to check if resizing was successful
const { width: colWidth } = cols[0].getBoundingClientRect();
// Check if we should crop to 1 column less (calculate new column width)
if (colWidth * visibleCols > tableWidth + newWidth * 0.95) {
cropTable(visibleCols - 1);
}
}
window.addEventListener("resize", function () {
cropTable(MAX_COLS);
});
cropTable(MAX_COLS);
Also see: Tab Triggers