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.
<p id="myTextWrapper">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
</p>
#myTextWrapper {
width: 80%;
margin: 0 auto;
margin-top: 30px;
font-family: sans-serif;
font-size: 13px;
line-height: 1.5;
}
.zeroWidth {
border-left: 1px solid #ccc;
margin-left: -1px;
}
.zeroWidth.end-of-line {
background-color: red;
border-left: 1px solid red;
margin-left: -1px;
}
.counter {
border-bottom: 1px solid #ccc;
position: absolute;
right: 5%;
font-size: 10px;
}
.error {
color: red;
}
function addZeroWidthElems(el){
// we expect that el has only one child, a text node: el.firstChild.nodeType === 3
// split the textNode into an array of words, then remove it from the DOM
var words = el.firstChild.nodeValue.trim().split(/\s+/);
el.removeChild(el.firstChild);
// when the zero-width marker elements are created, we will store them on the
// parent element for easy retrieval
el.zeroWidthElems = new Array();
// declare a variable for creating new text nodes
var newTextNode;
// loop through the words array, returning each word to the DOM as a textNode
// followed by marker element.
for(var i = 0; i < words.length; i++) {
// return each word into the element, with a space if necessary
newTextNode = document.createTextNode( ( i ? ' ' : '' ) + words[i] );
el.appendChild(newTextNode);
// create a zero-width element and store it for later access
el.zeroWidthElems.push( document.createElement('span') );
el.zeroWidthElems[i].className = 'zeroWidth';
el.appendChild(el.zeroWidthElems[i]);
}
}
function forEachTextLine(el, callback, stringArray) {
// call the above function to insert zero-width elements (if they haven't been already)
if(!el.hasOwnProperty('zeroWidthElems')) {
addZeroWidthElems(el);
}
// we will keep track of which line number we are on
var lineCount = 0;
var charachterCountNoSpaces = 0;
var charachterCountSpaces = 0;
var spaceCount = 0;
var lineCount = 0;
// declare variables for calculating element position
var maxTop = 0, curTop = 0;
// find the last zeroWidth element
while (lineCount < 10) {
for(var i = 0; i < el.zeroWidthElems.length; i++) {
// get the offset relative to the parent element
curTop = el.zeroWidthElems[i].offsetTop;
charachterCountNoSpaces = charachterCountNoSpaces + stringArray[i].length;
spaceCount++;
// console.log('Ord '+i+': '+stringArray[i]+' '+stringArray[i].length);
// the first element inspected is automatically the max
if(i === 0) {
maxTop = curTop;
}
// if the vertical position is different, the text has wrapped
if(curTop != maxTop) {
// update the maximum
maxTop = curTop;
// update the line count
lineCount++;
console.log(lineCount);
/*
Example line 1 and 2:
Lorem ipsum dolor sit amet, consectetuer
adipiscing elit, sed diam nonummy nibh
For some reason the counter includes the first word on the second line into the charachter count of line 1.
Ord 0: Lorem 5
Ord 1: ipsum 5
Ord 2: dolor 5
Ord 3: sit 3
Ord 4: amet, 5
Ord 5: consectetuer 12
Ord 6: adipiscing 10
Line 1 w/o spaces: 35
Line 1 w 7 spaces: 42
This needs to be taken into account and therefore we, below, need:
charachterCountNoSpaces = charachterCountNoSpaces - stringArray[i].length;
*/
charachterCountNoSpaces = charachterCountNoSpaces - stringArray[i].length;
charachterCountSpaces = charachterCountNoSpaces + spaceCount;
// console.log('Line '+lineCount+' w/o spaces: '+ charachterCountNoSpaces);
// console.log('Line '+lineCount+' w '+spaceCount+' spaces: '+charachterCountSpaces);
if ( (charachterCountSpaces < 80) && (charachterCountSpaces > 45) ) {
el.zeroWidthElems[i - 1].innerHTML = '<span class="counter">'+charachterCountSpaces+'</span>';
} else {
el.zeroWidthElems[i - 1].innerHTML = '<span class="counter error">'+charachterCountSpaces+'</span>';
}
// do something now that we have found the end of a line
callback.call(el, el.zeroWidthElems[i - 1], lineCount);
charachterCountSpaces = 0;
charachterCountNoSpaces = 0;
spaceCount = 0;
}
// if this is the last element, it is automatically the end of a line
else if(i == el.zeroWidthElems.length - 1) {
lineCount++;
callback.call(el, el.zeroWidthElems[i], lineCount);
}
} // END FOR
} // END WHILE
// return the number of lines in the text node
return lineCount;
}
// Splits string to array at separator
function splitString(stringToSplit, separator) {
var arrayOfStrings = stringToSplit.split(separator);
return arrayOfStrings;
}
var processEndOfLine = function (endOfLine, lineNumber) {
// do something to each line
endOfLine.className += ' end-of-line';
};
var myTextWrapper = document.getElementById('myTextWrapper');
stringArray = splitString(myTextWrapper.innerHTML, ' ');
var lineCount = forEachTextLine(myTextWrapper, processEndOfLine, stringArray);
// resorting to some jQuery
$(window).on('resize', function(){
// undo anything that might have been done in a previous call to processEndOfLine()
$('.end-of-line').removeClass('end-of-line');
// repeat processEndOfLine() now that the wrapping might have changed
lineCount = forEachTextLine(myTextWrapper, processEndOfLine, stringArray);
});
Also see: Tab Triggers