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.
v<div class="container">
<div class="jumbotron">
<div class="container">
<h2>execCommands supported in your browser</h2>
<div class="row">
<div class="col-md-6">The <small class="btn btn-success btn-xs">Green</small> commands are supported by your browser, where as the <small class="btn btn-error btn-xs">Grey</small> ones.. You guessed it! :-) <br />Anyway try to select contents below and click <small class="btn btn-success btn-xs">commands</small> to execute the commands on it. <br />I use the HTML attribute <code>contenteditable=true</code> to allow modifications to the content. <code>Updated</code>: Added <a href="https://fortawesome.github.io/Font-Awesome/icons/">font awesome icons</a>. <br /> <br />Inspired by <a href="https://css-tricks.com/cut-and-copy-from-javascript/">Cut and Copy (from JavaScript)</a></div>
<div class="col-md-6">
<code>2017-06-04 updated:</code><br>
<em>As pointed out by <a href="https://twitter.com/Robvanhe">@Robvanhe</a> the pen did not actually work in Safari. It was due to selection being cleared after click, so no selection to apply command existed. Fixed now. Rewritten also to vanilla javascript (no use for Angular).</em>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12 tags buttons">
</div>
</div>
<div class="row" contentEditable="true">
<div class="col-md-3"> <br /><br />From <a href="https://favpng.com/png_view/clint-eastwood-dirty-harry-charles-scorpio-killer-davis-film-unforgiven-png/jW3R6uVq">favpng.com</a>
<img src="https://assets.codepen.io/9729/dirtyharry.png" alt="" class="img-responsive" /><h4>CLINT EASTWOOD</h4></div>
<div class="col-md-9">
<div class="row" contentEditable="true">
<div class="col-md-12">
<h3>Try to alter this text using the execCommands buttons above</h3>
You see, in this world there's two kinds of people, my friend: those with loaded guns and those who dig. you dig. well, do you have anything to say for yourself? you see, in this world there's two kinds of people, my friend: those with loaded guns and those who dig. you dig. <code>don't p!ss down my back and tell me it's raining</code>. dyin' ain't much of a livin', boy. are you feeling lucky punk this is the ak-47 assault rifle, the preferred weapon of your enemy; and <u>it makes a distinctive sound</u> when fired at you, so remember it. when a naked man's chasing a woman through an alley with a butcher knife and a hard-on, i figure he's not out collecting for the red cross. man's gotta know his limitations. ever notice how sometimes you come across somebody you shouldn't have <a href="https://google.com?q=f**ked" target="_blank" title="Try to ”unlink“ this link.">f**ked</a> with? well, i'm that guy. you see, in this world there's two kinds of people, my friend: those with loaded guns and those who dig. you dig. man's gotta know his limitations. <br /><br /></div>
<div class="col-md-4">You see, in this world there's two kinds of people, my friend: those with loaded guns and those who dig. you dig. well, do you have anything to say for yourself? you want a guarantee, buy a toaster. You want a guarantee, buy a toaster. don't p!ss down my back and tell me it's raining. This is the <b>ak-47</b> assault rifle, the preferred weapon of your enemy; and it makes a <u>distinctive sound</u> when fired at you, <br /><br /></div>
<div class="col-md-4"><img src="http://dummyimage.com/300x220?text=From%20dummyimage.com" alt="" class="img-responsive" /></div>
<div class="col-md-4">…so remember it. dyin' ain't much of a livin', boy. ever notice how sometimes you come across somebody you shouldn't have f**ked with? well, i'm that guy. what you have to ask yourself is, do i feel lucky. well do ya' punk? man's gotta know his limitations. are you feeling lucky punk dyin' ain't much of a livin', boy.</div>
</div>
<div class="row" contentEditable="true">
<div class="col-md-12">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod neque eaque fuga culpa doloribus adipisci vel explicabo! Nihil ipsa commodi quae voluptates laudantium fugit nam. Itaque veniam iure aliquid harum!</div>
</div>
</div>
</div>
</div>
</div>
@import url(https://fonts.googleapis.com/css?family=Inconsolata:400,700|Open+Sans:400,800);
body {
font-family: 'Open Sans', sans-serif;
}
.jumbotron {
padding: 0 0 20px;
font-family: 'Inconsolata', monospace ;
}
.btn-succes {
background-color: green;
color: yellow;
}
.btn-error {
background-color: grey;
color: black;
}
.btn-succes, .btn-succes {
margin: 5px;
}
i {
color:white;
}
.btn-error i {
color: black;
}
.btn {
-webkit-user-select: none; /* webkit (safari, chrome) browsers */
-moz-user-select: none; /* mozilla browsers */
-khtml-user-select: none; /* webkit (konqueror) browsers */
-ms-user-select: none; /* IE10+ */
}
:focus {
outline: none;
}
var parms = [
{
cmd: "aCommandName",
desc: "A DOMString representing the name of the command"
},
{
cmd: "aShowDefaultUI",
desc:
"A Boolean indicating whether the default user interface should be shown. This is not implemented in Mozilla."
},
{
cmd: "aValueArgument",
desc:
"A DOMString representing some commands (such as insertimage) require an extra value argument (the image's url). Pass an argument of null if no argument is needed."
}
];
var commands = [
{
cmd: "backColor",
val: "red",
desc:
"Changes the document background color. In styleWithCss mode, it affects the background color of the containing block instead. This requires a color value string to be passed in as a value argument. (Internet Explorer uses this to set text background color.)"
},
{
cmd: "bold",
icon: "bold",
desc:
"Toggles bold on/off for the selection or at the insertion point. (Internet Explorer uses the STRONG tag instead of B.)"
},
{
cmd: "contentReadOnly",
desc:
"Makes the content document either read-only or editable. This requires a boolean true/false to be passed in as a value argument. (Not supported by Internet Explorer.)"
},
{
cmd: "copy",
icon: "clipboard",
desc:
"Copies the current selection to the clipboard. Clipboard capability must be enabled in the user.js preference file. See"
},
{
cmd: "createLink",
val: "https://twitter.com/netsi1964",
icon: "link",
desc:
"Creates an anchor link from the selection, only if there is a selection. This requires the HREF URI string to be passed in as a value argument. The URI must contain at least a single character, which may be a white space. (Internet Explorer will create a link with a null URI value.)"
},
{
cmd: "cut",
icon: "scissors",
desc:
"Cuts the current selection and copies it to the clipboard. Clipboard capability must be enabled in the user.js preference file. See"
},
{
cmd: "decreaseFontSize",
desc:
"Adds a SMALL tag around the selection or at the insertion point. (Not supported by Internet Explorer.)"
},
{
cmd: "delete",
icon: "scissors",
desc: "Deletes the current selection."
},
{
cmd: "enableInlineTableEditing",
desc:
"Enables or disables the table row and column insertion and deletion controls. (Not supported by Internet Explorer.)"
},
{
cmd: "enableObjectResizing",
desc:
"Enables or disables the resize handles on images and other resizable objects. (Not supported by Internet Explorer.)"
},
{
cmd: "fontName",
val: "'Inconsolata', monospace",
desc:
'Changes the font name for the selection or at the insertion point. This requires a font name string ("Arial" for example) to be passed in as a value argument.'
},
{
cmd: "fontSize",
val: "1-7",
icon: "text-height",
desc:
"Changes the font size for the selection or at the insertion point. This requires an HTML font size (1-7) to be passed in as a value argument."
},
{
cmd: "foreColor",
val: "rgba(0,0,0,.5)",
desc:
"Changes a font color for the selection or at the insertion point. This requires a color value string to be passed in as a value argument."
},
{
cmd: "formatBlock",
val: "<blockquote>",
desc:
'Adds an HTML block-style tag around the line containing the current selection, replacing the block element containing the line if one exists (in Firefox, BLOCKQUOTE is the exception - it will wrap any containing block element). Requires a tag-name string to be passed in as a value argument. Virtually all block style tags can be used (eg. "H1", "P", "DL", "BLOCKQUOTE"). (Internet Explorer supports only heading tags H1 - H6, ADDRESS, and PRE, which must also include the tag delimiters < >, such as "<H1>".)'
},
{
cmd: "forwardDelete",
desc:
"Deletes the character ahead of the cursor's position. It is the same as hitting the delete key."
},
{
cmd: "heading",
val: "h3",
icon: "header",
desc:
'Adds a heading tag around a selection or insertion point line. Requires the tag-name string to be passed in as a value argument (i.e. "H1", "H6"). (Not supported by Internet Explorer and Safari.)'
},
{
cmd: "hiliteColor",
val: "Orange",
desc:
"Changes the background color for the selection or at the insertion point. Requires a color value string to be passed in as a value argument. UseCSS must be turned on for this to function. (Not supported by Internet Explorer.)"
},
{
cmd: "increaseFontSize",
desc:
"Adds a BIG tag around the selection or at the insertion point. (Not supported by Internet Explorer.)"
},
{
cmd: "indent",
icon: "indent",
desc:
"Indents the line containing the selection or insertion point. In Firefox, if the selection spans multiple lines at different levels of indentation, only the least indented lines in the selection will be indented."
},
{
cmd: "insertBrOnReturn",
desc:
"Controls whether the Enter key inserts a br tag or splits the current block element into two. (Not supported by Internet Explorer.)"
},
{
cmd: "insertHorizontalRule",
desc:
"Inserts a horizontal rule at the insertion point (deletes selection)."
},
{
cmd: "insertHTML",
val: "<h3>Life is great!</h3>",
icon: "code",
desc:
"Inserts an HTML string at the insertion point (deletes selection). Requires a valid HTML string to be passed in as a value argument. (Not supported by Internet Explorer.)"
},
{
cmd: "insertImage",
val: "http://dummyimage.com/160x90",
icon: "picture-o",
desc:
"Inserts an image at the insertion point (deletes selection). Requires the image SRC URI string to be passed in as a value argument. The URI must contain at least a single character, which may be a white space. (Internet Explorer will create a link with a null URI value.)"
},
{
cmd: "insertOrderedList",
icon: "list-ol",
desc:
"Creates a numbered ordered list for the selection or at the insertion point."
},
{
cmd: "insertUnorderedList",
icon: "list-ul",
desc:
"Creates a bulleted unordered list for the selection or at the insertion point."
},
{
cmd: "insertParagraph",
icon: "paragraph",
desc:
"Inserts a paragraph around the selection or the current line. (Internet Explorer inserts a paragraph at the insertion point and deletes the selection.)"
},
{
cmd: "insertText",
val: new Date(),
icon: "file-text-o",
desc:
"Inserts the given plain text at the insertion point (deletes selection)."
},
{
cmd: "italic",
icon: "italic",
desc:
"Toggles italics on/off for the selection or at the insertion point. (Internet Explorer uses the EM tag instead of I.)"
},
{
cmd: "justifyCenter",
icon: "align-center",
desc: "Centers the selection or insertion point."
},
{
cmd: "justifyFull",
icon: "align-justify",
desc: "Justifies the selection or insertion point."
},
{
cmd: "justifyLeft",
icon: "align-left",
desc: "Justifies the selection or insertion point to the left."
},
{
cmd: "justifyRight",
icon: "align-right",
desc: "Right-justifies the selection or the insertion point."
},
{
cmd: "outdent",
icon: "outdent",
desc: "Outdents the line containing the selection or insertion point."
},
{
cmd: "paste",
icon: "clipboard",
desc:
"Pastes the clipboard contents at the insertion point (replaces current selection). Clipboard capability must be enabled in the user.js preference file. See"
},
{
cmd: "redo",
icon: "repeat",
desc: "Redoes the previous undo command."
},
{
cmd: "removeFormat",
desc: "Removes all formatting from the current selection."
},
{
cmd: "selectAll",
desc: "Selects all of the content of the editable region."
},
{
cmd: "strikeThrough",
icon: "strikethrough",
desc:
"Toggles strikethrough on/off for the selection or at the insertion point."
},
{
cmd: "subscript",
icon: "subscript",
desc:
"Toggles subscript on/off for the selection or at the insertion point."
},
{
cmd: "superscript",
icon: "superscript",
desc:
"Toggles superscript on/off for the selection or at the insertion point."
},
{
cmd: "underline",
icon: "underline",
desc:
"Toggles underline on/off for the selection or at the insertion point."
},
{
cmd: "undo",
icon: "undo",
desc: "Undoes the last executed command."
},
{
cmd: "unlink",
icon: "chain-broken",
desc: "Removes the anchor tag from a selected anchor link."
},
{
cmd: "useCSS ",
desc:
"Toggles the use of HTML tags or CSS for the generated markup. Requires a boolean true/false as a value argument. NOTE: This argument is logically backwards (i.e. use false to use CSS, true to use HTML). (Not supported by Internet Explorer.) This has been deprecated; use the styleWithCSS command instead."
},
{
cmd: "styleWithCSS",
desc:
"Replaces the useCSS command; argument works as expected, i.e. true modifies/generates style attributes in markup, false generates formatting elements."
}
];
var commandRelation = {};
function supported(cmd) {
var css = !!document.queryCommandSupported(cmd.cmd)
? "btn-succes"
: "btn-error";
return css;
}
function icon(cmd) {
return typeof cmd.icon !== "undefined" ? "fa fa-" + cmd.icon : "";
}
function doCommand(cmdKey) {
var cmd = commandRelation[cmdKey];
if (supported(cmd) === "btn-error") {
alert("execCommand(“" + cmd.cmd + "”)\nis not supported in your browser");
return;
}
val =
typeof cmd.val !== "undefined"
? prompt("Value for " + cmd.cmd + "?", cmd.val)
: "";
document.execCommand(cmd.cmd, false, val || ""); // Thanks to https://codepen.io/bluestreak for finding this bug
}
function init() {
var html = "",
template =
'<span><code class="btn btn-xs %btnClass%" title="%desc%" onmousedown="event.preventDefault();" onclick="doCommand(\'%cmd%\')"><i class="%iconClass%"></i> %cmd%</code></span>';
commands.map(function (command, i) {
commandRelation[command.cmd] = command;
var temp = template;
temp = temp.replace(/%iconClass%/gi, icon(command));
temp = temp.replace(/%desc%/gi, command.desc);
temp = temp.replace(/%btnClass%/gi, supported(command));
temp = temp.replace(/%cmd%/gi, command.cmd);
html += temp;
});
document.querySelector(".buttons").innerHTML = html;
}
init();
Also see: Tab Triggers