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>
Taille actuelle de la fenêtre :
<span id="window_size"></span> px
</div>
<br />
<div id="dynamic" class="resize fresize">
</div>
<pre>
*****************
*** PRÉAMBULE ***
*****************
- les variables représentant la taille (résolution et éléments) sont sans unités
(pixels) car les calculs dans clamp() (et calc()) sont soumis à certaines règles :
- On ne peut diviser que par un nombre (et non une unité).
- Un côté d'une multiplication doit être un nombre (et non une unité).
- On peut facilement affecter une unité à un nombre en le multipliant par 1 pixel.
*********************
*** MISE EN PLACE ***
*********************
La tranche de résolution sur laquelle on travaille est placée au niveau du root
(plus haut niveau du DOM).
La formule est mise en place dans une classe (.resize) pour éviter une redondance
dans le code CSS. Une 2nde classe (.fresize) est créé spécifiquement pour la police
car on ne peut pas à la fois cibler l'élément et la police au sein d'un sélecteur CSS.
Concernant l'élement ciblé, on va utiliser la propriété aspect-ratio pour préserver
le ratio et donc cibler uniquement la largeur de l'élément.
Les variables --min, --max, --fmin et fmax sont définies dans le scope de l'élément.
Elles sont donc réutilisables dans n'importe quel autre élément sans conflit.
******************
*** LA FORMULE ***
******************
(Taille minimale de l'élément) + {(ratio d'étirement) * (étirement maximum de l'élément)}
Taille minimale de l'élément : 100px dans l'exemple
Étirement maximum de l'élément : 100 px (taille maximum - taille minimum)
Ratio d'étirement : (étirement de la fenêtre depuis 900px) / (étirement pamamétré soit 300px (1200px - 900px))
Par exemple, si on a une fenêtre de 1100px, on a étiré de 200px, soit 2/3
de l'étirement de fenêtre paramétré. On étire donc notre élémment de 2/3 de 100px, soit 67px.
L'utilisation de clamp(valeur minimale, formule, valeur maximale) permet de contenir les
valeurs de formules hors normes (valeurs négatives ou taille de fenêtre supérieure aux paramètres).
</pre>
:root {
--reso_min: 900;
--reso_max: 1200;
}
.resize {
--resize: clamp(var(--min) * 1px,
(var(--min) * 1px) +
(((100vw - (var(--reso_min) * 1px)) / (var(--reso_max) - var(--reso_min))) * (var(--max) - var(--min))), var(--max) * 1px)
}
.fresize {
--fresize: clamp(var(--fmin) * 1px,
(var(--fmin) * 1px) +
(((100vw - (var(--reso_min) * 1px)) / (var(--reso_max) - var(--reso_min))) * (var(--fmax) - var(--fmin))), var(--fmax) * 1px)
}
#dynamic {
display: flex;
align-items: center; justify-content: center;
font-weight: 700;
background: green;
aspect-ratio: 1 / 1;
--min: 100;
--max: 200;
width: var(--resize);
--fmin: 10;
--fmax: 20;
font-size: var(--fresize);
}
const window_size = document.getElementById('window_size');
const dynamic = document.getElementById('dynamic');
window_size.textContent = window.innerWidth;
dynamic.textContent = dynamic.clientWidth + ' px';
window.addEventListener('resize', function() {
window_size.textContent = window.innerWidth;
dynamic.textContent = dynamic.clientWidth + ' px';
});
Also see: Tab Triggers