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.
<!--
pass to the ".truncate" element a "style" attribute
with "--line-clamp" desired lines to clamp
-->
<div class='truncate' contenteditable spellcheck='false'>
The <em><code>-webkit-line-clamp</code></em> CSS property allows limiting of the contents of a block container to the specified number of lines.
It <span>only</span> works in combination with the display property set to -webkit-box or -webkit-inline-box and the -webkit-box-orient property set to vertical.
In most cases you will also want to set overflow to hidden, otherwise the contents won't be clipped but an <span>ellipsis</span> will still be shown after the specified number of lines.
</div>
.truncate{
display: -webkit-box;
-webkit-line-clamp: var(--line-clamp, 1);
-webkit-box-orient: vertical;
word-break: var(--word-break, 'none');
overflow: hidden;
hyphens: auto;
text-align: var(--align, left);
// Automatically use "word-break: break-all" for single-lines
// (https://css-tricks.com/css-switch-case-conditions)
--is-single-line: 1 - Clamp(0, Calc(var(--line-clamp) - 1), var(--line-clamp));
--delay: Calc(-1s * (var(--is-single-line, 1) - 1));
animation: states 1s var(--delay) paused;
@keyframes states {
0% { word-break: break-all; }
}
}
/**** JUST FOR THIS DEMO: ****/
html,body{ height:100%; }
body{
--bg: #4A90E2;
background: var(--bg);
height: 100vh;
display: grid;
place-items: center;
font: calc(1vw + 1.2em)/1.4 Roboto, Arial;
color: white;
}
.truncate{
font-size: var(--font-size, 1em);
width: var(--width, 50%);
text-shadow: 1px 3px 0 rgba(black, .3);
position: relative;
background: inherit;
&:hover{
outline: 2px dotted #FFFFFF99;
}
// fade text's last word, if clamped ()
&::after{
content: '';
width: 0;
height: 2ch;
position: absolute;
bottom: 0;
right: 0;
pointer-events: none;
//background: linear-gradient(90deg, transparent, var(--bg));
background: inherit; // it's easier to work with inheritance
mask-image: linear-gradient(to left, black, transparent);
transition: .2s;
}
&.clamped{
&::after{
width: 3em;
}
}
> span{
text-decoration: underline;
}
}
// Top-right controls
const elm = document.querySelector('.truncate')
let debounce;
const myKnobs = new Knobs({
CSSVarTarget: elm,
visible: 2,
knobs: [
{
cssVar: ['line-clamp'],
label: 'Lines to clamp',
type: 'range',
value: 2,
min: 1,
max: 10,
step: 1,
onChange: checkTruncated
},
{
cssVar: ['width', '%'],
label: 'Width',
type: 'range',
value: 50,
min: 10,
max: 100,
step: 1,
onChange: checkTruncated
},
{
cssVar: ['font-size', 'em'],
label: 'Font size',
type: 'range',
value: 1,
min: .6,
max: 1.5,
step: .1,
onChange: checkTruncated
},
// break-all allows a better truncating for a single-line clamps,
// so the text will fit best within the container
/*
{
cssVar: ['word-break'],
label: 'Word Break',
type: 'checkbox',
labelTitle: 'will automatically be "breal-all" for single-lines',
checked: true,
value: 'break-all'
},
*/
{
cssVar: ['align'],
label: 'Align text',
type: 'radio',
name: 'align-radio-group',
options: [
{ value:'left', hidden:true, label: '<svg viewBox="0 0 28 28"><path d="M28 21v2c0 0.547-0.453 1-1 1h-26c-0.547 0-1-0.453-1-1v-2c0-0.547 0.453-1 1-1h26c0.547 0 1 0.453 1 1zM22 15v2c0 0.547-0.453 1-1 1h-20c-0.547 0-1-0.453-1-1v-2c0-0.547 0.453-1 1-1h20c0.547 0 1 0.453 1 1zM26 9v2c0 0.547-0.453 1-1 1h-24c-0.547 0-1-0.453-1-1v-2c0-0.547 0.453-1 1-1h24c0.547 0 1 0.453 1 1zM20 3v2c0 0.547-0.453 1-1 1h-18c-0.547 0-1-0.453-1-1v-2c0-0.547 0.453-1 1-1h18c0.547 0 1 0.453 1 1z"></path></svg>' },
{ value:'center', hidden:true, label:'<svg viewBox="0 0 1024 1024"><path d="M1024 768v73.143c0 20-16.571 36.571-36.571 36.571h-950.857c-20 0-36.571-16.571-36.571-36.571v-73.143c0-20 16.571-36.571 36.571-36.571h950.857c20 0 36.571 16.571 36.571 36.571zM804.571 548.571v73.143c0 20-16.571 36.571-36.571 36.571h-512c-20 0-36.571-16.571-36.571-36.571v-73.143c0-20 16.571-36.571 36.571-36.571h512c20 0 36.571 16.571 36.571 36.571zM950.857 329.143v73.143c0 20-16.571 36.571-36.571 36.571h-804.571c-20 0-36.571-16.571-36.571-36.571v-73.143c0-20 16.571-36.571 36.571-36.571h804.571c20 0 36.571 16.571 36.571 36.571zM731.429 109.714v73.143c0 20-16.571 36.571-36.571 36.571h-365.714c-20 0-36.571-16.571-36.571-36.571v-73.143c0-20 16.571-36.571 36.571-36.571h365.714c20 0 36.571 16.571 36.571 36.571z"></path></svg>' },
{ value:'right', hidden:true, label:'<svg viewBox="0 0 28 28"><path d="M28 21v2c0 0.547-0.453 1-1 1h-26c-0.547 0-1-0.453-1-1v-2c0-0.547 0.453-1 1-1h26c0.547 0 1 0.453 1 1zM28 15v2c0 0.547-0.453 1-1 1h-20c-0.547 0-1-0.453-1-1v-2c0-0.547 0.453-1 1-1h20c0.547 0 1 0.453 1 1zM28 9v2c0 0.547-0.453 1-1 1h-24c-0.547 0-1-0.453-1-1v-2c0-0.547 0.453-1 1-1h24c0.547 0 1 0.453 1 1zM28 3v2c0 0.547-0.453 1-1 1h-18c-0.547 0-1-0.453-1-1v-2c0-0.547 0.453-1 1-1h18c0.547 0 1 0.453 1 1z"></path></svg>' }
],
value: 'left',
defaultValue: 'left'
},
"Readonly:",
{
label: 'Truncated detected',
type: 'checkbox',
disabled: true,
checked: true,
id: 'isTruncatedToggle'
},
]
})
const isTextClamped = elm => elm.scrollHeight > elm.clientHeight
function checkTruncated(){
clearTimeout(debounce)
debounce = setTimeout(() => {
const clamped = isTextClamped(elm)
myKnobs.DOM.form.querySelector('#isTruncatedToggle').checked = clamped
elm.classList.toggle('clamped', clamped)
}
, 100)
}
// optional part - reset any possible scroll
// within truncated elements, because scrolling
// is possiign via keyboard.
window.addEventListener('scroll', e => {
if( e.target.matches('.truncate') )
e.target.scroll(0,0)
}, true);
Also see: Tab Triggers